deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / arm64 / assembler-arm64.h
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 #ifndef V8_ARM64_ASSEMBLER_ARM64_H_
6 #define V8_ARM64_ASSEMBLER_ARM64_H_
7
8 #include <deque>
9 #include <list>
10 #include <map>
11 #include <vector>
12
13 #include "src/arm64/instructions-arm64.h"
14 #include "src/assembler.h"
15 #include "src/compiler.h"
16 #include "src/globals.h"
17 #include "src/utils.h"
18
19
20 namespace v8 {
21 namespace internal {
22
23
24 // -----------------------------------------------------------------------------
25 // Registers.
26 #define REGISTER_CODE_LIST(R)                                                  \
27 R(0)  R(1)  R(2)  R(3)  R(4)  R(5)  R(6)  R(7)                                 \
28 R(8)  R(9)  R(10) R(11) R(12) R(13) R(14) R(15)                                \
29 R(16) R(17) R(18) R(19) R(20) R(21) R(22) R(23)                                \
30 R(24) R(25) R(26) R(27) R(28) R(29) R(30) R(31)
31
32
33 static const int kRegListSizeInBits = sizeof(RegList) * kBitsPerByte;
34
35
36 // Some CPURegister methods can return Register and FPRegister types, so we
37 // need to declare them in advance.
38 struct Register;
39 struct FPRegister;
40
41
42 struct CPURegister {
43   enum RegisterType {
44     // The kInvalid value is used to detect uninitialized static instances,
45     // which are always zero-initialized before any constructors are called.
46     kInvalid = 0,
47     kRegister,
48     kFPRegister,
49     kNoRegister
50   };
51
52   static CPURegister Create(unsigned code, unsigned size, RegisterType type) {
53     CPURegister r = {code, size, type};
54     return r;
55   }
56
57   unsigned code() const;
58   RegisterType type() const;
59   RegList Bit() const;
60   unsigned SizeInBits() const;
61   int SizeInBytes() const;
62   bool Is32Bits() const;
63   bool Is64Bits() const;
64   bool IsValid() const;
65   bool IsValidOrNone() const;
66   bool IsValidRegister() const;
67   bool IsValidFPRegister() const;
68   bool IsNone() const;
69   bool Is(const CPURegister& other) const;
70   bool Aliases(const CPURegister& other) const;
71
72   bool IsZero() const;
73   bool IsSP() const;
74
75   bool IsRegister() const;
76   bool IsFPRegister() const;
77
78   Register X() const;
79   Register W() const;
80   FPRegister D() const;
81   FPRegister S() const;
82
83   bool IsSameSizeAndType(const CPURegister& other) const;
84
85   // V8 compatibility.
86   bool is(const CPURegister& other) const { return Is(other); }
87   bool is_valid() const { return IsValid(); }
88
89   unsigned reg_code;
90   unsigned reg_size;
91   RegisterType reg_type;
92 };
93
94
95 struct Register : public CPURegister {
96   static Register Create(unsigned code, unsigned size) {
97     return Register(CPURegister::Create(code, size, CPURegister::kRegister));
98   }
99
100   Register() {
101     reg_code = 0;
102     reg_size = 0;
103     reg_type = CPURegister::kNoRegister;
104   }
105
106   explicit Register(const CPURegister& r) {
107     reg_code = r.reg_code;
108     reg_size = r.reg_size;
109     reg_type = r.reg_type;
110     DCHECK(IsValidOrNone());
111   }
112
113   Register(const Register& r) {  // NOLINT(runtime/explicit)
114     reg_code = r.reg_code;
115     reg_size = r.reg_size;
116     reg_type = r.reg_type;
117     DCHECK(IsValidOrNone());
118   }
119
120   bool IsValid() const {
121     DCHECK(IsRegister() || IsNone());
122     return IsValidRegister();
123   }
124
125   static Register XRegFromCode(unsigned code);
126   static Register WRegFromCode(unsigned code);
127
128   // Start of V8 compatibility section ---------------------
129   // These memebers are necessary for compilation.
130   // A few of them may be unused for now.
131
132   static const int kNumRegisters = kNumberOfRegisters;
133   static int NumRegisters() { return kNumRegisters; }
134
135   // We allow crankshaft to use the following registers:
136   //   - x0 to x15
137   //   - x18 to x24
138   //   - x27 (also context)
139   //
140   // TODO(all): Register x25 is currently free and could be available for
141   // crankshaft, but we don't use it as we might use it as a per function
142   // literal pool pointer in the future.
143   //
144   // TODO(all): Consider storing cp in x25 to have only two ranges.
145   // We split allocatable registers in three ranges called
146   //   - "low range"
147   //   - "high range"
148   //   - "context"
149   static const unsigned kAllocatableLowRangeBegin = 0;
150   static const unsigned kAllocatableLowRangeEnd = 15;
151   static const unsigned kAllocatableHighRangeBegin = 18;
152   static const unsigned kAllocatableHighRangeEnd = 24;
153   static const unsigned kAllocatableContext = 27;
154
155   // Gap between low and high ranges.
156   static const int kAllocatableRangeGapSize =
157       (kAllocatableHighRangeBegin - kAllocatableLowRangeEnd) - 1;
158
159   static const int kMaxNumAllocatableRegisters =
160       (kAllocatableLowRangeEnd - kAllocatableLowRangeBegin + 1) +
161       (kAllocatableHighRangeEnd - kAllocatableHighRangeBegin + 1) + 1;  // cp
162   static int NumAllocatableRegisters() { return kMaxNumAllocatableRegisters; }
163
164   // Return true if the register is one that crankshaft can allocate.
165   bool IsAllocatable() const {
166     return ((reg_code == kAllocatableContext) ||
167             (reg_code <= kAllocatableLowRangeEnd) ||
168             ((reg_code >= kAllocatableHighRangeBegin) &&
169              (reg_code <= kAllocatableHighRangeEnd)));
170   }
171
172   static Register FromAllocationIndex(unsigned index) {
173     DCHECK(index < static_cast<unsigned>(NumAllocatableRegisters()));
174     // cp is the last allocatable register.
175     if (index == (static_cast<unsigned>(NumAllocatableRegisters() - 1))) {
176       return from_code(kAllocatableContext);
177     }
178
179     // Handle low and high ranges.
180     return (index <= kAllocatableLowRangeEnd)
181         ? from_code(index)
182         : from_code(index + kAllocatableRangeGapSize);
183   }
184
185   static const char* AllocationIndexToString(int index) {
186     DCHECK((index >= 0) && (index < NumAllocatableRegisters()));
187     DCHECK((kAllocatableLowRangeBegin == 0) &&
188            (kAllocatableLowRangeEnd == 15) &&
189            (kAllocatableHighRangeBegin == 18) &&
190            (kAllocatableHighRangeEnd == 24) &&
191            (kAllocatableContext == 27));
192     const char* const names[] = {
193       "x0", "x1", "x2", "x3", "x4",
194       "x5", "x6", "x7", "x8", "x9",
195       "x10", "x11", "x12", "x13", "x14",
196       "x15", "x18", "x19", "x20", "x21",
197       "x22", "x23", "x24", "x27",
198     };
199     return names[index];
200   }
201
202   static int ToAllocationIndex(Register reg) {
203     DCHECK(reg.IsAllocatable());
204     unsigned code = reg.code();
205     if (code == kAllocatableContext) {
206       return NumAllocatableRegisters() - 1;
207     }
208
209     return (code <= kAllocatableLowRangeEnd)
210         ? code
211         : code - kAllocatableRangeGapSize;
212   }
213
214   static Register from_code(int code) {
215     // Always return an X register.
216     return Register::Create(code, kXRegSizeInBits);
217   }
218
219   // End of V8 compatibility section -----------------------
220 };
221
222
223 struct FPRegister : public CPURegister {
224   static FPRegister Create(unsigned code, unsigned size) {
225     return FPRegister(
226         CPURegister::Create(code, size, CPURegister::kFPRegister));
227   }
228
229   FPRegister() {
230     reg_code = 0;
231     reg_size = 0;
232     reg_type = CPURegister::kNoRegister;
233   }
234
235   explicit FPRegister(const CPURegister& r) {
236     reg_code = r.reg_code;
237     reg_size = r.reg_size;
238     reg_type = r.reg_type;
239     DCHECK(IsValidOrNone());
240   }
241
242   FPRegister(const FPRegister& r) {  // NOLINT(runtime/explicit)
243     reg_code = r.reg_code;
244     reg_size = r.reg_size;
245     reg_type = r.reg_type;
246     DCHECK(IsValidOrNone());
247   }
248
249   bool IsValid() const {
250     DCHECK(IsFPRegister() || IsNone());
251     return IsValidFPRegister();
252   }
253
254   static FPRegister SRegFromCode(unsigned code);
255   static FPRegister DRegFromCode(unsigned code);
256
257   // Start of V8 compatibility section ---------------------
258   static const int kMaxNumRegisters = kNumberOfFPRegisters;
259
260   // Crankshaft can use all the FP registers except:
261   //   - d15 which is used to keep the 0 double value
262   //   - d30 which is used in crankshaft as a double scratch register
263   //   - d31 which is used in the MacroAssembler as a double scratch register
264   static const unsigned kAllocatableLowRangeBegin = 0;
265   static const unsigned kAllocatableLowRangeEnd = 14;
266   static const unsigned kAllocatableHighRangeBegin = 16;
267   static const unsigned kAllocatableHighRangeEnd = 28;
268
269   static const RegList kAllocatableFPRegisters = 0x1fff7fff;
270
271   // Gap between low and high ranges.
272   static const int kAllocatableRangeGapSize =
273       (kAllocatableHighRangeBegin - kAllocatableLowRangeEnd) - 1;
274
275   static const int kMaxNumAllocatableRegisters =
276       (kAllocatableLowRangeEnd - kAllocatableLowRangeBegin + 1) +
277       (kAllocatableHighRangeEnd - kAllocatableHighRangeBegin + 1);
278   static int NumAllocatableRegisters() { return kMaxNumAllocatableRegisters; }
279
280   // TODO(turbofan): Proper float32 support.
281   static int NumAllocatableAliasedRegisters() {
282     return NumAllocatableRegisters();
283   }
284
285   // Return true if the register is one that crankshaft can allocate.
286   bool IsAllocatable() const {
287     return (Bit() & kAllocatableFPRegisters) != 0;
288   }
289
290   static FPRegister FromAllocationIndex(unsigned int index) {
291     DCHECK(index < static_cast<unsigned>(NumAllocatableRegisters()));
292
293     return (index <= kAllocatableLowRangeEnd)
294         ? from_code(index)
295         : from_code(index + kAllocatableRangeGapSize);
296   }
297
298   static const char* AllocationIndexToString(int index) {
299     DCHECK((index >= 0) && (index < NumAllocatableRegisters()));
300     DCHECK((kAllocatableLowRangeBegin == 0) &&
301            (kAllocatableLowRangeEnd == 14) &&
302            (kAllocatableHighRangeBegin == 16) &&
303            (kAllocatableHighRangeEnd == 28));
304     const char* const names[] = {
305       "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
306       "d8", "d9", "d10", "d11", "d12", "d13", "d14",
307       "d16", "d17", "d18", "d19", "d20", "d21", "d22", "d23",
308       "d24", "d25", "d26", "d27", "d28"
309     };
310     return names[index];
311   }
312
313   static int ToAllocationIndex(FPRegister reg) {
314     DCHECK(reg.IsAllocatable());
315     unsigned code = reg.code();
316
317     return (code <= kAllocatableLowRangeEnd)
318         ? code
319         : code - kAllocatableRangeGapSize;
320   }
321
322   static FPRegister from_code(int code) {
323     // Always return a D register.
324     return FPRegister::Create(code, kDRegSizeInBits);
325   }
326   // End of V8 compatibility section -----------------------
327 };
328
329
330 STATIC_ASSERT(sizeof(CPURegister) == sizeof(Register));
331 STATIC_ASSERT(sizeof(CPURegister) == sizeof(FPRegister));
332
333
334 #if defined(ARM64_DEFINE_REG_STATICS)
335 #define INITIALIZE_REGISTER(register_class, name, code, size, type)      \
336   const CPURegister init_##register_class##_##name = {code, size, type}; \
337   const register_class& name = *reinterpret_cast<const register_class*>( \
338                                     &init_##register_class##_##name)
339 #define ALIAS_REGISTER(register_class, alias, name)                       \
340   const register_class& alias = *reinterpret_cast<const register_class*>( \
341                                      &init_##register_class##_##name)
342 #else
343 #define INITIALIZE_REGISTER(register_class, name, code, size, type) \
344   extern const register_class& name
345 #define ALIAS_REGISTER(register_class, alias, name) \
346   extern const register_class& alias
347 #endif  // defined(ARM64_DEFINE_REG_STATICS)
348
349 // No*Reg is used to indicate an unused argument, or an error case. Note that
350 // these all compare equal (using the Is() method). The Register and FPRegister
351 // variants are provided for convenience.
352 INITIALIZE_REGISTER(Register, NoReg, 0, 0, CPURegister::kNoRegister);
353 INITIALIZE_REGISTER(FPRegister, NoFPReg, 0, 0, CPURegister::kNoRegister);
354 INITIALIZE_REGISTER(CPURegister, NoCPUReg, 0, 0, CPURegister::kNoRegister);
355
356 // v8 compatibility.
357 INITIALIZE_REGISTER(Register, no_reg, 0, 0, CPURegister::kNoRegister);
358
359 #define DEFINE_REGISTERS(N)                                                  \
360   INITIALIZE_REGISTER(Register, w##N, N,                                     \
361                       kWRegSizeInBits, CPURegister::kRegister);              \
362   INITIALIZE_REGISTER(Register, x##N, N,                                     \
363                       kXRegSizeInBits, CPURegister::kRegister);
364 REGISTER_CODE_LIST(DEFINE_REGISTERS)
365 #undef DEFINE_REGISTERS
366
367 INITIALIZE_REGISTER(Register, wcsp, kSPRegInternalCode, kWRegSizeInBits,
368                     CPURegister::kRegister);
369 INITIALIZE_REGISTER(Register, csp, kSPRegInternalCode, kXRegSizeInBits,
370                     CPURegister::kRegister);
371
372 #define DEFINE_FPREGISTERS(N)                                                  \
373   INITIALIZE_REGISTER(FPRegister, s##N, N,                                     \
374                       kSRegSizeInBits, CPURegister::kFPRegister);              \
375   INITIALIZE_REGISTER(FPRegister, d##N, N,                                     \
376                       kDRegSizeInBits, CPURegister::kFPRegister);
377 REGISTER_CODE_LIST(DEFINE_FPREGISTERS)
378 #undef DEFINE_FPREGISTERS
379
380 #undef INITIALIZE_REGISTER
381
382 // Registers aliases.
383 ALIAS_REGISTER(Register, ip0, x16);
384 ALIAS_REGISTER(Register, ip1, x17);
385 ALIAS_REGISTER(Register, wip0, w16);
386 ALIAS_REGISTER(Register, wip1, w17);
387 // Root register.
388 ALIAS_REGISTER(Register, root, x26);
389 ALIAS_REGISTER(Register, rr, x26);
390 // Context pointer register.
391 ALIAS_REGISTER(Register, cp, x27);
392 // We use a register as a JS stack pointer to overcome the restriction on the
393 // architectural SP alignment.
394 // We chose x28 because it is contiguous with the other specific purpose
395 // registers.
396 STATIC_ASSERT(kJSSPCode == 28);
397 ALIAS_REGISTER(Register, jssp, x28);
398 ALIAS_REGISTER(Register, wjssp, w28);
399 ALIAS_REGISTER(Register, fp, x29);
400 ALIAS_REGISTER(Register, lr, x30);
401 ALIAS_REGISTER(Register, xzr, x31);
402 ALIAS_REGISTER(Register, wzr, w31);
403
404 // Keeps the 0 double value.
405 ALIAS_REGISTER(FPRegister, fp_zero, d15);
406 // Crankshaft double scratch register.
407 ALIAS_REGISTER(FPRegister, crankshaft_fp_scratch, d29);
408 // MacroAssembler double scratch registers.
409 ALIAS_REGISTER(FPRegister, fp_scratch, d30);
410 ALIAS_REGISTER(FPRegister, fp_scratch1, d30);
411 ALIAS_REGISTER(FPRegister, fp_scratch2, d31);
412
413 #undef ALIAS_REGISTER
414
415
416 Register GetAllocatableRegisterThatIsNotOneOf(Register reg1,
417                                               Register reg2 = NoReg,
418                                               Register reg3 = NoReg,
419                                               Register reg4 = NoReg);
420
421
422 // AreAliased returns true if any of the named registers overlap. Arguments set
423 // to NoReg are ignored. The system stack pointer may be specified.
424 bool AreAliased(const CPURegister& reg1,
425                 const CPURegister& reg2,
426                 const CPURegister& reg3 = NoReg,
427                 const CPURegister& reg4 = NoReg,
428                 const CPURegister& reg5 = NoReg,
429                 const CPURegister& reg6 = NoReg,
430                 const CPURegister& reg7 = NoReg,
431                 const CPURegister& reg8 = NoReg);
432
433 // AreSameSizeAndType returns true if all of the specified registers have the
434 // same size, and are of the same type. The system stack pointer may be
435 // specified. Arguments set to NoReg are ignored, as are any subsequent
436 // arguments. At least one argument (reg1) must be valid (not NoCPUReg).
437 bool AreSameSizeAndType(const CPURegister& reg1,
438                         const CPURegister& reg2,
439                         const CPURegister& reg3 = NoCPUReg,
440                         const CPURegister& reg4 = NoCPUReg,
441                         const CPURegister& reg5 = NoCPUReg,
442                         const CPURegister& reg6 = NoCPUReg,
443                         const CPURegister& reg7 = NoCPUReg,
444                         const CPURegister& reg8 = NoCPUReg);
445
446
447 typedef FPRegister DoubleRegister;
448
449
450 // -----------------------------------------------------------------------------
451 // Lists of registers.
452 class CPURegList {
453  public:
454   explicit CPURegList(CPURegister reg1,
455                       CPURegister reg2 = NoCPUReg,
456                       CPURegister reg3 = NoCPUReg,
457                       CPURegister reg4 = NoCPUReg)
458       : list_(reg1.Bit() | reg2.Bit() | reg3.Bit() | reg4.Bit()),
459         size_(reg1.SizeInBits()), type_(reg1.type()) {
460     DCHECK(AreSameSizeAndType(reg1, reg2, reg3, reg4));
461     DCHECK(IsValid());
462   }
463
464   CPURegList(CPURegister::RegisterType type, unsigned size, RegList list)
465       : list_(list), size_(size), type_(type) {
466     DCHECK(IsValid());
467   }
468
469   CPURegList(CPURegister::RegisterType type, unsigned size,
470              unsigned first_reg, unsigned last_reg)
471       : size_(size), type_(type) {
472     DCHECK(((type == CPURegister::kRegister) &&
473             (last_reg < kNumberOfRegisters)) ||
474            ((type == CPURegister::kFPRegister) &&
475             (last_reg < kNumberOfFPRegisters)));
476     DCHECK(last_reg >= first_reg);
477     list_ = (1UL << (last_reg + 1)) - 1;
478     list_ &= ~((1UL << first_reg) - 1);
479     DCHECK(IsValid());
480   }
481
482   CPURegister::RegisterType type() const {
483     DCHECK(IsValid());
484     return type_;
485   }
486
487   RegList list() const {
488     DCHECK(IsValid());
489     return list_;
490   }
491
492   inline void set_list(RegList new_list) {
493     DCHECK(IsValid());
494     list_ = new_list;
495   }
496
497   // Combine another CPURegList into this one. Registers that already exist in
498   // this list are left unchanged. The type and size of the registers in the
499   // 'other' list must match those in this list.
500   void Combine(const CPURegList& other);
501
502   // Remove every register in the other CPURegList from this one. Registers that
503   // do not exist in this list are ignored. The type of the registers in the
504   // 'other' list must match those in this list.
505   void Remove(const CPURegList& other);
506
507   // Variants of Combine and Remove which take CPURegisters.
508   void Combine(const CPURegister& other);
509   void Remove(const CPURegister& other1,
510               const CPURegister& other2 = NoCPUReg,
511               const CPURegister& other3 = NoCPUReg,
512               const CPURegister& other4 = NoCPUReg);
513
514   // Variants of Combine and Remove which take a single register by its code;
515   // the type and size of the register is inferred from this list.
516   void Combine(int code);
517   void Remove(int code);
518
519   // Remove all callee-saved registers from the list. This can be useful when
520   // preparing registers for an AAPCS64 function call, for example.
521   void RemoveCalleeSaved();
522
523   CPURegister PopLowestIndex();
524   CPURegister PopHighestIndex();
525
526   // AAPCS64 callee-saved registers.
527   static CPURegList GetCalleeSaved(unsigned size = kXRegSizeInBits);
528   static CPURegList GetCalleeSavedFP(unsigned size = kDRegSizeInBits);
529
530   // AAPCS64 caller-saved registers. Note that this includes lr.
531   static CPURegList GetCallerSaved(unsigned size = kXRegSizeInBits);
532   static CPURegList GetCallerSavedFP(unsigned size = kDRegSizeInBits);
533
534   // Registers saved as safepoints.
535   static CPURegList GetSafepointSavedRegisters();
536
537   bool IsEmpty() const {
538     DCHECK(IsValid());
539     return list_ == 0;
540   }
541
542   bool IncludesAliasOf(const CPURegister& other1,
543                        const CPURegister& other2 = NoCPUReg,
544                        const CPURegister& other3 = NoCPUReg,
545                        const CPURegister& other4 = NoCPUReg) const {
546     DCHECK(IsValid());
547     RegList list = 0;
548     if (!other1.IsNone() && (other1.type() == type_)) list |= other1.Bit();
549     if (!other2.IsNone() && (other2.type() == type_)) list |= other2.Bit();
550     if (!other3.IsNone() && (other3.type() == type_)) list |= other3.Bit();
551     if (!other4.IsNone() && (other4.type() == type_)) list |= other4.Bit();
552     return (list_ & list) != 0;
553   }
554
555   int Count() const {
556     DCHECK(IsValid());
557     return CountSetBits(list_, kRegListSizeInBits);
558   }
559
560   unsigned RegisterSizeInBits() const {
561     DCHECK(IsValid());
562     return size_;
563   }
564
565   unsigned RegisterSizeInBytes() const {
566     int size_in_bits = RegisterSizeInBits();
567     DCHECK((size_in_bits % kBitsPerByte) == 0);
568     return size_in_bits / kBitsPerByte;
569   }
570
571   unsigned TotalSizeInBytes() const {
572     DCHECK(IsValid());
573     return RegisterSizeInBytes() * Count();
574   }
575
576  private:
577   RegList list_;
578   unsigned size_;
579   CPURegister::RegisterType type_;
580
581   bool IsValid() const {
582     const RegList kValidRegisters = 0x8000000ffffffff;
583     const RegList kValidFPRegisters = 0x0000000ffffffff;
584     switch (type_) {
585       case CPURegister::kRegister:
586         return (list_ & kValidRegisters) == list_;
587       case CPURegister::kFPRegister:
588         return (list_ & kValidFPRegisters) == list_;
589       case CPURegister::kNoRegister:
590         return list_ == 0;
591       default:
592         UNREACHABLE();
593         return false;
594     }
595   }
596 };
597
598
599 // AAPCS64 callee-saved registers.
600 #define kCalleeSaved CPURegList::GetCalleeSaved()
601 #define kCalleeSavedFP CPURegList::GetCalleeSavedFP()
602
603
604 // AAPCS64 caller-saved registers. Note that this includes lr.
605 #define kCallerSaved CPURegList::GetCallerSaved()
606 #define kCallerSavedFP CPURegList::GetCallerSavedFP()
607
608 // -----------------------------------------------------------------------------
609 // Immediates.
610 class Immediate {
611  public:
612   template<typename T>
613   inline explicit Immediate(Handle<T> handle);
614
615   // This is allowed to be an implicit constructor because Immediate is
616   // a wrapper class that doesn't normally perform any type conversion.
617   template<typename T>
618   inline Immediate(T value);  // NOLINT(runtime/explicit)
619
620   template<typename T>
621   inline Immediate(T value, RelocInfo::Mode rmode);
622
623   int64_t value() const { return value_; }
624   RelocInfo::Mode rmode() const { return rmode_; }
625
626  private:
627   void InitializeHandle(Handle<Object> value);
628
629   int64_t value_;
630   RelocInfo::Mode rmode_;
631 };
632
633
634 // -----------------------------------------------------------------------------
635 // Operands.
636 const int kSmiShift = kSmiTagSize + kSmiShiftSize;
637 const uint64_t kSmiShiftMask = (1UL << kSmiShift) - 1;
638
639 // Represents an operand in a machine instruction.
640 class Operand {
641   // TODO(all): If necessary, study more in details which methods
642   // TODO(all): should be inlined or not.
643  public:
644   // rm, {<shift> {#<shift_amount>}}
645   // where <shift> is one of {LSL, LSR, ASR, ROR}.
646   //       <shift_amount> is uint6_t.
647   // This is allowed to be an implicit constructor because Operand is
648   // a wrapper class that doesn't normally perform any type conversion.
649   inline Operand(Register reg,
650                  Shift shift = LSL,
651                  unsigned shift_amount = 0);  // NOLINT(runtime/explicit)
652
653   // rm, <extend> {#<shift_amount>}
654   // where <extend> is one of {UXTB, UXTH, UXTW, UXTX, SXTB, SXTH, SXTW, SXTX}.
655   //       <shift_amount> is uint2_t.
656   inline Operand(Register reg,
657                  Extend extend,
658                  unsigned shift_amount = 0);
659
660   template<typename T>
661   inline explicit Operand(Handle<T> handle);
662
663   // Implicit constructor for all int types, ExternalReference, and Smi.
664   template<typename T>
665   inline Operand(T t);  // NOLINT(runtime/explicit)
666
667   // Implicit constructor for int types.
668   template<typename T>
669   inline Operand(T t, RelocInfo::Mode rmode);
670
671   inline bool IsImmediate() const;
672   inline bool IsShiftedRegister() const;
673   inline bool IsExtendedRegister() const;
674   inline bool IsZero() const;
675
676   // This returns an LSL shift (<= 4) operand as an equivalent extend operand,
677   // which helps in the encoding of instructions that use the stack pointer.
678   inline Operand ToExtendedRegister() const;
679
680   inline Immediate immediate() const;
681   inline int64_t ImmediateValue() const;
682   inline Register reg() const;
683   inline Shift shift() const;
684   inline Extend extend() const;
685   inline unsigned shift_amount() const;
686
687   // Relocation information.
688   bool NeedsRelocation(const Assembler* assembler) const;
689
690   // Helpers
691   inline static Operand UntagSmi(Register smi);
692   inline static Operand UntagSmiAndScale(Register smi, int scale);
693
694  private:
695   Immediate immediate_;
696   Register reg_;
697   Shift shift_;
698   Extend extend_;
699   unsigned shift_amount_;
700 };
701
702
703 // MemOperand represents a memory operand in a load or store instruction.
704 class MemOperand {
705  public:
706   inline MemOperand();
707   inline explicit MemOperand(Register base,
708                              int64_t offset = 0,
709                              AddrMode addrmode = Offset);
710   inline explicit MemOperand(Register base,
711                              Register regoffset,
712                              Shift shift = LSL,
713                              unsigned shift_amount = 0);
714   inline explicit MemOperand(Register base,
715                              Register regoffset,
716                              Extend extend,
717                              unsigned shift_amount = 0);
718   inline explicit MemOperand(Register base,
719                              const Operand& offset,
720                              AddrMode addrmode = Offset);
721
722   const Register& base() const { return base_; }
723   const Register& regoffset() const { return regoffset_; }
724   int64_t offset() const { return offset_; }
725   AddrMode addrmode() const { return addrmode_; }
726   Shift shift() const { return shift_; }
727   Extend extend() const { return extend_; }
728   unsigned shift_amount() const { return shift_amount_; }
729   inline bool IsImmediateOffset() const;
730   inline bool IsRegisterOffset() const;
731   inline bool IsPreIndex() const;
732   inline bool IsPostIndex() const;
733
734   // For offset modes, return the offset as an Operand. This helper cannot
735   // handle indexed modes.
736   inline Operand OffsetAsOperand() const;
737
738   enum PairResult {
739     kNotPair,   // Can't use a pair instruction.
740     kPairAB,    // Can use a pair instruction (operandA has lower address).
741     kPairBA     // Can use a pair instruction (operandB has lower address).
742   };
743   // Check if two MemOperand are consistent for stp/ldp use.
744   static PairResult AreConsistentForPair(const MemOperand& operandA,
745                                          const MemOperand& operandB,
746                                          int access_size_log2 = kXRegSizeLog2);
747
748  private:
749   Register base_;
750   Register regoffset_;
751   int64_t offset_;
752   AddrMode addrmode_;
753   Shift shift_;
754   Extend extend_;
755   unsigned shift_amount_;
756 };
757
758
759 class ConstPool {
760  public:
761   explicit ConstPool(Assembler* assm)
762       : assm_(assm),
763         first_use_(-1),
764         shared_entries_count(0) {}
765   void RecordEntry(intptr_t data, RelocInfo::Mode mode);
766   int EntryCount() const {
767     return shared_entries_count + unique_entries_.size();
768   }
769   bool IsEmpty() const {
770     return shared_entries_.empty() && unique_entries_.empty();
771   }
772   // Distance in bytes between the current pc and the first instruction
773   // using the pool. If there are no pending entries return kMaxInt.
774   int DistanceToFirstUse();
775   // Offset after which instructions using the pool will be out of range.
776   int MaxPcOffset();
777   // Maximum size the constant pool can be with current entries. It always
778   // includes alignment padding and branch over.
779   int WorstCaseSize();
780   // Size in bytes of the literal pool *if* it is emitted at the current
781   // pc. The size will include the branch over the pool if it was requested.
782   int SizeIfEmittedAtCurrentPc(bool require_jump);
783   // Emit the literal pool at the current pc with a branch over the pool if
784   // requested.
785   void Emit(bool require_jump);
786   // Discard any pending pool entries.
787   void Clear();
788
789  private:
790   bool CanBeShared(RelocInfo::Mode mode);
791   void EmitMarker();
792   void EmitGuard();
793   void EmitEntries();
794
795   Assembler* assm_;
796   // Keep track of the first instruction requiring a constant pool entry
797   // since the previous constant pool was emitted.
798   int first_use_;
799   // values, pc offset(s) of entries which can be shared.
800   std::multimap<uint64_t, int> shared_entries_;
801   // Number of distinct literal in shared entries.
802   int shared_entries_count;
803   // values, pc offset of entries which cannot be shared.
804   std::vector<std::pair<uint64_t, int> > unique_entries_;
805 };
806
807
808 // -----------------------------------------------------------------------------
809 // Assembler.
810
811 class Assembler : public AssemblerBase {
812  public:
813   // Create an assembler. Instructions and relocation information are emitted
814   // into a buffer, with the instructions starting from the beginning and the
815   // relocation information starting from the end of the buffer. See CodeDesc
816   // for a detailed comment on the layout (globals.h).
817   //
818   // If the provided buffer is NULL, the assembler allocates and grows its own
819   // buffer, and buffer_size determines the initial buffer size. The buffer is
820   // owned by the assembler and deallocated upon destruction of the assembler.
821   //
822   // If the provided buffer is not NULL, the assembler uses the provided buffer
823   // for code generation and assumes its size to be buffer_size. If the buffer
824   // is too small, a fatal error occurs. No deallocation of the buffer is done
825   // upon destruction of the assembler.
826   Assembler(Isolate* arg_isolate, void* buffer, int buffer_size);
827
828   virtual ~Assembler();
829
830   virtual void AbortedCodeGeneration() {
831     constpool_.Clear();
832   }
833
834   // System functions ---------------------------------------------------------
835   // Start generating code from the beginning of the buffer, discarding any code
836   // and data that has already been emitted into the buffer.
837   //
838   // In order to avoid any accidental transfer of state, Reset DCHECKs that the
839   // constant pool is not blocked.
840   void Reset();
841
842   // GetCode emits any pending (non-emitted) code and fills the descriptor
843   // desc. GetCode() is idempotent; it returns the same result if no other
844   // Assembler functions are invoked in between GetCode() calls.
845   //
846   // The descriptor (desc) can be NULL. In that case, the code is finalized as
847   // usual, but the descriptor is not populated.
848   void GetCode(CodeDesc* desc);
849
850   // Insert the smallest number of nop instructions
851   // possible to align the pc offset to a multiple
852   // of m. m must be a power of 2 (>= 4).
853   void Align(int m);
854
855   inline void Unreachable();
856
857   // Label --------------------------------------------------------------------
858   // Bind a label to the current pc. Note that labels can only be bound once,
859   // and if labels are linked to other instructions, they _must_ be bound
860   // before they go out of scope.
861   void bind(Label* label);
862
863
864   // RelocInfo and pools ------------------------------------------------------
865
866   // Record relocation information for current pc_.
867   void RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data = 0);
868
869   // Return the address in the constant pool of the code target address used by
870   // the branch/call instruction at pc.
871   inline static Address target_pointer_address_at(Address pc);
872
873   // Read/Modify the code target address in the branch/call instruction at pc.
874   inline static Address target_address_at(Address pc,
875                                           ConstantPoolArray* constant_pool);
876   inline static void set_target_address_at(Address pc,
877                                            ConstantPoolArray* constant_pool,
878                                            Address target,
879                                            ICacheFlushMode icache_flush_mode =
880                                                FLUSH_ICACHE_IF_NEEDED);
881   static inline Address target_address_at(Address pc, Code* code);
882   static inline void set_target_address_at(Address pc,
883                                            Code* code,
884                                            Address target,
885                                            ICacheFlushMode icache_flush_mode =
886                                                FLUSH_ICACHE_IF_NEEDED);
887
888   // Return the code target address at a call site from the return address of
889   // that call in the instruction stream.
890   inline static Address target_address_from_return_address(Address pc);
891
892   // Given the address of the beginning of a call, return the address in the
893   // instruction stream that call will return from.
894   inline static Address return_address_from_call_start(Address pc);
895
896   // Return the code target address of the patch debug break slot
897   inline static Address break_address_from_return_address(Address pc);
898
899   // This sets the branch destination (which is in the constant pool on ARM).
900   // This is for calls and branches within generated code.
901   inline static void deserialization_set_special_target_at(
902       Address constant_pool_entry, Code* code, Address target);
903
904   // This sets the internal reference at the pc.
905   inline static void deserialization_set_target_internal_reference_at(
906       Address pc, Address target,
907       RelocInfo::Mode mode = RelocInfo::INTERNAL_REFERENCE);
908
909   // All addresses in the constant pool are the same size as pointers.
910   static const int kSpecialTargetSize = kPointerSize;
911
912   // The sizes of the call sequences emitted by MacroAssembler::Call.
913   // Wherever possible, use MacroAssembler::CallSize instead of these constants,
914   // as it will choose the correct value for a given relocation mode.
915   //
916   // Without relocation:
917   //  movz  temp, #(target & 0x000000000000ffff)
918   //  movk  temp, #(target & 0x00000000ffff0000)
919   //  movk  temp, #(target & 0x0000ffff00000000)
920   //  blr   temp
921   //
922   // With relocation:
923   //  ldr   temp, =target
924   //  blr   temp
925   static const int kCallSizeWithoutRelocation = 4 * kInstructionSize;
926   static const int kCallSizeWithRelocation = 2 * kInstructionSize;
927
928   // Size of the generated code in bytes
929   uint64_t SizeOfGeneratedCode() const {
930     DCHECK((pc_ >= buffer_) && (pc_ < (buffer_ + buffer_size_)));
931     return pc_ - buffer_;
932   }
933
934   // Return the code size generated from label to the current position.
935   uint64_t SizeOfCodeGeneratedSince(const Label* label) {
936     DCHECK(label->is_bound());
937     DCHECK(pc_offset() >= label->pos());
938     DCHECK(pc_offset() < buffer_size_);
939     return pc_offset() - label->pos();
940   }
941
942   // Check the size of the code generated since the given label. This function
943   // is used primarily to work around comparisons between signed and unsigned
944   // quantities, since V8 uses both.
945   // TODO(jbramley): Work out what sign to use for these things and if possible,
946   // change things to be consistent.
947   void AssertSizeOfCodeGeneratedSince(const Label* label, ptrdiff_t size) {
948     DCHECK(size >= 0);
949     DCHECK(static_cast<uint64_t>(size) == SizeOfCodeGeneratedSince(label));
950   }
951
952   // Return the number of instructions generated from label to the
953   // current position.
954   int InstructionsGeneratedSince(const Label* label) {
955     return SizeOfCodeGeneratedSince(label) / kInstructionSize;
956   }
957
958   // Number of instructions generated for the return sequence in
959   // FullCodeGenerator::EmitReturnSequence.
960   static const int kJSReturnSequenceInstructions = 7;
961   static const int kJSReturnSequenceLength =
962       kJSReturnSequenceInstructions * kInstructionSize;
963   // Distance between start of patched return sequence and the emitted address
964   // to jump to.
965   static const int kPatchReturnSequenceAddressOffset =  0;
966   static const int kPatchDebugBreakSlotAddressOffset =  0;
967
968   // Number of instructions necessary to be able to later patch it to a call.
969   // See DebugCodegen::GenerateSlot() and
970   // BreakLocation::SetDebugBreakAtSlot().
971   static const int kDebugBreakSlotInstructions = 4;
972   static const int kDebugBreakSlotLength =
973     kDebugBreakSlotInstructions * kInstructionSize;
974
975   static const int kPatchDebugBreakSlotReturnOffset = 2 * kInstructionSize;
976
977   // Prevent contant pool emission until EndBlockConstPool is called.
978   // Call to this function can be nested but must be followed by an equal
979   // number of call to EndBlockConstpool.
980   void StartBlockConstPool();
981
982   // Resume constant pool emission. Need to be called as many time as
983   // StartBlockConstPool to have an effect.
984   void EndBlockConstPool();
985
986   bool is_const_pool_blocked() const;
987   static bool IsConstantPoolAt(Instruction* instr);
988   static int ConstantPoolSizeAt(Instruction* instr);
989   // See Assembler::CheckConstPool for more info.
990   void EmitPoolGuard();
991
992   // Prevent veneer pool emission until EndBlockVeneerPool is called.
993   // Call to this function can be nested but must be followed by an equal
994   // number of call to EndBlockConstpool.
995   void StartBlockVeneerPool();
996
997   // Resume constant pool emission. Need to be called as many time as
998   // StartBlockVeneerPool to have an effect.
999   void EndBlockVeneerPool();
1000
1001   bool is_veneer_pool_blocked() const {
1002     return veneer_pool_blocked_nesting_ > 0;
1003   }
1004
1005   // Block/resume emission of constant pools and veneer pools.
1006   void StartBlockPools() {
1007     StartBlockConstPool();
1008     StartBlockVeneerPool();
1009   }
1010   void EndBlockPools() {
1011     EndBlockConstPool();
1012     EndBlockVeneerPool();
1013   }
1014
1015   // Debugging ----------------------------------------------------------------
1016   PositionsRecorder* positions_recorder() { return &positions_recorder_; }
1017   void RecordComment(const char* msg);
1018
1019   // Record a deoptimization reason that can be used by a log or cpu profiler.
1020   // Use --trace-deopt to enable.
1021   void RecordDeoptReason(const int reason, const SourcePosition position);
1022
1023   int buffer_space() const;
1024
1025   // Mark address of the ExitJSFrame code.
1026   void RecordJSReturn();
1027
1028   // Mark address of a debug break slot.
1029   void RecordDebugBreakSlot();
1030
1031   // Record the emission of a constant pool.
1032   //
1033   // The emission of constant and veneer pools depends on the size of the code
1034   // generated and the number of RelocInfo recorded.
1035   // The Debug mechanism needs to map code offsets between two versions of a
1036   // function, compiled with and without debugger support (see for example
1037   // Debug::PrepareForBreakPoints()).
1038   // Compiling functions with debugger support generates additional code
1039   // (DebugCodegen::GenerateSlot()). This may affect the emission of the pools
1040   // and cause the version of the code with debugger support to have pools
1041   // generated in different places.
1042   // Recording the position and size of emitted pools allows to correctly
1043   // compute the offset mappings between the different versions of a function in
1044   // all situations.
1045   //
1046   // The parameter indicates the size of the pool (in bytes), including
1047   // the marker and branch over the data.
1048   void RecordConstPool(int size);
1049
1050
1051   // Instruction set functions ------------------------------------------------
1052
1053   // Branch / Jump instructions.
1054   // For branches offsets are scaled, i.e. they in instrcutions not in bytes.
1055   // Branch to register.
1056   void br(const Register& xn);
1057
1058   // Branch-link to register.
1059   void blr(const Register& xn);
1060
1061   // Branch to register with return hint.
1062   void ret(const Register& xn = lr);
1063
1064   // Unconditional branch to label.
1065   void b(Label* label);
1066
1067   // Conditional branch to label.
1068   void b(Label* label, Condition cond);
1069
1070   // Unconditional branch to PC offset.
1071   void b(int imm26);
1072
1073   // Conditional branch to PC offset.
1074   void b(int imm19, Condition cond);
1075
1076   // Branch-link to label / pc offset.
1077   void bl(Label* label);
1078   void bl(int imm26);
1079
1080   // Compare and branch to label / pc offset if zero.
1081   void cbz(const Register& rt, Label* label);
1082   void cbz(const Register& rt, int imm19);
1083
1084   // Compare and branch to label / pc offset if not zero.
1085   void cbnz(const Register& rt, Label* label);
1086   void cbnz(const Register& rt, int imm19);
1087
1088   // Test bit and branch to label / pc offset if zero.
1089   void tbz(const Register& rt, unsigned bit_pos, Label* label);
1090   void tbz(const Register& rt, unsigned bit_pos, int imm14);
1091
1092   // Test bit and branch to label / pc offset if not zero.
1093   void tbnz(const Register& rt, unsigned bit_pos, Label* label);
1094   void tbnz(const Register& rt, unsigned bit_pos, int imm14);
1095
1096   // Address calculation instructions.
1097   // Calculate a PC-relative address. Unlike for branches the offset in adr is
1098   // unscaled (i.e. the result can be unaligned).
1099   void adr(const Register& rd, Label* label);
1100   void adr(const Register& rd, int imm21);
1101
1102   // Data Processing instructions.
1103   // Add.
1104   void add(const Register& rd,
1105            const Register& rn,
1106            const Operand& operand);
1107
1108   // Add and update status flags.
1109   void adds(const Register& rd,
1110             const Register& rn,
1111             const Operand& operand);
1112
1113   // Compare negative.
1114   void cmn(const Register& rn, const Operand& operand);
1115
1116   // Subtract.
1117   void sub(const Register& rd,
1118            const Register& rn,
1119            const Operand& operand);
1120
1121   // Subtract and update status flags.
1122   void subs(const Register& rd,
1123             const Register& rn,
1124             const Operand& operand);
1125
1126   // Compare.
1127   void cmp(const Register& rn, const Operand& operand);
1128
1129   // Negate.
1130   void neg(const Register& rd,
1131            const Operand& operand);
1132
1133   // Negate and update status flags.
1134   void negs(const Register& rd,
1135             const Operand& operand);
1136
1137   // Add with carry bit.
1138   void adc(const Register& rd,
1139            const Register& rn,
1140            const Operand& operand);
1141
1142   // Add with carry bit and update status flags.
1143   void adcs(const Register& rd,
1144             const Register& rn,
1145             const Operand& operand);
1146
1147   // Subtract with carry bit.
1148   void sbc(const Register& rd,
1149            const Register& rn,
1150            const Operand& operand);
1151
1152   // Subtract with carry bit and update status flags.
1153   void sbcs(const Register& rd,
1154             const Register& rn,
1155             const Operand& operand);
1156
1157   // Negate with carry bit.
1158   void ngc(const Register& rd,
1159            const Operand& operand);
1160
1161   // Negate with carry bit and update status flags.
1162   void ngcs(const Register& rd,
1163             const Operand& operand);
1164
1165   // Logical instructions.
1166   // Bitwise and (A & B).
1167   void and_(const Register& rd,
1168             const Register& rn,
1169             const Operand& operand);
1170
1171   // Bitwise and (A & B) and update status flags.
1172   void ands(const Register& rd,
1173             const Register& rn,
1174             const Operand& operand);
1175
1176   // Bit test, and set flags.
1177   void tst(const Register& rn, const Operand& operand);
1178
1179   // Bit clear (A & ~B).
1180   void bic(const Register& rd,
1181            const Register& rn,
1182            const Operand& operand);
1183
1184   // Bit clear (A & ~B) and update status flags.
1185   void bics(const Register& rd,
1186             const Register& rn,
1187             const Operand& operand);
1188
1189   // Bitwise or (A | B).
1190   void orr(const Register& rd, const Register& rn, const Operand& operand);
1191
1192   // Bitwise nor (A | ~B).
1193   void orn(const Register& rd, const Register& rn, const Operand& operand);
1194
1195   // Bitwise eor/xor (A ^ B).
1196   void eor(const Register& rd, const Register& rn, const Operand& operand);
1197
1198   // Bitwise enor/xnor (A ^ ~B).
1199   void eon(const Register& rd, const Register& rn, const Operand& operand);
1200
1201   // Logical shift left variable.
1202   void lslv(const Register& rd, const Register& rn, const Register& rm);
1203
1204   // Logical shift right variable.
1205   void lsrv(const Register& rd, const Register& rn, const Register& rm);
1206
1207   // Arithmetic shift right variable.
1208   void asrv(const Register& rd, const Register& rn, const Register& rm);
1209
1210   // Rotate right variable.
1211   void rorv(const Register& rd, const Register& rn, const Register& rm);
1212
1213   // Bitfield instructions.
1214   // Bitfield move.
1215   void bfm(const Register& rd,
1216            const Register& rn,
1217            unsigned immr,
1218            unsigned imms);
1219
1220   // Signed bitfield move.
1221   void sbfm(const Register& rd,
1222             const Register& rn,
1223             unsigned immr,
1224             unsigned imms);
1225
1226   // Unsigned bitfield move.
1227   void ubfm(const Register& rd,
1228             const Register& rn,
1229             unsigned immr,
1230             unsigned imms);
1231
1232   // Bfm aliases.
1233   // Bitfield insert.
1234   void bfi(const Register& rd,
1235            const Register& rn,
1236            unsigned lsb,
1237            unsigned width) {
1238     DCHECK(width >= 1);
1239     DCHECK(lsb + width <= rn.SizeInBits());
1240     bfm(rd, rn, (rd.SizeInBits() - lsb) & (rd.SizeInBits() - 1), width - 1);
1241   }
1242
1243   // Bitfield extract and insert low.
1244   void bfxil(const Register& rd,
1245              const Register& rn,
1246              unsigned lsb,
1247              unsigned width) {
1248     DCHECK(width >= 1);
1249     DCHECK(lsb + width <= rn.SizeInBits());
1250     bfm(rd, rn, lsb, lsb + width - 1);
1251   }
1252
1253   // Sbfm aliases.
1254   // Arithmetic shift right.
1255   void asr(const Register& rd, const Register& rn, unsigned shift) {
1256     DCHECK(shift < rd.SizeInBits());
1257     sbfm(rd, rn, shift, rd.SizeInBits() - 1);
1258   }
1259
1260   // Signed bitfield insert in zero.
1261   void sbfiz(const Register& rd,
1262              const Register& rn,
1263              unsigned lsb,
1264              unsigned width) {
1265     DCHECK(width >= 1);
1266     DCHECK(lsb + width <= rn.SizeInBits());
1267     sbfm(rd, rn, (rd.SizeInBits() - lsb) & (rd.SizeInBits() - 1), width - 1);
1268   }
1269
1270   // Signed bitfield extract.
1271   void sbfx(const Register& rd,
1272             const Register& rn,
1273             unsigned lsb,
1274             unsigned width) {
1275     DCHECK(width >= 1);
1276     DCHECK(lsb + width <= rn.SizeInBits());
1277     sbfm(rd, rn, lsb, lsb + width - 1);
1278   }
1279
1280   // Signed extend byte.
1281   void sxtb(const Register& rd, const Register& rn) {
1282     sbfm(rd, rn, 0, 7);
1283   }
1284
1285   // Signed extend halfword.
1286   void sxth(const Register& rd, const Register& rn) {
1287     sbfm(rd, rn, 0, 15);
1288   }
1289
1290   // Signed extend word.
1291   void sxtw(const Register& rd, const Register& rn) {
1292     sbfm(rd, rn, 0, 31);
1293   }
1294
1295   // Ubfm aliases.
1296   // Logical shift left.
1297   void lsl(const Register& rd, const Register& rn, unsigned shift) {
1298     unsigned reg_size = rd.SizeInBits();
1299     DCHECK(shift < reg_size);
1300     ubfm(rd, rn, (reg_size - shift) % reg_size, reg_size - shift - 1);
1301   }
1302
1303   // Logical shift right.
1304   void lsr(const Register& rd, const Register& rn, unsigned shift) {
1305     DCHECK(shift < rd.SizeInBits());
1306     ubfm(rd, rn, shift, rd.SizeInBits() - 1);
1307   }
1308
1309   // Unsigned bitfield insert in zero.
1310   void ubfiz(const Register& rd,
1311              const Register& rn,
1312              unsigned lsb,
1313              unsigned width) {
1314     DCHECK(width >= 1);
1315     DCHECK(lsb + width <= rn.SizeInBits());
1316     ubfm(rd, rn, (rd.SizeInBits() - lsb) & (rd.SizeInBits() - 1), width - 1);
1317   }
1318
1319   // Unsigned bitfield extract.
1320   void ubfx(const Register& rd,
1321             const Register& rn,
1322             unsigned lsb,
1323             unsigned width) {
1324     DCHECK(width >= 1);
1325     DCHECK(lsb + width <= rn.SizeInBits());
1326     ubfm(rd, rn, lsb, lsb + width - 1);
1327   }
1328
1329   // Unsigned extend byte.
1330   void uxtb(const Register& rd, const Register& rn) {
1331     ubfm(rd, rn, 0, 7);
1332   }
1333
1334   // Unsigned extend halfword.
1335   void uxth(const Register& rd, const Register& rn) {
1336     ubfm(rd, rn, 0, 15);
1337   }
1338
1339   // Unsigned extend word.
1340   void uxtw(const Register& rd, const Register& rn) {
1341     ubfm(rd, rn, 0, 31);
1342   }
1343
1344   // Extract.
1345   void extr(const Register& rd,
1346             const Register& rn,
1347             const Register& rm,
1348             unsigned lsb);
1349
1350   // Conditional select: rd = cond ? rn : rm.
1351   void csel(const Register& rd,
1352             const Register& rn,
1353             const Register& rm,
1354             Condition cond);
1355
1356   // Conditional select increment: rd = cond ? rn : rm + 1.
1357   void csinc(const Register& rd,
1358              const Register& rn,
1359              const Register& rm,
1360              Condition cond);
1361
1362   // Conditional select inversion: rd = cond ? rn : ~rm.
1363   void csinv(const Register& rd,
1364              const Register& rn,
1365              const Register& rm,
1366              Condition cond);
1367
1368   // Conditional select negation: rd = cond ? rn : -rm.
1369   void csneg(const Register& rd,
1370              const Register& rn,
1371              const Register& rm,
1372              Condition cond);
1373
1374   // Conditional set: rd = cond ? 1 : 0.
1375   void cset(const Register& rd, Condition cond);
1376
1377   // Conditional set minus: rd = cond ? -1 : 0.
1378   void csetm(const Register& rd, Condition cond);
1379
1380   // Conditional increment: rd = cond ? rn + 1 : rn.
1381   void cinc(const Register& rd, const Register& rn, Condition cond);
1382
1383   // Conditional invert: rd = cond ? ~rn : rn.
1384   void cinv(const Register& rd, const Register& rn, Condition cond);
1385
1386   // Conditional negate: rd = cond ? -rn : rn.
1387   void cneg(const Register& rd, const Register& rn, Condition cond);
1388
1389   // Extr aliases.
1390   void ror(const Register& rd, const Register& rs, unsigned shift) {
1391     extr(rd, rs, rs, shift);
1392   }
1393
1394   // Conditional comparison.
1395   // Conditional compare negative.
1396   void ccmn(const Register& rn,
1397             const Operand& operand,
1398             StatusFlags nzcv,
1399             Condition cond);
1400
1401   // Conditional compare.
1402   void ccmp(const Register& rn,
1403             const Operand& operand,
1404             StatusFlags nzcv,
1405             Condition cond);
1406
1407   // Multiplication.
1408   // 32 x 32 -> 32-bit and 64 x 64 -> 64-bit multiply.
1409   void mul(const Register& rd, const Register& rn, const Register& rm);
1410
1411   // 32 + 32 x 32 -> 32-bit and 64 + 64 x 64 -> 64-bit multiply accumulate.
1412   void madd(const Register& rd,
1413             const Register& rn,
1414             const Register& rm,
1415             const Register& ra);
1416
1417   // -(32 x 32) -> 32-bit and -(64 x 64) -> 64-bit multiply.
1418   void mneg(const Register& rd, const Register& rn, const Register& rm);
1419
1420   // 32 - 32 x 32 -> 32-bit and 64 - 64 x 64 -> 64-bit multiply subtract.
1421   void msub(const Register& rd,
1422             const Register& rn,
1423             const Register& rm,
1424             const Register& ra);
1425
1426   // 32 x 32 -> 64-bit multiply.
1427   void smull(const Register& rd, const Register& rn, const Register& rm);
1428
1429   // Xd = bits<127:64> of Xn * Xm.
1430   void smulh(const Register& rd, const Register& rn, const Register& rm);
1431
1432   // Signed 32 x 32 -> 64-bit multiply and accumulate.
1433   void smaddl(const Register& rd,
1434               const Register& rn,
1435               const Register& rm,
1436               const Register& ra);
1437
1438   // Unsigned 32 x 32 -> 64-bit multiply and accumulate.
1439   void umaddl(const Register& rd,
1440               const Register& rn,
1441               const Register& rm,
1442               const Register& ra);
1443
1444   // Signed 32 x 32 -> 64-bit multiply and subtract.
1445   void smsubl(const Register& rd,
1446               const Register& rn,
1447               const Register& rm,
1448               const Register& ra);
1449
1450   // Unsigned 32 x 32 -> 64-bit multiply and subtract.
1451   void umsubl(const Register& rd,
1452               const Register& rn,
1453               const Register& rm,
1454               const Register& ra);
1455
1456   // Signed integer divide.
1457   void sdiv(const Register& rd, const Register& rn, const Register& rm);
1458
1459   // Unsigned integer divide.
1460   void udiv(const Register& rd, const Register& rn, const Register& rm);
1461
1462   // Bit count, bit reverse and endian reverse.
1463   void rbit(const Register& rd, const Register& rn);
1464   void rev16(const Register& rd, const Register& rn);
1465   void rev32(const Register& rd, const Register& rn);
1466   void rev(const Register& rd, const Register& rn);
1467   void clz(const Register& rd, const Register& rn);
1468   void cls(const Register& rd, const Register& rn);
1469
1470   // Memory instructions.
1471
1472   // Load integer or FP register.
1473   void ldr(const CPURegister& rt, const MemOperand& src);
1474
1475   // Store integer or FP register.
1476   void str(const CPURegister& rt, const MemOperand& dst);
1477
1478   // Load word with sign extension.
1479   void ldrsw(const Register& rt, const MemOperand& src);
1480
1481   // Load byte.
1482   void ldrb(const Register& rt, const MemOperand& src);
1483
1484   // Store byte.
1485   void strb(const Register& rt, const MemOperand& dst);
1486
1487   // Load byte with sign extension.
1488   void ldrsb(const Register& rt, const MemOperand& src);
1489
1490   // Load half-word.
1491   void ldrh(const Register& rt, const MemOperand& src);
1492
1493   // Store half-word.
1494   void strh(const Register& rt, const MemOperand& dst);
1495
1496   // Load half-word with sign extension.
1497   void ldrsh(const Register& rt, const MemOperand& src);
1498
1499   // Load integer or FP register pair.
1500   void ldp(const CPURegister& rt, const CPURegister& rt2,
1501            const MemOperand& src);
1502
1503   // Store integer or FP register pair.
1504   void stp(const CPURegister& rt, const CPURegister& rt2,
1505            const MemOperand& dst);
1506
1507   // Load word pair with sign extension.
1508   void ldpsw(const Register& rt, const Register& rt2, const MemOperand& src);
1509
1510   // Load integer or FP register pair, non-temporal.
1511   void ldnp(const CPURegister& rt, const CPURegister& rt2,
1512             const MemOperand& src);
1513
1514   // Store integer or FP register pair, non-temporal.
1515   void stnp(const CPURegister& rt, const CPURegister& rt2,
1516             const MemOperand& dst);
1517
1518   // Load literal to register from a pc relative address.
1519   void ldr_pcrel(const CPURegister& rt, int imm19);
1520
1521   // Load literal to register.
1522   void ldr(const CPURegister& rt, const Immediate& imm);
1523
1524   // Move instructions. The default shift of -1 indicates that the move
1525   // instruction will calculate an appropriate 16-bit immediate and left shift
1526   // that is equal to the 64-bit immediate argument. If an explicit left shift
1527   // is specified (0, 16, 32 or 48), the immediate must be a 16-bit value.
1528   //
1529   // For movk, an explicit shift can be used to indicate which half word should
1530   // be overwritten, eg. movk(x0, 0, 0) will overwrite the least-significant
1531   // half word with zero, whereas movk(x0, 0, 48) will overwrite the
1532   // most-significant.
1533
1534   // Move and keep.
1535   void movk(const Register& rd, uint64_t imm, int shift = -1) {
1536     MoveWide(rd, imm, shift, MOVK);
1537   }
1538
1539   // Move with non-zero.
1540   void movn(const Register& rd, uint64_t imm, int shift = -1) {
1541     MoveWide(rd, imm, shift, MOVN);
1542   }
1543
1544   // Move with zero.
1545   void movz(const Register& rd, uint64_t imm, int shift = -1) {
1546     MoveWide(rd, imm, shift, MOVZ);
1547   }
1548
1549   // Misc instructions.
1550   // Monitor debug-mode breakpoint.
1551   void brk(int code);
1552
1553   // Halting debug-mode breakpoint.
1554   void hlt(int code);
1555
1556   // Move register to register.
1557   void mov(const Register& rd, const Register& rn);
1558
1559   // Move NOT(operand) to register.
1560   void mvn(const Register& rd, const Operand& operand);
1561
1562   // System instructions.
1563   // Move to register from system register.
1564   void mrs(const Register& rt, SystemRegister sysreg);
1565
1566   // Move from register to system register.
1567   void msr(SystemRegister sysreg, const Register& rt);
1568
1569   // System hint.
1570   void hint(SystemHint code);
1571
1572   // Data memory barrier
1573   void dmb(BarrierDomain domain, BarrierType type);
1574
1575   // Data synchronization barrier
1576   void dsb(BarrierDomain domain, BarrierType type);
1577
1578   // Instruction synchronization barrier
1579   void isb();
1580
1581   // Alias for system instructions.
1582   void nop() { hint(NOP); }
1583
1584   // Different nop operations are used by the code generator to detect certain
1585   // states of the generated code.
1586   enum NopMarkerTypes {
1587     DEBUG_BREAK_NOP,
1588     INTERRUPT_CODE_NOP,
1589     ADR_FAR_NOP,
1590     FIRST_NOP_MARKER = DEBUG_BREAK_NOP,
1591     LAST_NOP_MARKER = ADR_FAR_NOP
1592   };
1593
1594   void nop(NopMarkerTypes n) {
1595     DCHECK((FIRST_NOP_MARKER <= n) && (n <= LAST_NOP_MARKER));
1596     mov(Register::XRegFromCode(n), Register::XRegFromCode(n));
1597   }
1598
1599   // FP instructions.
1600   // Move immediate to FP register.
1601   void fmov(FPRegister fd, double imm);
1602   void fmov(FPRegister fd, float imm);
1603
1604   // Move FP register to register.
1605   void fmov(Register rd, FPRegister fn);
1606
1607   // Move register to FP register.
1608   void fmov(FPRegister fd, Register rn);
1609
1610   // Move FP register to FP register.
1611   void fmov(FPRegister fd, FPRegister fn);
1612
1613   // FP add.
1614   void fadd(const FPRegister& fd, const FPRegister& fn, const FPRegister& fm);
1615
1616   // FP subtract.
1617   void fsub(const FPRegister& fd, const FPRegister& fn, const FPRegister& fm);
1618
1619   // FP multiply.
1620   void fmul(const FPRegister& fd, const FPRegister& fn, const FPRegister& fm);
1621
1622   // FP fused multiply and add.
1623   void fmadd(const FPRegister& fd,
1624              const FPRegister& fn,
1625              const FPRegister& fm,
1626              const FPRegister& fa);
1627
1628   // FP fused multiply and subtract.
1629   void fmsub(const FPRegister& fd,
1630              const FPRegister& fn,
1631              const FPRegister& fm,
1632              const FPRegister& fa);
1633
1634   // FP fused multiply, add and negate.
1635   void fnmadd(const FPRegister& fd,
1636               const FPRegister& fn,
1637               const FPRegister& fm,
1638               const FPRegister& fa);
1639
1640   // FP fused multiply, subtract and negate.
1641   void fnmsub(const FPRegister& fd,
1642               const FPRegister& fn,
1643               const FPRegister& fm,
1644               const FPRegister& fa);
1645
1646   // FP divide.
1647   void fdiv(const FPRegister& fd, const FPRegister& fn, const FPRegister& fm);
1648
1649   // FP maximum.
1650   void fmax(const FPRegister& fd, const FPRegister& fn, const FPRegister& fm);
1651
1652   // FP minimum.
1653   void fmin(const FPRegister& fd, const FPRegister& fn, const FPRegister& fm);
1654
1655   // FP maximum.
1656   void fmaxnm(const FPRegister& fd, const FPRegister& fn, const FPRegister& fm);
1657
1658   // FP minimum.
1659   void fminnm(const FPRegister& fd, const FPRegister& fn, const FPRegister& fm);
1660
1661   // FP absolute.
1662   void fabs(const FPRegister& fd, const FPRegister& fn);
1663
1664   // FP negate.
1665   void fneg(const FPRegister& fd, const FPRegister& fn);
1666
1667   // FP square root.
1668   void fsqrt(const FPRegister& fd, const FPRegister& fn);
1669
1670   // FP round to integer (nearest with ties to away).
1671   void frinta(const FPRegister& fd, const FPRegister& fn);
1672
1673   // FP round to integer (toward minus infinity).
1674   void frintm(const FPRegister& fd, const FPRegister& fn);
1675
1676   // FP round to integer (nearest with ties to even).
1677   void frintn(const FPRegister& fd, const FPRegister& fn);
1678
1679   // FP round to integer (towards plus infinity).
1680   void frintp(const FPRegister& fd, const FPRegister& fn);
1681
1682   // FP round to integer (towards zero.)
1683   void frintz(const FPRegister& fd, const FPRegister& fn);
1684
1685   // FP compare registers.
1686   void fcmp(const FPRegister& fn, const FPRegister& fm);
1687
1688   // FP compare immediate.
1689   void fcmp(const FPRegister& fn, double value);
1690
1691   // FP conditional compare.
1692   void fccmp(const FPRegister& fn,
1693              const FPRegister& fm,
1694              StatusFlags nzcv,
1695              Condition cond);
1696
1697   // FP conditional select.
1698   void fcsel(const FPRegister& fd,
1699              const FPRegister& fn,
1700              const FPRegister& fm,
1701              Condition cond);
1702
1703   // Common FP Convert function
1704   void FPConvertToInt(const Register& rd,
1705                       const FPRegister& fn,
1706                       FPIntegerConvertOp op);
1707
1708   // FP convert between single and double precision.
1709   void fcvt(const FPRegister& fd, const FPRegister& fn);
1710
1711   // Convert FP to unsigned integer (nearest with ties to away).
1712   void fcvtau(const Register& rd, const FPRegister& fn);
1713
1714   // Convert FP to signed integer (nearest with ties to away).
1715   void fcvtas(const Register& rd, const FPRegister& fn);
1716
1717   // Convert FP to unsigned integer (round towards -infinity).
1718   void fcvtmu(const Register& rd, const FPRegister& fn);
1719
1720   // Convert FP to signed integer (round towards -infinity).
1721   void fcvtms(const Register& rd, const FPRegister& fn);
1722
1723   // Convert FP to unsigned integer (nearest with ties to even).
1724   void fcvtnu(const Register& rd, const FPRegister& fn);
1725
1726   // Convert FP to signed integer (nearest with ties to even).
1727   void fcvtns(const Register& rd, const FPRegister& fn);
1728
1729   // Convert FP to unsigned integer (round towards zero).
1730   void fcvtzu(const Register& rd, const FPRegister& fn);
1731
1732   // Convert FP to signed integer (rounf towards zero).
1733   void fcvtzs(const Register& rd, const FPRegister& fn);
1734
1735   // Convert signed integer or fixed point to FP.
1736   void scvtf(const FPRegister& fd, const Register& rn, unsigned fbits = 0);
1737
1738   // Convert unsigned integer or fixed point to FP.
1739   void ucvtf(const FPRegister& fd, const Register& rn, unsigned fbits = 0);
1740
1741   // Instruction functions used only for test, debug, and patching.
1742   // Emit raw instructions in the instruction stream.
1743   void dci(Instr raw_inst) { Emit(raw_inst); }
1744
1745   // Emit 8 bits of data in the instruction stream.
1746   void dc8(uint8_t data) { EmitData(&data, sizeof(data)); }
1747
1748   // Emit 32 bits of data in the instruction stream.
1749   void dc32(uint32_t data) { EmitData(&data, sizeof(data)); }
1750
1751   // Emit 64 bits of data in the instruction stream.
1752   void dc64(uint64_t data) { EmitData(&data, sizeof(data)); }
1753
1754   // Emit an address in the instruction stream.
1755   void dcptr(Label* label);
1756
1757   // Copy a string into the instruction stream, including the terminating NULL
1758   // character. The instruction pointer (pc_) is then aligned correctly for
1759   // subsequent instructions.
1760   void EmitStringData(const char* string);
1761
1762   // Pseudo-instructions ------------------------------------------------------
1763
1764   // Parameters are described in arm64/instructions-arm64.h.
1765   void debug(const char* message, uint32_t code, Instr params = BREAK);
1766
1767   // Required by V8.
1768   void dd(uint32_t data) { dc32(data); }
1769   void db(uint8_t data) { dc8(data); }
1770
1771   // Code generation helpers --------------------------------------------------
1772
1773   bool IsConstPoolEmpty() const { return constpool_.IsEmpty(); }
1774
1775   Instruction* pc() const { return Instruction::Cast(pc_); }
1776
1777   Instruction* InstructionAt(int offset) const {
1778     return reinterpret_cast<Instruction*>(buffer_ + offset);
1779   }
1780
1781   ptrdiff_t InstructionOffset(Instruction* instr) const {
1782     return reinterpret_cast<byte*>(instr) - buffer_;
1783   }
1784
1785   // Register encoding.
1786   static Instr Rd(CPURegister rd) {
1787     DCHECK(rd.code() != kSPRegInternalCode);
1788     return rd.code() << Rd_offset;
1789   }
1790
1791   static Instr Rn(CPURegister rn) {
1792     DCHECK(rn.code() != kSPRegInternalCode);
1793     return rn.code() << Rn_offset;
1794   }
1795
1796   static Instr Rm(CPURegister rm) {
1797     DCHECK(rm.code() != kSPRegInternalCode);
1798     return rm.code() << Rm_offset;
1799   }
1800
1801   static Instr Ra(CPURegister ra) {
1802     DCHECK(ra.code() != kSPRegInternalCode);
1803     return ra.code() << Ra_offset;
1804   }
1805
1806   static Instr Rt(CPURegister rt) {
1807     DCHECK(rt.code() != kSPRegInternalCode);
1808     return rt.code() << Rt_offset;
1809   }
1810
1811   static Instr Rt2(CPURegister rt2) {
1812     DCHECK(rt2.code() != kSPRegInternalCode);
1813     return rt2.code() << Rt2_offset;
1814   }
1815
1816   // These encoding functions allow the stack pointer to be encoded, and
1817   // disallow the zero register.
1818   static Instr RdSP(Register rd) {
1819     DCHECK(!rd.IsZero());
1820     return (rd.code() & kRegCodeMask) << Rd_offset;
1821   }
1822
1823   static Instr RnSP(Register rn) {
1824     DCHECK(!rn.IsZero());
1825     return (rn.code() & kRegCodeMask) << Rn_offset;
1826   }
1827
1828   // Flags encoding.
1829   inline static Instr Flags(FlagsUpdate S);
1830   inline static Instr Cond(Condition cond);
1831
1832   // PC-relative address encoding.
1833   inline static Instr ImmPCRelAddress(int imm21);
1834
1835   // Branch encoding.
1836   inline static Instr ImmUncondBranch(int imm26);
1837   inline static Instr ImmCondBranch(int imm19);
1838   inline static Instr ImmCmpBranch(int imm19);
1839   inline static Instr ImmTestBranch(int imm14);
1840   inline static Instr ImmTestBranchBit(unsigned bit_pos);
1841
1842   // Data Processing encoding.
1843   inline static Instr SF(Register rd);
1844   inline static Instr ImmAddSub(int64_t imm);
1845   inline static Instr ImmS(unsigned imms, unsigned reg_size);
1846   inline static Instr ImmR(unsigned immr, unsigned reg_size);
1847   inline static Instr ImmSetBits(unsigned imms, unsigned reg_size);
1848   inline static Instr ImmRotate(unsigned immr, unsigned reg_size);
1849   inline static Instr ImmLLiteral(int imm19);
1850   inline static Instr BitN(unsigned bitn, unsigned reg_size);
1851   inline static Instr ShiftDP(Shift shift);
1852   inline static Instr ImmDPShift(unsigned amount);
1853   inline static Instr ExtendMode(Extend extend);
1854   inline static Instr ImmExtendShift(unsigned left_shift);
1855   inline static Instr ImmCondCmp(unsigned imm);
1856   inline static Instr Nzcv(StatusFlags nzcv);
1857
1858   static bool IsImmAddSub(int64_t immediate);
1859   static bool IsImmLogical(uint64_t value,
1860                            unsigned width,
1861                            unsigned* n,
1862                            unsigned* imm_s,
1863                            unsigned* imm_r);
1864
1865   // MemOperand offset encoding.
1866   inline static Instr ImmLSUnsigned(int imm12);
1867   inline static Instr ImmLS(int imm9);
1868   inline static Instr ImmLSPair(int imm7, LSDataSize size);
1869   inline static Instr ImmShiftLS(unsigned shift_amount);
1870   inline static Instr ImmException(int imm16);
1871   inline static Instr ImmSystemRegister(int imm15);
1872   inline static Instr ImmHint(int imm7);
1873   inline static Instr ImmBarrierDomain(int imm2);
1874   inline static Instr ImmBarrierType(int imm2);
1875   inline static LSDataSize CalcLSDataSize(LoadStoreOp op);
1876
1877   static bool IsImmLSUnscaled(int64_t offset);
1878   static bool IsImmLSScaled(int64_t offset, LSDataSize size);
1879
1880   // Move immediates encoding.
1881   inline static Instr ImmMoveWide(uint64_t imm);
1882   inline static Instr ShiftMoveWide(int64_t shift);
1883
1884   // FP Immediates.
1885   static Instr ImmFP32(float imm);
1886   static Instr ImmFP64(double imm);
1887   inline static Instr FPScale(unsigned scale);
1888
1889   // FP register type.
1890   inline static Instr FPType(FPRegister fd);
1891
1892   // Class for scoping postponing the constant pool generation.
1893   class BlockConstPoolScope {
1894    public:
1895     explicit BlockConstPoolScope(Assembler* assem) : assem_(assem) {
1896       assem_->StartBlockConstPool();
1897     }
1898     ~BlockConstPoolScope() {
1899       assem_->EndBlockConstPool();
1900     }
1901
1902    private:
1903     Assembler* assem_;
1904
1905     DISALLOW_IMPLICIT_CONSTRUCTORS(BlockConstPoolScope);
1906   };
1907
1908   // Check if is time to emit a constant pool.
1909   void CheckConstPool(bool force_emit, bool require_jump);
1910
1911   // Allocate a constant pool of the correct size for the generated code.
1912   Handle<ConstantPoolArray> NewConstantPool(Isolate* isolate);
1913
1914   // Generate the constant pool for the generated code.
1915   void PopulateConstantPool(ConstantPoolArray* constant_pool);
1916
1917   // Returns true if we should emit a veneer as soon as possible for a branch
1918   // which can at most reach to specified pc.
1919   bool ShouldEmitVeneer(int max_reachable_pc,
1920                         int margin = kVeneerDistanceMargin);
1921   bool ShouldEmitVeneers(int margin = kVeneerDistanceMargin) {
1922     return ShouldEmitVeneer(unresolved_branches_first_limit(), margin);
1923   }
1924
1925   // The maximum code size generated for a veneer. Currently one branch
1926   // instruction. This is for code size checking purposes, and can be extended
1927   // in the future for example if we decide to add nops between the veneers.
1928   static const int kMaxVeneerCodeSize = 1 * kInstructionSize;
1929
1930   void RecordVeneerPool(int location_offset, int size);
1931   // Emits veneers for branches that are approaching their maximum range.
1932   // If need_protection is true, the veneers are protected by a branch jumping
1933   // over the code.
1934   void EmitVeneers(bool force_emit, bool need_protection,
1935                    int margin = kVeneerDistanceMargin);
1936   void EmitVeneersGuard() { EmitPoolGuard(); }
1937   // Checks whether veneers need to be emitted at this point.
1938   // If force_emit is set, a veneer is generated for *all* unresolved branches.
1939   void CheckVeneerPool(bool force_emit, bool require_jump,
1940                        int margin = kVeneerDistanceMargin);
1941
1942   class BlockPoolsScope {
1943    public:
1944     explicit BlockPoolsScope(Assembler* assem) : assem_(assem) {
1945       assem_->StartBlockPools();
1946     }
1947     ~BlockPoolsScope() {
1948       assem_->EndBlockPools();
1949     }
1950
1951    private:
1952     Assembler* assem_;
1953
1954     DISALLOW_IMPLICIT_CONSTRUCTORS(BlockPoolsScope);
1955   };
1956
1957  protected:
1958   inline const Register& AppropriateZeroRegFor(const CPURegister& reg) const;
1959
1960   void LoadStore(const CPURegister& rt,
1961                  const MemOperand& addr,
1962                  LoadStoreOp op);
1963
1964   void LoadStorePair(const CPURegister& rt, const CPURegister& rt2,
1965                      const MemOperand& addr, LoadStorePairOp op);
1966   static bool IsImmLSPair(int64_t offset, LSDataSize size);
1967
1968   void Logical(const Register& rd,
1969                const Register& rn,
1970                const Operand& operand,
1971                LogicalOp op);
1972   void LogicalImmediate(const Register& rd,
1973                         const Register& rn,
1974                         unsigned n,
1975                         unsigned imm_s,
1976                         unsigned imm_r,
1977                         LogicalOp op);
1978
1979   void ConditionalCompare(const Register& rn,
1980                           const Operand& operand,
1981                           StatusFlags nzcv,
1982                           Condition cond,
1983                           ConditionalCompareOp op);
1984   static bool IsImmConditionalCompare(int64_t immediate);
1985
1986   void AddSubWithCarry(const Register& rd,
1987                        const Register& rn,
1988                        const Operand& operand,
1989                        FlagsUpdate S,
1990                        AddSubWithCarryOp op);
1991
1992   // Functions for emulating operands not directly supported by the instruction
1993   // set.
1994   void EmitShift(const Register& rd,
1995                  const Register& rn,
1996                  Shift shift,
1997                  unsigned amount);
1998   void EmitExtendShift(const Register& rd,
1999                        const Register& rn,
2000                        Extend extend,
2001                        unsigned left_shift);
2002
2003   void AddSub(const Register& rd,
2004               const Register& rn,
2005               const Operand& operand,
2006               FlagsUpdate S,
2007               AddSubOp op);
2008
2009   static bool IsImmFP32(float imm);
2010   static bool IsImmFP64(double imm);
2011
2012   // Find an appropriate LoadStoreOp or LoadStorePairOp for the specified
2013   // registers. Only simple loads are supported; sign- and zero-extension (such
2014   // as in LDPSW_x or LDRB_w) are not supported.
2015   static inline LoadStoreOp LoadOpFor(const CPURegister& rt);
2016   static inline LoadStorePairOp LoadPairOpFor(const CPURegister& rt,
2017                                               const CPURegister& rt2);
2018   static inline LoadStoreOp StoreOpFor(const CPURegister& rt);
2019   static inline LoadStorePairOp StorePairOpFor(const CPURegister& rt,
2020                                                const CPURegister& rt2);
2021   static inline LoadStorePairNonTemporalOp LoadPairNonTemporalOpFor(
2022     const CPURegister& rt, const CPURegister& rt2);
2023   static inline LoadStorePairNonTemporalOp StorePairNonTemporalOpFor(
2024     const CPURegister& rt, const CPURegister& rt2);
2025   static inline LoadLiteralOp LoadLiteralOpFor(const CPURegister& rt);
2026
2027   // Remove the specified branch from the unbound label link chain.
2028   // If available, a veneer for this label can be used for other branches in the
2029   // chain if the link chain cannot be fixed up without this branch.
2030   void RemoveBranchFromLabelLinkChain(Instruction* branch,
2031                                       Label* label,
2032                                       Instruction* label_veneer = NULL);
2033
2034  private:
2035   // Instruction helpers.
2036   void MoveWide(const Register& rd,
2037                 uint64_t imm,
2038                 int shift,
2039                 MoveWideImmediateOp mov_op);
2040   void DataProcShiftedRegister(const Register& rd,
2041                                const Register& rn,
2042                                const Operand& operand,
2043                                FlagsUpdate S,
2044                                Instr op);
2045   void DataProcExtendedRegister(const Register& rd,
2046                                 const Register& rn,
2047                                 const Operand& operand,
2048                                 FlagsUpdate S,
2049                                 Instr op);
2050   void LoadStorePairNonTemporal(const CPURegister& rt,
2051                                 const CPURegister& rt2,
2052                                 const MemOperand& addr,
2053                                 LoadStorePairNonTemporalOp op);
2054   void ConditionalSelect(const Register& rd,
2055                          const Register& rn,
2056                          const Register& rm,
2057                          Condition cond,
2058                          ConditionalSelectOp op);
2059   void DataProcessing1Source(const Register& rd,
2060                              const Register& rn,
2061                              DataProcessing1SourceOp op);
2062   void DataProcessing3Source(const Register& rd,
2063                              const Register& rn,
2064                              const Register& rm,
2065                              const Register& ra,
2066                              DataProcessing3SourceOp op);
2067   void FPDataProcessing1Source(const FPRegister& fd,
2068                                const FPRegister& fn,
2069                                FPDataProcessing1SourceOp op);
2070   void FPDataProcessing2Source(const FPRegister& fd,
2071                                const FPRegister& fn,
2072                                const FPRegister& fm,
2073                                FPDataProcessing2SourceOp op);
2074   void FPDataProcessing3Source(const FPRegister& fd,
2075                                const FPRegister& fn,
2076                                const FPRegister& fm,
2077                                const FPRegister& fa,
2078                                FPDataProcessing3SourceOp op);
2079
2080   // Label helpers.
2081
2082   // Return an offset for a label-referencing instruction, typically a branch.
2083   int LinkAndGetByteOffsetTo(Label* label);
2084
2085   // This is the same as LinkAndGetByteOffsetTo, but return an offset
2086   // suitable for fields that take instruction offsets.
2087   inline int LinkAndGetInstructionOffsetTo(Label* label);
2088
2089   static const int kStartOfLabelLinkChain = 0;
2090
2091   // Verify that a label's link chain is intact.
2092   void CheckLabelLinkChain(Label const * label);
2093
2094   void RecordLiteral(int64_t imm, unsigned size);
2095
2096   // Postpone the generation of the constant pool for the specified number of
2097   // instructions.
2098   void BlockConstPoolFor(int instructions);
2099
2100   // Set how far from current pc the next constant pool check will be.
2101   void SetNextConstPoolCheckIn(int instructions) {
2102     next_constant_pool_check_ = pc_offset() + instructions * kInstructionSize;
2103   }
2104
2105   // Emit the instruction at pc_.
2106   void Emit(Instr instruction) {
2107     STATIC_ASSERT(sizeof(*pc_) == 1);
2108     STATIC_ASSERT(sizeof(instruction) == kInstructionSize);
2109     DCHECK((pc_ + sizeof(instruction)) <= (buffer_ + buffer_size_));
2110
2111     memcpy(pc_, &instruction, sizeof(instruction));
2112     pc_ += sizeof(instruction);
2113     CheckBuffer();
2114   }
2115
2116   // Emit data inline in the instruction stream.
2117   void EmitData(void const * data, unsigned size) {
2118     DCHECK(sizeof(*pc_) == 1);
2119     DCHECK((pc_ + size) <= (buffer_ + buffer_size_));
2120
2121     // TODO(all): Somehow register we have some data here. Then we can
2122     // disassemble it correctly.
2123     memcpy(pc_, data, size);
2124     pc_ += size;
2125     CheckBuffer();
2126   }
2127
2128   void GrowBuffer();
2129   void CheckBufferSpace();
2130   void CheckBuffer();
2131
2132   // Pc offset of the next constant pool check.
2133   int next_constant_pool_check_;
2134
2135   // Constant pool generation
2136   // Pools are emitted in the instruction stream. They are emitted when:
2137   //  * the distance to the first use is above a pre-defined distance or
2138   //  * the numbers of entries in the pool is above a pre-defined size or
2139   //  * code generation is finished
2140   // If a pool needs to be emitted before code generation is finished a branch
2141   // over the emitted pool will be inserted.
2142
2143   // Constants in the pool may be addresses of functions that gets relocated;
2144   // if so, a relocation info entry is associated to the constant pool entry.
2145
2146   // Repeated checking whether the constant pool should be emitted is rather
2147   // expensive. By default we only check again once a number of instructions
2148   // has been generated. That also means that the sizing of the buffers is not
2149   // an exact science, and that we rely on some slop to not overrun buffers.
2150   static const int kCheckConstPoolInterval = 128;
2151
2152   // Distance to first use after a which a pool will be emitted. Pool entries
2153   // are accessed with pc relative load therefore this cannot be more than
2154   // 1 * MB. Since constant pool emission checks are interval based this value
2155   // is an approximation.
2156   static const int kApproxMaxDistToConstPool = 64 * KB;
2157
2158   // Number of pool entries after which a pool will be emitted. Since constant
2159   // pool emission checks are interval based this value is an approximation.
2160   static const int kApproxMaxPoolEntryCount = 512;
2161
2162   // Emission of the constant pool may be blocked in some code sequences.
2163   int const_pool_blocked_nesting_;  // Block emission if this is not zero.
2164   int no_const_pool_before_;  // Block emission before this pc offset.
2165
2166   // Emission of the veneer pools may be blocked in some code sequences.
2167   int veneer_pool_blocked_nesting_;  // Block emission if this is not zero.
2168
2169   // Relocation info generation
2170   // Each relocation is encoded as a variable size value
2171   static const int kMaxRelocSize = RelocInfoWriter::kMaxSize;
2172   RelocInfoWriter reloc_info_writer;
2173   // Internal reference positions, required for (potential) patching in
2174   // GrowBuffer(); contains only those internal references whose labels
2175   // are already bound.
2176   std::deque<int> internal_reference_positions_;
2177
2178   // Relocation info records are also used during code generation as temporary
2179   // containers for constants and code target addresses until they are emitted
2180   // to the constant pool. These pending relocation info records are temporarily
2181   // stored in a separate buffer until a constant pool is emitted.
2182   // If every instruction in a long sequence is accessing the pool, we need one
2183   // pending relocation entry per instruction.
2184
2185   // The pending constant pool.
2186   ConstPool constpool_;
2187
2188   // Relocation for a type-recording IC has the AST id added to it.  This
2189   // member variable is a way to pass the information from the call site to
2190   // the relocation info.
2191   TypeFeedbackId recorded_ast_id_;
2192
2193   inline TypeFeedbackId RecordedAstId();
2194   inline void ClearRecordedAstId();
2195
2196  protected:
2197   // Record the AST id of the CallIC being compiled, so that it can be placed
2198   // in the relocation information.
2199   void SetRecordedAstId(TypeFeedbackId ast_id) {
2200     DCHECK(recorded_ast_id_.IsNone());
2201     recorded_ast_id_ = ast_id;
2202   }
2203
2204   // Code generation
2205   // The relocation writer's position is at least kGap bytes below the end of
2206   // the generated instructions. This is so that multi-instruction sequences do
2207   // not have to check for overflow. The same is true for writes of large
2208   // relocation info entries, and debug strings encoded in the instruction
2209   // stream.
2210   static const int kGap = 128;
2211
2212  public:
2213   class FarBranchInfo {
2214    public:
2215     FarBranchInfo(int offset, Label* label)
2216         : pc_offset_(offset), label_(label) {}
2217     // Offset of the branch in the code generation buffer.
2218     int pc_offset_;
2219     // The label branched to.
2220     Label* label_;
2221   };
2222
2223  protected:
2224   // Information about unresolved (forward) branches.
2225   // The Assembler is only allowed to delete out-of-date information from here
2226   // after a label is bound. The MacroAssembler uses this information to
2227   // generate veneers.
2228   //
2229   // The second member gives information about the unresolved branch. The first
2230   // member of the pair is the maximum offset that the branch can reach in the
2231   // buffer. The map is sorted according to this reachable offset, allowing to
2232   // easily check when veneers need to be emitted.
2233   // Note that the maximum reachable offset (first member of the pairs) should
2234   // always be positive but has the same type as the return value for
2235   // pc_offset() for convenience.
2236   std::multimap<int, FarBranchInfo> unresolved_branches_;
2237
2238   // We generate a veneer for a branch if we reach within this distance of the
2239   // limit of the range.
2240   static const int kVeneerDistanceMargin = 1 * KB;
2241   // The factor of 2 is a finger in the air guess. With a default margin of
2242   // 1KB, that leaves us an addional 256 instructions to avoid generating a
2243   // protective branch.
2244   static const int kVeneerNoProtectionFactor = 2;
2245   static const int kVeneerDistanceCheckMargin =
2246     kVeneerNoProtectionFactor * kVeneerDistanceMargin;
2247   int unresolved_branches_first_limit() const {
2248     DCHECK(!unresolved_branches_.empty());
2249     return unresolved_branches_.begin()->first;
2250   }
2251   // This is similar to next_constant_pool_check_ and helps reduce the overhead
2252   // of checking for veneer pools.
2253   // It is maintained to the closest unresolved branch limit minus the maximum
2254   // veneer margin (or kMaxInt if there are no unresolved branches).
2255   int next_veneer_pool_check_;
2256
2257  private:
2258   // If a veneer is emitted for a branch instruction, that instruction must be
2259   // removed from the associated label's link chain so that the assembler does
2260   // not later attempt (likely unsuccessfully) to patch it to branch directly to
2261   // the label.
2262   void DeleteUnresolvedBranchInfoForLabel(Label* label);
2263   // This function deletes the information related to the label by traversing
2264   // the label chain, and for each PC-relative instruction in the chain checking
2265   // if pending unresolved information exists. Its complexity is proportional to
2266   // the length of the label chain.
2267   void DeleteUnresolvedBranchInfoForLabelTraverse(Label* label);
2268
2269  private:
2270   PositionsRecorder positions_recorder_;
2271   friend class PositionsRecorder;
2272   friend class EnsureSpace;
2273   friend class ConstPool;
2274 };
2275
2276 class PatchingAssembler : public Assembler {
2277  public:
2278   // Create an Assembler with a buffer starting at 'start'.
2279   // The buffer size is
2280   //   size of instructions to patch + kGap
2281   // Where kGap is the distance from which the Assembler tries to grow the
2282   // buffer.
2283   // If more or fewer instructions than expected are generated or if some
2284   // relocation information takes space in the buffer, the PatchingAssembler
2285   // will crash trying to grow the buffer.
2286   PatchingAssembler(Instruction* start, unsigned count)
2287     : Assembler(NULL,
2288                 reinterpret_cast<byte*>(start),
2289                 count * kInstructionSize + kGap) {
2290     StartBlockPools();
2291   }
2292
2293   PatchingAssembler(byte* start, unsigned count)
2294     : Assembler(NULL, start, count * kInstructionSize + kGap) {
2295     // Block constant pool emission.
2296     StartBlockPools();
2297   }
2298
2299   ~PatchingAssembler() {
2300     // Const pool should still be blocked.
2301     DCHECK(is_const_pool_blocked());
2302     EndBlockPools();
2303     // Verify we have generated the number of instruction we expected.
2304     DCHECK((pc_offset() + kGap) == buffer_size_);
2305     // Verify no relocation information has been emitted.
2306     DCHECK(IsConstPoolEmpty());
2307     // Flush the Instruction cache.
2308     size_t length = buffer_size_ - kGap;
2309     CpuFeatures::FlushICache(buffer_, length);
2310   }
2311
2312   // See definition of PatchAdrFar() for details.
2313   static const int kAdrFarPatchableNNops = 2;
2314   static const int kAdrFarPatchableNInstrs = kAdrFarPatchableNNops + 2;
2315   void PatchAdrFar(int64_t target_offset);
2316 };
2317
2318
2319 class EnsureSpace BASE_EMBEDDED {
2320  public:
2321   explicit EnsureSpace(Assembler* assembler) {
2322     assembler->CheckBufferSpace();
2323   }
2324 };
2325
2326 } }  // namespace v8::internal
2327
2328 #endif  // V8_ARM64_ASSEMBLER_ARM64_H_