1 // Copyright 2015, ARM Limited
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //   * Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //   * Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //   * Neither the name of ARM Limited nor the names of its contributors may be
13 //     used to endorse or promote products derived from this software without
14 //     specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef VIXL_A64_INSTRUCTIONS_A64_H_
28 #define VIXL_A64_INSTRUCTIONS_A64_H_
29 
30 #include "vixl/globals.h"
31 #include "vixl/utils.h"
32 #include "vixl/a64/constants-a64.h"
33 
34 namespace vixl {
35 // ISA constants. --------------------------------------------------------------
36 
37 typedef uint32_t Instr;
38 const unsigned kInstructionSize = 4;
39 const unsigned kInstructionSizeLog2 = 2;
40 const unsigned kLiteralEntrySize = 4;
41 const unsigned kLiteralEntrySizeLog2 = 2;
42 const unsigned kMaxLoadLiteralRange = 1 * MBytes;
43 
44 // This is the nominal page size (as used by the adrp instruction); the actual
45 // size of the memory pages allocated by the kernel is likely to differ.
46 const unsigned kPageSize = 4 * KBytes;
47 const unsigned kPageSizeLog2 = 12;
48 
49 const unsigned kBRegSize = 8;
50 const unsigned kBRegSizeLog2 = 3;
51 const unsigned kBRegSizeInBytes = kBRegSize / 8;
52 const unsigned kBRegSizeInBytesLog2 = kBRegSizeLog2 - 3;
53 const unsigned kHRegSize = 16;
54 const unsigned kHRegSizeLog2 = 4;
55 const unsigned kHRegSizeInBytes = kHRegSize / 8;
56 const unsigned kHRegSizeInBytesLog2 = kHRegSizeLog2 - 3;
57 const unsigned kWRegSize = 32;
58 const unsigned kWRegSizeLog2 = 5;
59 const unsigned kWRegSizeInBytes = kWRegSize / 8;
60 const unsigned kWRegSizeInBytesLog2 = kWRegSizeLog2 - 3;
61 const unsigned kXRegSize = 64;
62 const unsigned kXRegSizeLog2 = 6;
63 const unsigned kXRegSizeInBytes = kXRegSize / 8;
64 const unsigned kXRegSizeInBytesLog2 = kXRegSizeLog2 - 3;
65 const unsigned kSRegSize = 32;
66 const unsigned kSRegSizeLog2 = 5;
67 const unsigned kSRegSizeInBytes = kSRegSize / 8;
68 const unsigned kSRegSizeInBytesLog2 = kSRegSizeLog2 - 3;
69 const unsigned kDRegSize = 64;
70 const unsigned kDRegSizeLog2 = 6;
71 const unsigned kDRegSizeInBytes = kDRegSize / 8;
72 const unsigned kDRegSizeInBytesLog2 = kDRegSizeLog2 - 3;
73 const unsigned kQRegSize = 128;
74 const unsigned kQRegSizeLog2 = 7;
75 const unsigned kQRegSizeInBytes = kQRegSize / 8;
76 const unsigned kQRegSizeInBytesLog2 = kQRegSizeLog2 - 3;
77 const uint64_t kWRegMask = UINT64_C(0xffffffff);
78 const uint64_t kXRegMask = UINT64_C(0xffffffffffffffff);
79 const uint64_t kSRegMask = UINT64_C(0xffffffff);
80 const uint64_t kDRegMask = UINT64_C(0xffffffffffffffff);
81 const uint64_t kSSignMask = UINT64_C(0x80000000);
82 const uint64_t kDSignMask = UINT64_C(0x8000000000000000);
83 const uint64_t kWSignMask = UINT64_C(0x80000000);
84 const uint64_t kXSignMask = UINT64_C(0x8000000000000000);
85 const uint64_t kByteMask = UINT64_C(0xff);
86 const uint64_t kHalfWordMask = UINT64_C(0xffff);
87 const uint64_t kWordMask = UINT64_C(0xffffffff);
88 const uint64_t kXMaxUInt = UINT64_C(0xffffffffffffffff);
89 const uint64_t kWMaxUInt = UINT64_C(0xffffffff);
90 const int64_t kXMaxInt = INT64_C(0x7fffffffffffffff);
91 const int64_t kXMinInt = INT64_C(0x8000000000000000);
92 const int32_t kWMaxInt = INT32_C(0x7fffffff);
93 const int32_t kWMinInt = INT32_C(0x80000000);
94 const unsigned kLinkRegCode = 30;
95 const unsigned kZeroRegCode = 31;
96 const unsigned kSPRegInternalCode = 63;
97 const unsigned kRegCodeMask = 0x1f;
98 
99 const unsigned kAddressTagOffset = 56;
100 const unsigned kAddressTagWidth = 8;
101 const uint64_t kAddressTagMask =
102     ((UINT64_C(1) << kAddressTagWidth) - 1) << kAddressTagOffset;
103 VIXL_STATIC_ASSERT(kAddressTagMask == UINT64_C(0xff00000000000000));
104 
105 // AArch64 floating-point specifics. These match IEEE-754.
106 const unsigned kDoubleMantissaBits = 52;
107 const unsigned kDoubleExponentBits = 11;
108 const unsigned kFloatMantissaBits = 23;
109 const unsigned kFloatExponentBits = 8;
110 const unsigned kFloat16MantissaBits = 10;
111 const unsigned kFloat16ExponentBits = 5;
112 
113 // Floating-point infinity values.
114 extern const float16 kFP16PositiveInfinity;
115 extern const float16 kFP16NegativeInfinity;
116 extern const float kFP32PositiveInfinity;
117 extern const float kFP32NegativeInfinity;
118 extern const double kFP64PositiveInfinity;
119 extern const double kFP64NegativeInfinity;
120 
121 // The default NaN values (for FPCR.DN=1).
122 extern const float16 kFP16DefaultNaN;
123 extern const float kFP32DefaultNaN;
124 extern const double kFP64DefaultNaN;
125 
126 unsigned CalcLSDataSize(LoadStoreOp op);
127 unsigned CalcLSPairDataSize(LoadStorePairOp op);
128 
129 enum ImmBranchType {
130   UnknownBranchType = 0,
131   CondBranchType    = 1,
132   UncondBranchType  = 2,
133   CompareBranchType = 3,
134   TestBranchType    = 4
135 };
136 
137 enum AddrMode {
138   Offset,
139   PreIndex,
140   PostIndex
141 };
142 
143 enum FPRounding {
144   // The first four values are encodable directly by FPCR<RMode>.
145   FPTieEven = 0x0,
146   FPPositiveInfinity = 0x1,
147   FPNegativeInfinity = 0x2,
148   FPZero = 0x3,
149 
150   // The final rounding modes are only available when explicitly specified by
151   // the instruction (such as with fcvta). It cannot be set in FPCR.
152   FPTieAway,
153   FPRoundOdd
154 };
155 
156 enum Reg31Mode {
157   Reg31IsStackPointer,
158   Reg31IsZeroRegister
159 };
160 
161 // Instructions. ---------------------------------------------------------------
162 
163 class Instruction {
164  public:
InstructionBits()165   Instr InstructionBits() const {
166     return *(reinterpret_cast<const Instr*>(this));
167   }
168 
SetInstructionBits(Instr new_instr)169   void SetInstructionBits(Instr new_instr) {
170     *(reinterpret_cast<Instr*>(this)) = new_instr;
171   }
172 
Bit(int pos)173   int Bit(int pos) const {
174     return (InstructionBits() >> pos) & 1;
175   }
176 
Bits(int msb,int lsb)177   uint32_t Bits(int msb, int lsb) const {
178     return unsigned_bitextract_32(msb, lsb, InstructionBits());
179   }
180 
SignedBits(int msb,int lsb)181   int32_t SignedBits(int msb, int lsb) const {
182     int32_t bits = *(reinterpret_cast<const int32_t*>(this));
183     return signed_bitextract_32(msb, lsb, bits);
184   }
185 
Mask(uint32_t mask)186   Instr Mask(uint32_t mask) const {
187     return InstructionBits() & mask;
188   }
189 
190   #define DEFINE_GETTER(Name, HighBit, LowBit, Func)             \
191   int64_t Name() const { return Func(HighBit, LowBit); }
INSTRUCTION_FIELDS_LIST(DEFINE_GETTER)192   INSTRUCTION_FIELDS_LIST(DEFINE_GETTER)
193   #undef DEFINE_GETTER
194 
195   // ImmPCRel is a compound field (not present in INSTRUCTION_FIELDS_LIST),
196   // formed from ImmPCRelLo and ImmPCRelHi.
197   int ImmPCRel() const {
198     int const offset = ((ImmPCRelHi() << ImmPCRelLo_width) | ImmPCRelLo());
199     int const width = ImmPCRelLo_width + ImmPCRelHi_width;
200     return signed_bitextract_32(width-1, 0, offset);
201   }
202 
203   uint64_t ImmLogical() const;
204   unsigned ImmNEONabcdefgh() const;
205   float ImmFP32() const;
206   double ImmFP64() const;
207   float ImmNEONFP32() const;
208   double ImmNEONFP64() const;
209 
SizeLS()210   unsigned SizeLS() const {
211     return CalcLSDataSize(static_cast<LoadStoreOp>(Mask(LoadStoreMask)));
212   }
213 
SizeLSPair()214   unsigned SizeLSPair() const {
215     return CalcLSPairDataSize(
216         static_cast<LoadStorePairOp>(Mask(LoadStorePairMask)));
217   }
218 
NEONLSIndex(int access_size_shift)219   int NEONLSIndex(int access_size_shift) const {
220     int q = NEONQ();
221     int s = NEONS();
222     int size = NEONLSSize();
223     int index = (q << 3) | (s << 2) | size;
224     return index >> access_size_shift;
225   }
226 
227   // Helpers.
IsCondBranchImm()228   bool IsCondBranchImm() const {
229     return Mask(ConditionalBranchFMask) == ConditionalBranchFixed;
230   }
231 
IsUncondBranchImm()232   bool IsUncondBranchImm() const {
233     return Mask(UnconditionalBranchFMask) == UnconditionalBranchFixed;
234   }
235 
IsCompareBranch()236   bool IsCompareBranch() const {
237     return Mask(CompareBranchFMask) == CompareBranchFixed;
238   }
239 
IsTestBranch()240   bool IsTestBranch() const {
241     return Mask(TestBranchFMask) == TestBranchFixed;
242   }
243 
IsImmBranch()244   bool IsImmBranch() const {
245     return BranchType() != UnknownBranchType;
246   }
247 
IsPCRelAddressing()248   bool IsPCRelAddressing() const {
249     return Mask(PCRelAddressingFMask) == PCRelAddressingFixed;
250   }
251 
IsLogicalImmediate()252   bool IsLogicalImmediate() const {
253     return Mask(LogicalImmediateFMask) == LogicalImmediateFixed;
254   }
255 
IsAddSubImmediate()256   bool IsAddSubImmediate() const {
257     return Mask(AddSubImmediateFMask) == AddSubImmediateFixed;
258   }
259 
IsAddSubExtended()260   bool IsAddSubExtended() const {
261     return Mask(AddSubExtendedFMask) == AddSubExtendedFixed;
262   }
263 
IsLoadOrStore()264   bool IsLoadOrStore() const {
265     return Mask(LoadStoreAnyFMask) == LoadStoreAnyFixed;
266   }
267 
268   bool IsLoad() const;
269   bool IsStore() const;
270 
IsLoadLiteral()271   bool IsLoadLiteral() const {
272     // This includes PRFM_lit.
273     return Mask(LoadLiteralFMask) == LoadLiteralFixed;
274   }
275 
IsMovn()276   bool IsMovn() const {
277     return (Mask(MoveWideImmediateMask) == MOVN_x) ||
278            (Mask(MoveWideImmediateMask) == MOVN_w);
279   }
280 
281   static int ImmBranchRangeBitwidth(ImmBranchType branch_type);
282   static int32_t ImmBranchForwardRange(ImmBranchType branch_type);
283   static bool IsValidImmPCOffset(ImmBranchType branch_type, int32_t offset);
284 
285   // Indicate whether Rd can be the stack pointer or the zero register. This
286   // does not check that the instruction actually has an Rd field.
RdMode()287   Reg31Mode RdMode() const {
288     // The following instructions use sp or wsp as Rd:
289     //  Add/sub (immediate) when not setting the flags.
290     //  Add/sub (extended) when not setting the flags.
291     //  Logical (immediate) when not setting the flags.
292     // Otherwise, r31 is the zero register.
293     if (IsAddSubImmediate() || IsAddSubExtended()) {
294       if (Mask(AddSubSetFlagsBit)) {
295         return Reg31IsZeroRegister;
296       } else {
297         return Reg31IsStackPointer;
298       }
299     }
300     if (IsLogicalImmediate()) {
301       // Of the logical (immediate) instructions, only ANDS (and its aliases)
302       // can set the flags. The others can all write into sp.
303       // Note that some logical operations are not available to
304       // immediate-operand instructions, so we have to combine two masks here.
305       if (Mask(LogicalImmediateMask & LogicalOpMask) == ANDS) {
306         return Reg31IsZeroRegister;
307       } else {
308         return Reg31IsStackPointer;
309       }
310     }
311     return Reg31IsZeroRegister;
312   }
313 
314   // Indicate whether Rn can be the stack pointer or the zero register. This
315   // does not check that the instruction actually has an Rn field.
RnMode()316   Reg31Mode RnMode() const {
317     // The following instructions use sp or wsp as Rn:
318     //  All loads and stores.
319     //  Add/sub (immediate).
320     //  Add/sub (extended).
321     // Otherwise, r31 is the zero register.
322     if (IsLoadOrStore() || IsAddSubImmediate() || IsAddSubExtended()) {
323       return Reg31IsStackPointer;
324     }
325     return Reg31IsZeroRegister;
326   }
327 
BranchType()328   ImmBranchType BranchType() const {
329     if (IsCondBranchImm()) {
330       return CondBranchType;
331     } else if (IsUncondBranchImm()) {
332       return UncondBranchType;
333     } else if (IsCompareBranch()) {
334       return CompareBranchType;
335     } else if (IsTestBranch()) {
336       return TestBranchType;
337     } else {
338       return UnknownBranchType;
339     }
340   }
341 
342   // Find the target of this instruction. 'this' may be a branch or a
343   // PC-relative addressing instruction.
344   const Instruction* ImmPCOffsetTarget() const;
345 
346   // Patch a PC-relative offset to refer to 'target'. 'this' may be a branch or
347   // a PC-relative addressing instruction.
348   void SetImmPCOffsetTarget(const Instruction* target);
349   // Patch a literal load instruction to load from 'source'.
350   void SetImmLLiteral(const Instruction* source);
351 
352   // The range of a load literal instruction, expressed as 'instr +- range'.
353   // The range is actually the 'positive' range; the branch instruction can
354   // target [instr - range - kInstructionSize, instr + range].
355   static const int kLoadLiteralImmBitwidth = 19;
356   static const int kLoadLiteralRange =
357       (1 << kLoadLiteralImmBitwidth) / 2 - kInstructionSize;
358 
359   // Calculate the address of a literal referred to by a load-literal
360   // instruction, and return it as the specified type.
361   //
362   // The literal itself is safely mutable only if the backing buffer is safely
363   // mutable.
364   template <typename T>
LiteralAddress()365   T LiteralAddress() const {
366     uint64_t base_raw = reinterpret_cast<uintptr_t>(this);
367     ptrdiff_t offset = ImmLLiteral() << kLiteralEntrySizeLog2;
368     uint64_t address_raw = base_raw + offset;
369 
370     // Cast the address using a C-style cast. A reinterpret_cast would be
371     // appropriate, but it can't cast one integral type to another.
372     T address = (T)(address_raw);
373 
374     // Assert that the address can be represented by the specified type.
375     VIXL_ASSERT((uint64_t)(address) == address_raw);
376 
377     return address;
378   }
379 
Literal32()380   uint32_t Literal32() const {
381     uint32_t literal;
382     memcpy(&literal, LiteralAddress<const void*>(), sizeof(literal));
383     return literal;
384   }
385 
Literal64()386   uint64_t Literal64() const {
387     uint64_t literal;
388     memcpy(&literal, LiteralAddress<const void*>(), sizeof(literal));
389     return literal;
390   }
391 
LiteralFP32()392   float LiteralFP32() const {
393     return rawbits_to_float(Literal32());
394   }
395 
LiteralFP64()396   double LiteralFP64() const {
397     return rawbits_to_double(Literal64());
398   }
399 
NextInstruction()400   const Instruction* NextInstruction() const {
401     return this + kInstructionSize;
402   }
403 
InstructionAtOffset(int64_t offset)404   const Instruction* InstructionAtOffset(int64_t offset) const {
405     VIXL_ASSERT(IsWordAligned(this + offset));
406     return this + offset;
407   }
408 
Cast(T src)409   template<typename T> static Instruction* Cast(T src) {
410     return reinterpret_cast<Instruction*>(src);
411   }
412 
CastConst(T src)413   template<typename T> static const Instruction* CastConst(T src) {
414     return reinterpret_cast<const Instruction*>(src);
415   }
416 
417  private:
418   int ImmBranch() const;
419 
420   static float Imm8ToFP32(uint32_t imm8);
421   static double Imm8ToFP64(uint32_t imm8);
422 
423   void SetPCRelImmTarget(const Instruction* target);
424   void SetBranchImmTarget(const Instruction* target);
425 };
426 
427 
428 // Functions for handling NEON vector format information.
429 enum VectorFormat {
430   kFormatUndefined = 0xffffffff,
431   kFormat8B  = NEON_8B,
432   kFormat16B = NEON_16B,
433   kFormat4H  = NEON_4H,
434   kFormat8H  = NEON_8H,
435   kFormat2S  = NEON_2S,
436   kFormat4S  = NEON_4S,
437   kFormat1D  = NEON_1D,
438   kFormat2D  = NEON_2D,
439 
440   // Scalar formats. We add the scalar bit to distinguish between scalar and
441   // vector enumerations; the bit is always set in the encoding of scalar ops
442   // and always clear for vector ops. Although kFormatD and kFormat1D appear
443   // to be the same, their meaning is subtly different. The first is a scalar
444   // operation, the second a vector operation that only affects one lane.
445   kFormatB = NEON_B | NEONScalar,
446   kFormatH = NEON_H | NEONScalar,
447   kFormatS = NEON_S | NEONScalar,
448   kFormatD = NEON_D | NEONScalar
449 };
450 
451 VectorFormat VectorFormatHalfWidth(const VectorFormat vform);
452 VectorFormat VectorFormatDoubleWidth(const VectorFormat vform);
453 VectorFormat VectorFormatDoubleLanes(const VectorFormat vform);
454 VectorFormat VectorFormatHalfLanes(const VectorFormat vform);
455 VectorFormat ScalarFormatFromLaneSize(int lanesize);
456 VectorFormat VectorFormatHalfWidthDoubleLanes(const VectorFormat vform);
457 VectorFormat VectorFormatFillQ(const VectorFormat vform);
458 unsigned RegisterSizeInBitsFromFormat(VectorFormat vform);
459 unsigned RegisterSizeInBytesFromFormat(VectorFormat vform);
460 // TODO: Make the return types of these functions consistent.
461 unsigned LaneSizeInBitsFromFormat(VectorFormat vform);
462 int LaneSizeInBytesFromFormat(VectorFormat vform);
463 int LaneSizeInBytesLog2FromFormat(VectorFormat vform);
464 int LaneCountFromFormat(VectorFormat vform);
465 int MaxLaneCountFromFormat(VectorFormat vform);
466 bool IsVectorFormat(VectorFormat vform);
467 int64_t MaxIntFromFormat(VectorFormat vform);
468 int64_t MinIntFromFormat(VectorFormat vform);
469 uint64_t MaxUintFromFormat(VectorFormat vform);
470 
471 
472 enum NEONFormat {
473   NF_UNDEF = 0,
474   NF_8B    = 1,
475   NF_16B   = 2,
476   NF_4H    = 3,
477   NF_8H    = 4,
478   NF_2S    = 5,
479   NF_4S    = 6,
480   NF_1D    = 7,
481   NF_2D    = 8,
482   NF_B     = 9,
483   NF_H     = 10,
484   NF_S     = 11,
485   NF_D     = 12
486 };
487 
488 static const unsigned kNEONFormatMaxBits = 6;
489 
490 struct NEONFormatMap {
491   // The bit positions in the instruction to consider.
492   uint8_t bits[kNEONFormatMaxBits];
493 
494   // Mapping from concatenated bits to format.
495   NEONFormat map[1 << kNEONFormatMaxBits];
496 };
497 
498 class NEONFormatDecoder {
499  public:
500   enum SubstitutionMode {
501     kPlaceholder,
502     kFormat
503   };
504 
505   // Construct a format decoder with increasingly specific format maps for each
506   // subsitution. If no format map is specified, the default is the integer
507   // format map.
NEONFormatDecoder(const Instruction * instr)508   explicit NEONFormatDecoder(const Instruction* instr) {
509     instrbits_ = instr->InstructionBits();
510     SetFormatMaps(IntegerFormatMap());
511   }
NEONFormatDecoder(const Instruction * instr,const NEONFormatMap * format)512   NEONFormatDecoder(const Instruction* instr,
513                     const NEONFormatMap* format) {
514     instrbits_ = instr->InstructionBits();
515     SetFormatMaps(format);
516   }
NEONFormatDecoder(const Instruction * instr,const NEONFormatMap * format0,const NEONFormatMap * format1)517   NEONFormatDecoder(const Instruction* instr,
518                     const NEONFormatMap* format0,
519                     const NEONFormatMap* format1) {
520     instrbits_ = instr->InstructionBits();
521     SetFormatMaps(format0, format1);
522   }
NEONFormatDecoder(const Instruction * instr,const NEONFormatMap * format0,const NEONFormatMap * format1,const NEONFormatMap * format2)523   NEONFormatDecoder(const Instruction* instr,
524                     const NEONFormatMap* format0,
525                     const NEONFormatMap* format1,
526                     const NEONFormatMap* format2) {
527     instrbits_ = instr->InstructionBits();
528     SetFormatMaps(format0, format1, format2);
529   }
530 
531   // Set the format mapping for all or individual substitutions.
532   void SetFormatMaps(const NEONFormatMap* format0,
533                      const NEONFormatMap* format1 = NULL,
534                      const NEONFormatMap* format2 = NULL) {
535     VIXL_ASSERT(format0 != NULL);
536     formats_[0] = format0;
537     formats_[1] = (format1 == NULL) ? formats_[0] : format1;
538     formats_[2] = (format2 == NULL) ? formats_[1] : format2;
539   }
SetFormatMap(unsigned index,const NEONFormatMap * format)540   void SetFormatMap(unsigned index, const NEONFormatMap* format) {
541     VIXL_ASSERT(index <= (sizeof(formats_) / sizeof(formats_[0])));
542     VIXL_ASSERT(format != NULL);
543     formats_[index] = format;
544   }
545 
546   // Substitute %s in the input string with the placeholder string for each
547   // register, ie. "'B", "'H", etc.
SubstitutePlaceholders(const char * string)548   const char* SubstitutePlaceholders(const char* string) {
549     return Substitute(string, kPlaceholder, kPlaceholder, kPlaceholder);
550   }
551 
552   // Substitute %s in the input string with a new string based on the
553   // substitution mode.
554   const char* Substitute(const char* string,
555                          SubstitutionMode mode0 = kFormat,
556                          SubstitutionMode mode1 = kFormat,
557                          SubstitutionMode mode2 = kFormat) {
558     snprintf(form_buffer_, sizeof(form_buffer_), string,
559              GetSubstitute(0, mode0),
560              GetSubstitute(1, mode1),
561              GetSubstitute(2, mode2));
562     return form_buffer_;
563   }
564 
565   // Append a "2" to a mnemonic string based of the state of the Q bit.
Mnemonic(const char * mnemonic)566   const char* Mnemonic(const char* mnemonic) {
567     if ((instrbits_ & NEON_Q) != 0) {
568       snprintf(mne_buffer_, sizeof(mne_buffer_), "%s2", mnemonic);
569       return mne_buffer_;
570     }
571     return mnemonic;
572   }
573 
574   VectorFormat GetVectorFormat(int format_index = 0) {
575     return GetVectorFormat(formats_[format_index]);
576   }
577 
GetVectorFormat(const NEONFormatMap * format_map)578   VectorFormat GetVectorFormat(const NEONFormatMap* format_map) {
579     static const VectorFormat vform[] = {
580       kFormatUndefined,
581       kFormat8B, kFormat16B, kFormat4H, kFormat8H,
582       kFormat2S, kFormat4S, kFormat1D, kFormat2D,
583       kFormatB, kFormatH, kFormatS, kFormatD
584     };
585     VIXL_ASSERT(GetNEONFormat(format_map) < (sizeof(vform) / sizeof(vform[0])));
586     return vform[GetNEONFormat(format_map)];
587   }
588 
589   // Built in mappings for common cases.
590 
591   // The integer format map uses three bits (Q, size<1:0>) to encode the
592   // "standard" set of NEON integer vector formats.
IntegerFormatMap()593   static const NEONFormatMap* IntegerFormatMap() {
594     static const NEONFormatMap map = {
595       {23, 22, 30},
596       {NF_8B, NF_16B, NF_4H, NF_8H, NF_2S, NF_4S, NF_UNDEF, NF_2D}
597     };
598     return &map;
599   }
600 
601   // The long integer format map uses two bits (size<1:0>) to encode the
602   // long set of NEON integer vector formats. These are used in narrow, wide
603   // and long operations.
LongIntegerFormatMap()604   static const NEONFormatMap* LongIntegerFormatMap() {
605     static const NEONFormatMap map = {
606       {23, 22}, {NF_8H, NF_4S, NF_2D}
607     };
608     return &map;
609   }
610 
611   // The FP format map uses two bits (Q, size<0>) to encode the NEON FP vector
612   // formats: NF_2S, NF_4S, NF_2D.
FPFormatMap()613   static const NEONFormatMap* FPFormatMap() {
614     // The FP format map assumes two bits (Q, size<0>) are used to encode the
615     // NEON FP vector formats: NF_2S, NF_4S, NF_2D.
616     static const NEONFormatMap map = {
617       {22, 30}, {NF_2S, NF_4S, NF_UNDEF, NF_2D}
618     };
619     return &map;
620   }
621 
622   // The load/store format map uses three bits (Q, 11, 10) to encode the
623   // set of NEON vector formats.
LoadStoreFormatMap()624   static const NEONFormatMap* LoadStoreFormatMap() {
625     static const NEONFormatMap map = {
626       {11, 10, 30},
627       {NF_8B, NF_16B, NF_4H, NF_8H, NF_2S, NF_4S, NF_1D, NF_2D}
628     };
629     return &map;
630   }
631 
632   // The logical format map uses one bit (Q) to encode the NEON vector format:
633   // NF_8B, NF_16B.
LogicalFormatMap()634   static const NEONFormatMap* LogicalFormatMap() {
635     static const NEONFormatMap map = {
636       {30}, {NF_8B, NF_16B}
637     };
638     return &map;
639   }
640 
641   // The triangular format map uses between two and five bits to encode the NEON
642   // vector format:
643   // xxx10->8B, xxx11->16B, xx100->4H, xx101->8H
644   // x1000->2S, x1001->4S,  10001->2D, all others undefined.
TriangularFormatMap()645   static const NEONFormatMap* TriangularFormatMap() {
646     static const NEONFormatMap map = {
647       {19, 18, 17, 16, 30},
648       {NF_UNDEF, NF_UNDEF, NF_8B, NF_16B, NF_4H, NF_8H, NF_8B, NF_16B, NF_2S,
649        NF_4S, NF_8B, NF_16B, NF_4H, NF_8H, NF_8B, NF_16B, NF_UNDEF, NF_2D,
650        NF_8B, NF_16B, NF_4H, NF_8H, NF_8B, NF_16B, NF_2S, NF_4S, NF_8B, NF_16B,
651        NF_4H, NF_8H, NF_8B, NF_16B}
652     };
653     return &map;
654   }
655 
656   // The scalar format map uses two bits (size<1:0>) to encode the NEON scalar
657   // formats: NF_B, NF_H, NF_S, NF_D.
ScalarFormatMap()658   static const NEONFormatMap* ScalarFormatMap() {
659     static const NEONFormatMap map = {
660       {23, 22}, {NF_B, NF_H, NF_S, NF_D}
661     };
662     return &map;
663   }
664 
665   // The long scalar format map uses two bits (size<1:0>) to encode the longer
666   // NEON scalar formats: NF_H, NF_S, NF_D.
LongScalarFormatMap()667   static const NEONFormatMap* LongScalarFormatMap() {
668     static const NEONFormatMap map = {
669       {23, 22}, {NF_H, NF_S, NF_D}
670     };
671     return &map;
672   }
673 
674   // The FP scalar format map assumes one bit (size<0>) is used to encode the
675   // NEON FP scalar formats: NF_S, NF_D.
FPScalarFormatMap()676   static const NEONFormatMap* FPScalarFormatMap() {
677     static const NEONFormatMap map = {
678       {22}, {NF_S, NF_D}
679     };
680     return &map;
681   }
682 
683   // The triangular scalar format map uses between one and four bits to encode
684   // the NEON FP scalar formats:
685   // xxx1->B, xx10->H, x100->S, 1000->D, all others undefined.
TriangularScalarFormatMap()686   static const NEONFormatMap* TriangularScalarFormatMap() {
687     static const NEONFormatMap map = {
688       {19, 18, 17, 16},
689       {NF_UNDEF, NF_B, NF_H, NF_B, NF_S, NF_B, NF_H, NF_B,
690        NF_D,     NF_B, NF_H, NF_B, NF_S, NF_B, NF_H, NF_B}
691     };
692     return &map;
693   }
694 
695  private:
696   // Get a pointer to a string that represents the format or placeholder for
697   // the specified substitution index, based on the format map and instruction.
GetSubstitute(int index,SubstitutionMode mode)698   const char* GetSubstitute(int index, SubstitutionMode mode) {
699     if (mode == kFormat) {
700       return NEONFormatAsString(GetNEONFormat(formats_[index]));
701     }
702     VIXL_ASSERT(mode == kPlaceholder);
703     return NEONFormatAsPlaceholder(GetNEONFormat(formats_[index]));
704   }
705 
706   // Get the NEONFormat enumerated value for bits obtained from the
707   // instruction based on the specified format mapping.
GetNEONFormat(const NEONFormatMap * format_map)708   NEONFormat GetNEONFormat(const NEONFormatMap* format_map) {
709     return format_map->map[PickBits(format_map->bits)];
710   }
711 
712   // Convert a NEONFormat into a string.
NEONFormatAsString(NEONFormat format)713   static const char* NEONFormatAsString(NEONFormat format) {
714     static const char* formats[] = {
715       "undefined",
716       "8b", "16b", "4h", "8h", "2s", "4s", "1d", "2d",
717       "b", "h", "s", "d"
718     };
719     VIXL_ASSERT(format < (sizeof(formats) / sizeof(formats[0])));
720     return formats[format];
721   }
722 
723   // Convert a NEONFormat into a register placeholder string.
NEONFormatAsPlaceholder(NEONFormat format)724   static const char* NEONFormatAsPlaceholder(NEONFormat format) {
725     VIXL_ASSERT((format == NF_B) || (format == NF_H) ||
726                 (format == NF_S) || (format == NF_D) ||
727                 (format == NF_UNDEF));
728     static const char* formats[] = {
729       "undefined",
730       "undefined", "undefined", "undefined", "undefined",
731       "undefined", "undefined", "undefined", "undefined",
732       "'B", "'H", "'S", "'D"
733     };
734     return formats[format];
735   }
736 
737   // Select bits from instrbits_ defined by the bits array, concatenate them,
738   // and return the value.
PickBits(const uint8_t bits[])739   uint8_t PickBits(const uint8_t bits[]) {
740     uint8_t result = 0;
741     for (unsigned b = 0; b < kNEONFormatMaxBits; b++) {
742       if (bits[b] == 0) break;
743       result <<= 1;
744       result |= ((instrbits_ & (1 << bits[b])) == 0) ? 0 : 1;
745     }
746     return result;
747   }
748 
749   Instr instrbits_;
750   const NEONFormatMap* formats_[3];
751   char form_buffer_[64];
752   char mne_buffer_[16];
753 };
754 }  // namespace vixl
755 
756 #endif  // VIXL_A64_INSTRUCTIONS_A64_H_
757