| 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 "jit/arm64/vixl/Constants-vixl.h" |
| 31 |
#include "jit/arm64/vixl/Globals-vixl.h" |
| 32 |
#include "jit/arm64/vixl/Utils-vixl.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 kXMaxExactUInt = UINT64_C(0xfffffffffffff800); |
| 90 |
const uint64_t kWMaxUInt = UINT64_C(0xffffffff); |
| 91 |
const int64_t kXMaxInt = INT64_C(0x7fffffffffffffff); |
| 92 |
const int64_t kXMaxExactInt = UINT64_C(0x7ffffffffffffc00); |
| 93 |
const int64_t kXMinInt = INT64_C(0x8000000000000000); |
| 94 |
const int32_t kWMaxInt = INT32_C(0x7fffffff); |
| 95 |
const int32_t kWMinInt = INT32_C(0x80000000); |
| 96 |
const unsigned kLinkRegCode = 30; |
| 97 |
const unsigned kZeroRegCode = 31; |
| 98 |
const unsigned kSPRegInternalCode = 63; |
| 99 |
const unsigned kRegCodeMask = 0x1f; |
| 100 |
|
| 101 |
const unsigned kAddressTagOffset = 56; |
| 102 |
const unsigned kAddressTagWidth = 8; |
| 103 |
const uint64_t kAddressTagMask = |
| 104 |
((UINT64_C(1) << kAddressTagWidth) - 1) << kAddressTagOffset; |
| 105 |
VIXL_STATIC_ASSERT(kAddressTagMask == UINT64_C(0xff00000000000000)); |
| 106 |
|
| 107 |
static inline unsigned CalcLSDataSize(LoadStoreOp op) { |
| 108 |
VIXL_ASSERT((LSSize_offset + LSSize_width) == (kInstructionSize * 8)); |
| 109 |
unsigned size = static_cast<Instr>(op) >> LSSize_offset; |
| 110 |
if ((op & LSVector_mask) != 0) { |
| 111 |
// Vector register memory operations encode the access size in the "size" |
| 112 |
// and "opc" fields. |
| 113 |
if ((size == 0) && ((op & LSOpc_mask) >> LSOpc_offset) >= 2) { |
| 114 |
size = kQRegSizeInBytesLog2; |
| 115 |
} |
| 116 |
} |
| 117 |
return size; |
| 118 |
} |
| 119 |
|
| 120 |
unsigned CalcLSPairDataSize(LoadStorePairOp op); |
| 121 |
|
| 122 |
enum ImmBranchType { |
| 123 |
UnknownBranchType = 0, |
| 124 |
CondBranchType = 1, |
| 125 |
UncondBranchType = 2, |
| 126 |
CompareBranchType = 3, |
| 127 |
TestBranchType = 4 |
| 128 |
}; |
| 129 |
|
| 130 |
// The classes of immediate branch ranges, in order of increasing range. |
| 131 |
// Note that CondBranchType and CompareBranchType have the same range. |
| 132 |
enum ImmBranchRangeType { |
| 133 |
TestBranchRangeType, // tbz/tbnz: imm14 = +/- 32KB. |
| 134 |
CondBranchRangeType, // b.cond/cbz/cbnz: imm19 = +/- 1MB. |
| 135 |
UncondBranchRangeType, // b/bl: imm26 = +/- 128MB. |
| 136 |
UnknownBranchRangeType, |
| 137 |
|
| 138 |
// Number of 'short-range' branch range types. |
| 139 |
// We don't consider unconditional branches 'short-range'. |
| 140 |
NumShortBranchRangeTypes = UncondBranchRangeType |
| 141 |
}; |
| 142 |
|
| 143 |
enum AddrMode { |
| 144 |
Offset, |
| 145 |
PreIndex, |
| 146 |
PostIndex |
| 147 |
}; |
| 148 |
|
| 149 |
enum Reg31Mode { |
| 150 |
Reg31IsStackPointer, |
| 151 |
Reg31IsZeroRegister |
| 152 |
}; |
| 153 |
|
| 154 |
// Instructions. --------------------------------------------------------------- |
| 155 |
|
| 156 |
class Instruction { |
| 157 |
public: |
| 158 |
Instr InstructionBits() const { |
| 159 |
return *(reinterpret_cast<const Instr*>(this)); |
| 160 |
} |
| 161 |
|
| 162 |
void SetInstructionBits(Instr new_instr) { |
| 163 |
*(reinterpret_cast<Instr*>(this)) = new_instr; |
| 164 |
} |
| 165 |
|
| 166 |
int Bit(int pos) const { |
| 167 |
return (InstructionBits() >> pos) & 1; |
| 168 |
} |
| 169 |
|
| 170 |
uint32_t Bits(int msb, int lsb) const { |
| 171 |
return ExtractUnsignedBitfield32(msb, lsb, InstructionBits()); |
| 172 |
} |
| 173 |
|
| 174 |
int32_t SignedBits(int msb, int lsb) const { |
| 175 |
int32_t bits = *(reinterpret_cast<const int32_t*>(this)); |
| 176 |
return ExtractSignedBitfield32(msb, lsb, bits); |
| 177 |
} |
| 178 |
|
| 179 |
Instr Mask(uint32_t mask) const { |
| 180 |
return InstructionBits() & mask; |
| 181 |
} |
| 182 |
|
| 183 |
#define DEFINE_GETTER(Name, HighBit, LowBit, Func) \ |
| 184 |
int32_t Name() const { return Func(HighBit, LowBit); } |
| 185 |
INSTRUCTION_FIELDS_LIST(DEFINE_GETTER) |
| 186 |
#undef DEFINE_GETTER |
| 187 |
|
| 188 |
#define DEFINE_SETTER(Name, HighBit, LowBit, Func) \ |
| 189 |
inline void Set##Name(unsigned n) { SetBits32(HighBit, LowBit, n); } |
| 190 |
INSTRUCTION_FIELDS_LIST(DEFINE_SETTER) |
| 191 |
#undef DEFINE_SETTER |
| 192 |
|
| 193 |
// ImmPCRel is a compound field (not present in INSTRUCTION_FIELDS_LIST), |
| 194 |
// formed from ImmPCRelLo and ImmPCRelHi. |
| 195 |
int ImmPCRel() const { |
| 196 |
int offset = |
| 197 |
static_cast<int>((ImmPCRelHi() << ImmPCRelLo_width) | ImmPCRelLo()); |
| 198 |
int width = ImmPCRelLo_width + ImmPCRelHi_width; |
| 199 |
return ExtractSignedBitfield32(width - 1, 0, offset); |
| 200 |
} |
| 201 |
|
| 202 |
uint64_t ImmLogical() const; |
| 203 |
unsigned ImmNEONabcdefgh() const; |
| 204 |
float ImmFP32() const; |
| 205 |
double ImmFP64() const; |
| 206 |
float ImmNEONFP32() const; |
| 207 |
double ImmNEONFP64() const; |
| 208 |
|
| 209 |
unsigned SizeLS() const { |
| 210 |
return CalcLSDataSize(static_cast<LoadStoreOp>(Mask(LoadStoreMask))); |
| 211 |
} |
| 212 |
|
| 213 |
unsigned SizeLSPair() const { |
| 214 |
return CalcLSPairDataSize( |
| 215 |
static_cast<LoadStorePairOp>(Mask(LoadStorePairMask))); |
| 216 |
} |
| 217 |
|
| 218 |
int NEONLSIndex(int access_size_shift) const { |
| 219 |
int64_t q = NEONQ(); |
| 220 |
int64_t s = NEONS(); |
| 221 |
int64_t size = NEONLSSize(); |
| 222 |
int64_t index = (q << 3) | (s << 2) | size; |
| 223 |
return static_cast<int>(index >> access_size_shift); |
| 224 |
} |
| 225 |
|
| 226 |
// Helpers. |
| 227 |
bool IsCondBranchImm() const { |
| 228 |
return Mask(ConditionalBranchFMask) == ConditionalBranchFixed; |
| 229 |
} |
| 230 |
|
| 231 |
bool IsUncondBranchImm() const { |
| 232 |
return Mask(UnconditionalBranchFMask) == UnconditionalBranchFixed; |
| 233 |
} |
| 234 |
|
| 235 |
bool IsCompareBranch() const { |
| 236 |
return Mask(CompareBranchFMask) == CompareBranchFixed; |
| 237 |
} |
| 238 |
|
| 239 |
bool IsTestBranch() const { |
| 240 |
return Mask(TestBranchFMask) == TestBranchFixed; |
| 241 |
} |
| 242 |
|
| 243 |
bool IsImmBranch() const { |
| 244 |
return BranchType() != UnknownBranchType; |
| 245 |
} |
| 246 |
|
| 247 |
bool IsPCRelAddressing() const { |
| 248 |
return Mask(PCRelAddressingFMask) == PCRelAddressingFixed; |
| 249 |
} |
| 250 |
|
| 251 |
bool IsLogicalImmediate() const { |
| 252 |
return Mask(LogicalImmediateFMask) == LogicalImmediateFixed; |
| 253 |
} |
| 254 |
|
| 255 |
bool IsAddSubImmediate() const { |
| 256 |
return Mask(AddSubImmediateFMask) == AddSubImmediateFixed; |
| 257 |
} |
| 258 |
|
| 259 |
bool IsAddSubExtended() const { |
| 260 |
return Mask(AddSubExtendedFMask) == AddSubExtendedFixed; |
| 261 |
} |
| 262 |
|
| 263 |
bool IsLoadOrStore() const { |
| 264 |
return Mask(LoadStoreAnyFMask) == LoadStoreAnyFixed; |
| 265 |
} |
| 266 |
|
| 267 |
bool IsLoad() const; |
| 268 |
bool IsStore() const; |
| 269 |
|
| 270 |
bool IsLoadLiteral() const { |
| 271 |
// This includes PRFM_lit. |
| 272 |
return Mask(LoadLiteralFMask) == LoadLiteralFixed; |
| 273 |
} |
| 274 |
|
| 275 |
bool IsMovn() const { |
| 276 |
return (Mask(MoveWideImmediateMask) == MOVN_x) || |
| 277 |
(Mask(MoveWideImmediateMask) == MOVN_w); |
| 278 |
} |
| 279 |
|
| 280 |
// Mozilla modifications. |
| 281 |
bool IsUncondB() const; |
| 282 |
bool IsCondB() const; |
| 283 |
bool IsBL() const; |
| 284 |
bool IsBR() const; |
| 285 |
bool IsBLR() const; |
| 286 |
bool IsTBZ() const; |
| 287 |
bool IsTBNZ() const; |
| 288 |
bool IsCBZ() const; |
| 289 |
bool IsCBNZ() const; |
| 290 |
bool IsLDR() const; |
| 291 |
bool IsNOP() const; |
| 292 |
bool IsCSDB() const; |
| 293 |
bool IsADR() const; |
| 294 |
bool IsADRP() const; |
| 295 |
bool IsMovz() const; |
| 296 |
bool IsMovk() const; |
| 297 |
bool IsBranchLinkImm() const; |
| 298 |
bool IsTargetReachable(const Instruction* target) const; |
| 299 |
ptrdiff_t ImmPCRawOffset() const; |
| 300 |
void SetImmPCRawOffset(ptrdiff_t offset); |
| 301 |
void SetBits32(int msb, int lsb, unsigned value); |
| 302 |
|
| 303 |
// Is this a stack pointer synchronization instruction as inserted by |
| 304 |
// MacroAssembler::syncStackPtr()? |
| 305 |
bool IsStackPtrSync() const; |
| 306 |
|
| 307 |
static int ImmBranchRangeBitwidth(ImmBranchType branch_type); |
| 308 |
static int32_t ImmBranchForwardRange(ImmBranchType branch_type); |
| 309 |
|
| 310 |
// Check if offset can be encoded as a RAW offset in a branch_type |
| 311 |
// instruction. The offset must be encodeable directly as the immediate field |
| 312 |
// in the instruction, it is not scaled by kInstructionSize first. |
| 313 |
static bool IsValidImmPCOffset(ImmBranchType branch_type, int64_t offset); |
| 314 |
|
| 315 |
// Get the range type corresponding to a branch type. |
| 316 |
static ImmBranchRangeType ImmBranchTypeToRange(ImmBranchType); |
| 317 |
|
| 318 |
// Get the maximum realizable forward PC offset (in bytes) for an immediate |
| 319 |
// branch of the given range type. |
| 320 |
// This is the largest positive multiple of kInstructionSize, offset, such |
| 321 |
// that: |
| 322 |
// |
| 323 |
// IsValidImmPCOffset(xxx, offset / kInstructionSize) |
| 324 |
// |
| 325 |
// returns true for the same branch type. |
| 326 |
static int32_t ImmBranchMaxForwardOffset(ImmBranchRangeType range_type); |
| 327 |
|
| 328 |
// Get the minimuum realizable backward PC offset (in bytes) for an immediate |
| 329 |
// branch of the given range type. |
| 330 |
// This is the smallest (i.e., largest in magnitude) negative multiple of |
| 331 |
// kInstructionSize, offset, such that: |
| 332 |
// |
| 333 |
// IsValidImmPCOffset(xxx, offset / kInstructionSize) |
| 334 |
// |
| 335 |
// returns true for the same branch type. |
| 336 |
static int32_t ImmBranchMinBackwardOffset(ImmBranchRangeType range_type); |
| 337 |
|
| 338 |
// Indicate whether Rd can be the stack pointer or the zero register. This |
| 339 |
// does not check that the instruction actually has an Rd field. |
| 340 |
Reg31Mode RdMode() const { |
| 341 |
// The following instructions use sp or wsp as Rd: |
| 342 |
// Add/sub (immediate) when not setting the flags. |
| 343 |
// Add/sub (extended) when not setting the flags. |
| 344 |
// Logical (immediate) when not setting the flags. |
| 345 |
// Otherwise, r31 is the zero register. |
| 346 |
if (IsAddSubImmediate() || IsAddSubExtended()) { |
| 347 |
if (Mask(AddSubSetFlagsBit)) { |
| 348 |
return Reg31IsZeroRegister; |
| 349 |
} else { |
| 350 |
return Reg31IsStackPointer; |
| 351 |
} |
| 352 |
} |
| 353 |
if (IsLogicalImmediate()) { |
| 354 |
// Of the logical (immediate) instructions, only ANDS (and its aliases) |
| 355 |
// can set the flags. The others can all write into sp. |
| 356 |
// Note that some logical operations are not available to |
| 357 |
// immediate-operand instructions, so we have to combine two masks here. |
| 358 |
if (Mask(LogicalImmediateMask & LogicalOpMask) == ANDS) { |
| 359 |
return Reg31IsZeroRegister; |
| 360 |
} else { |
| 361 |
return Reg31IsStackPointer; |
| 362 |
} |
| 363 |
} |
| 364 |
return Reg31IsZeroRegister; |
| 365 |
} |
| 366 |
|
| 367 |
// Indicate whether Rn can be the stack pointer or the zero register. This |
| 368 |
// does not check that the instruction actually has an Rn field. |
| 369 |
Reg31Mode RnMode() const { |
| 370 |
// The following instructions use sp or wsp as Rn: |
| 371 |
// All loads and stores. |
| 372 |
// Add/sub (immediate). |
| 373 |
// Add/sub (extended). |
| 374 |
// Otherwise, r31 is the zero register. |
| 375 |
if (IsLoadOrStore() || IsAddSubImmediate() || IsAddSubExtended()) { |
| 376 |
return Reg31IsStackPointer; |
| 377 |
} |
| 378 |
return Reg31IsZeroRegister; |
| 379 |
} |
| 380 |
|
| 381 |
ImmBranchType BranchType() const { |
| 382 |
if (IsCondBranchImm()) { |
| 383 |
return CondBranchType; |
| 384 |
} else if (IsUncondBranchImm()) { |
| 385 |
return UncondBranchType; |
| 386 |
} else if (IsCompareBranch()) { |
| 387 |
return CompareBranchType; |
| 388 |
} else if (IsTestBranch()) { |
| 389 |
return TestBranchType; |
| 390 |
} else { |
| 391 |
return UnknownBranchType; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
// Find the target of this instruction. 'this' may be a branch or a |
| 396 |
// PC-relative addressing instruction. |
| 397 |
const Instruction* ImmPCOffsetTarget() const; |
| 398 |
|
| 399 |
// Patch a PC-relative offset to refer to 'target'. 'this' may be a branch or |
| 400 |
// a PC-relative addressing instruction. |
| 401 |
void SetImmPCOffsetTarget(const Instruction* target); |
| 402 |
// Patch a literal load instruction to load from 'source'. |
| 403 |
void SetImmLLiteral(const Instruction* source); |
| 404 |
|
| 405 |
// The range of a load literal instruction, expressed as 'instr +- range'. |
| 406 |
// The range is actually the 'positive' range; the branch instruction can |
| 407 |
// target [instr - range - kInstructionSize, instr + range]. |
| 408 |
static const int kLoadLiteralImmBitwidth = 19; |
| 409 |
static const int kLoadLiteralRange = |
| 410 |
(1 << kLoadLiteralImmBitwidth) / 2 - kInstructionSize; |
| 411 |
|
| 412 |
// Calculate the address of a literal referred to by a load-literal |
| 413 |
// instruction, and return it as the specified type. |
| 414 |
// |
| 415 |
// The literal itself is safely mutable only if the backing buffer is safely |
| 416 |
// mutable. |
| 417 |
template <typename T> |
| 418 |
T LiteralAddress() const { |
| 419 |
uint64_t base_raw = reinterpret_cast<uint64_t>(this); |
| 420 |
int64_t offset = ImmLLiteral() << kLiteralEntrySizeLog2; |
| 421 |
uint64_t address_raw = base_raw + offset; |
| 422 |
|
| 423 |
// Cast the address using a C-style cast. A reinterpret_cast would be |
| 424 |
// appropriate, but it can't cast one integral type to another. |
| 425 |
T address = (T)(address_raw); |
| 426 |
|
| 427 |
// Assert that the address can be represented by the specified type. |
| 428 |
VIXL_ASSERT((uint64_t)(address) == address_raw); |
| 429 |
|
| 430 |
return address; |
| 431 |
} |
| 432 |
|
| 433 |
uint32_t Literal32() const { |
| 434 |
uint32_t literal; |
| 435 |
memcpy(&literal, LiteralAddress<const void*>(), sizeof(literal)); |
| 436 |
return literal; |
| 437 |
} |
| 438 |
|
| 439 |
uint64_t Literal64() const { |
| 440 |
uint64_t literal; |
| 441 |
memcpy(&literal, LiteralAddress<const void*>(), sizeof(literal)); |
| 442 |
return literal; |
| 443 |
} |
| 444 |
|
| 445 |
void SetLiteral64(uint64_t literal) const { |
| 446 |
memcpy(LiteralAddress<void*>(), &literal, sizeof(literal)); |
| 447 |
} |
| 448 |
|
| 449 |
float LiteralFP32() const { |
| 450 |
return RawbitsToFloat(Literal32()); |
| 451 |
} |
| 452 |
|
| 453 |
double LiteralFP64() const { |
| 454 |
return RawbitsToDouble(Literal64()); |
| 455 |
} |
| 456 |
|
| 457 |
const Instruction* NextInstruction() const { |
| 458 |
return this + kInstructionSize; |
| 459 |
} |
| 460 |
|
| 461 |
// Skip any constant pools with artificial guards at this point. |
| 462 |
// Return either |this| or the first instruction after the pool. |
| 463 |
const Instruction* skipPool() const; |
| 464 |
|
| 465 |
const Instruction* InstructionAtOffset(int64_t offset) const { |
| 466 |
VIXL_ASSERT(IsWordAligned(this + offset)); |
| 467 |
return this + offset; |
| 468 |
} |
| 469 |
|
| 470 |
template<typename T> static Instruction* Cast(T src) { |
| 471 |
return reinterpret_cast<Instruction*>(src); |
| 472 |
} |
| 473 |
|
| 474 |
template<typename T> static const Instruction* CastConst(T src) { |
| 475 |
return reinterpret_cast<const Instruction*>(src); |
| 476 |
} |
| 477 |
|
| 478 |
private: |
| 479 |
int ImmBranch() const; |
| 480 |
|
| 481 |
static float Imm8ToFP32(uint32_t imm8); |
| 482 |
static double Imm8ToFP64(uint32_t imm8); |
| 483 |
|
| 484 |
void SetPCRelImmTarget(const Instruction* target); |
| 485 |
void SetBranchImmTarget(const Instruction* target); |
| 486 |
}; |
| 487 |
|
| 488 |
|
| 489 |
// Functions for handling NEON vector format information. |
| 490 |
enum VectorFormat { |
| 491 |
kFormatUndefined = 0xffffffff, |
| 492 |
kFormat8B = NEON_8B, |
| 493 |
kFormat16B = NEON_16B, |
| 494 |
kFormat4H = NEON_4H, |
| 495 |
kFormat8H = NEON_8H, |
| 496 |
kFormat2S = NEON_2S, |
| 497 |
kFormat4S = NEON_4S, |
| 498 |
kFormat1D = NEON_1D, |
| 499 |
kFormat2D = NEON_2D, |
| 500 |
|
| 501 |
// Scalar formats. We add the scalar bit to distinguish between scalar and |
| 502 |
// vector enumerations; the bit is always set in the encoding of scalar ops |
| 503 |
// and always clear for vector ops. Although kFormatD and kFormat1D appear |
| 504 |
// to be the same, their meaning is subtly different. The first is a scalar |
| 505 |
// operation, the second a vector operation that only affects one lane. |
| 506 |
kFormatB = NEON_B | NEONScalar, |
| 507 |
kFormatH = NEON_H | NEONScalar, |
| 508 |
kFormatS = NEON_S | NEONScalar, |
| 509 |
kFormatD = NEON_D | NEONScalar |
| 510 |
}; |
| 511 |
|
| 512 |
VectorFormat VectorFormatHalfWidth(const VectorFormat vform); |
| 513 |
VectorFormat VectorFormatDoubleWidth(const VectorFormat vform); |
| 514 |
VectorFormat VectorFormatDoubleLanes(const VectorFormat vform); |
| 515 |
VectorFormat VectorFormatHalfLanes(const VectorFormat vform); |
| 516 |
VectorFormat ScalarFormatFromLaneSize(int lanesize); |
| 517 |
VectorFormat VectorFormatHalfWidthDoubleLanes(const VectorFormat vform); |
| 518 |
VectorFormat VectorFormatFillQ(const VectorFormat vform); |
| 519 |
unsigned RegisterSizeInBitsFromFormat(VectorFormat vform); |
| 520 |
unsigned RegisterSizeInBytesFromFormat(VectorFormat vform); |
| 521 |
// TODO: Make the return types of these functions consistent. |
| 522 |
unsigned LaneSizeInBitsFromFormat(VectorFormat vform); |
| 523 |
int LaneSizeInBytesFromFormat(VectorFormat vform); |
| 524 |
int LaneSizeInBytesLog2FromFormat(VectorFormat vform); |
| 525 |
int LaneCountFromFormat(VectorFormat vform); |
| 526 |
int MaxLaneCountFromFormat(VectorFormat vform); |
| 527 |
bool IsVectorFormat(VectorFormat vform); |
| 528 |
int64_t MaxIntFromFormat(VectorFormat vform); |
| 529 |
int64_t MinIntFromFormat(VectorFormat vform); |
| 530 |
uint64_t MaxUintFromFormat(VectorFormat vform); |
| 531 |
|
| 532 |
|
| 533 |
enum NEONFormat { |
| 534 |
NF_UNDEF = 0, |
| 535 |
NF_8B = 1, |
| 536 |
NF_16B = 2, |
| 537 |
NF_4H = 3, |
| 538 |
NF_8H = 4, |
| 539 |
NF_2S = 5, |
| 540 |
NF_4S = 6, |
| 541 |
NF_1D = 7, |
| 542 |
NF_2D = 8, |
| 543 |
NF_B = 9, |
| 544 |
NF_H = 10, |
| 545 |
NF_S = 11, |
| 546 |
NF_D = 12 |
| 547 |
}; |
| 548 |
|
| 549 |
static const unsigned kNEONFormatMaxBits = 6; |
| 550 |
|
| 551 |
struct NEONFormatMap { |
| 552 |
// The bit positions in the instruction to consider. |
| 553 |
uint8_t bits[kNEONFormatMaxBits]; |
| 554 |
|
| 555 |
// Mapping from concatenated bits to format. |
| 556 |
NEONFormat map[1 << kNEONFormatMaxBits]; |
| 557 |
}; |
| 558 |
|
| 559 |
class NEONFormatDecoder { |
| 560 |
public: |
| 561 |
enum SubstitutionMode { |
| 562 |
kPlaceholder, |
| 563 |
kFormat |
| 564 |
}; |
| 565 |
|
| 566 |
// Construct a format decoder with increasingly specific format maps for each |
| 567 |
// subsitution. If no format map is specified, the default is the integer |
| 568 |
// format map. |
| 569 |
explicit NEONFormatDecoder(const Instruction* instr) { |
| 570 |
instrbits_ = instr->InstructionBits(); |
| 571 |
SetFormatMaps(IntegerFormatMap()); |
| 572 |
} |
| 573 |
NEONFormatDecoder(const Instruction* instr, |
| 574 |
const NEONFormatMap* format) { |
| 575 |
instrbits_ = instr->InstructionBits(); |
| 576 |
SetFormatMaps(format); |
| 577 |
} |
| 578 |
NEONFormatDecoder(const Instruction* instr, |
| 579 |
const NEONFormatMap* format0, |
| 580 |
const NEONFormatMap* format1) { |
| 581 |
instrbits_ = instr->InstructionBits(); |
| 582 |
SetFormatMaps(format0, format1); |
| 583 |
} |
| 584 |
NEONFormatDecoder(const Instruction* instr, |
| 585 |
const NEONFormatMap* format0, |
| 586 |
const NEONFormatMap* format1, |
| 587 |
const NEONFormatMap* format2) { |
| 588 |
instrbits_ = instr->InstructionBits(); |
| 589 |
SetFormatMaps(format0, format1, format2); |
| 590 |
} |
| 591 |
|
| 592 |
// Set the format mapping for all or individual substitutions. |
| 593 |
void SetFormatMaps(const NEONFormatMap* format0, |
| 594 |
const NEONFormatMap* format1 = NULL, |
| 595 |
const NEONFormatMap* format2 = NULL) { |
| 596 |
VIXL_ASSERT(format0 != NULL); |
| 597 |
formats_[0] = format0; |
| 598 |
formats_[1] = (format1 == NULL) ? formats_[0] : format1; |
| 599 |
formats_[2] = (format2 == NULL) ? formats_[1] : format2; |
| 600 |
} |
| 601 |
void SetFormatMap(unsigned index, const NEONFormatMap* format) { |
| 602 |
VIXL_ASSERT(index <= (sizeof(formats_) / sizeof(formats_[0]))); |
| 603 |
VIXL_ASSERT(format != NULL); |
| 604 |
formats_[index] = format; |
| 605 |
} |
| 606 |
|
| 607 |
// Substitute %s in the input string with the placeholder string for each |
| 608 |
// register, ie. "'B", "'H", etc. |
| 609 |
const char* SubstitutePlaceholders(const char* string) { |
| 610 |
return Substitute(string, kPlaceholder, kPlaceholder, kPlaceholder); |
| 611 |
} |
| 612 |
|
| 613 |
// Substitute %s in the input string with a new string based on the |
| 614 |
// substitution mode. |
| 615 |
const char* Substitute(const char* string, |
| 616 |
SubstitutionMode mode0 = kFormat, |
| 617 |
SubstitutionMode mode1 = kFormat, |
| 618 |
SubstitutionMode mode2 = kFormat) { |
| 619 |
snprintf(form_buffer_, sizeof(form_buffer_), string, |
| 620 |
GetSubstitute(0, mode0), |
| 621 |
GetSubstitute(1, mode1), |
| 622 |
GetSubstitute(2, mode2)); |
| 623 |
return form_buffer_; |
| 624 |
} |
| 625 |
|
| 626 |
// Append a "2" to a mnemonic string based of the state of the Q bit. |
| 627 |
const char* Mnemonic(const char* mnemonic) { |
| 628 |
if ((instrbits_ & NEON_Q) != 0) { |
| 629 |
snprintf(mne_buffer_, sizeof(mne_buffer_), "%s2", mnemonic); |
| 630 |
return mne_buffer_; |
| 631 |
} |
| 632 |
return mnemonic; |
| 633 |
} |
| 634 |
|
| 635 |
VectorFormat GetVectorFormat(int format_index = 0) { |
| 636 |
return GetVectorFormat(formats_[format_index]); |
| 637 |
} |
| 638 |
|
| 639 |
VectorFormat GetVectorFormat(const NEONFormatMap* format_map) { |
| 640 |
static const VectorFormat vform[] = { |
| 641 |
kFormatUndefined, |
| 642 |
kFormat8B, kFormat16B, kFormat4H, kFormat8H, |
| 643 |
kFormat2S, kFormat4S, kFormat1D, kFormat2D, |
| 644 |
kFormatB, kFormatH, kFormatS, kFormatD |
| 645 |
}; |
| 646 |
VIXL_ASSERT(GetNEONFormat(format_map) < (sizeof(vform) / sizeof(vform[0]))); |
| 647 |
return vform[GetNEONFormat(format_map)]; |
| 648 |
} |
| 649 |
|
| 650 |
// Built in mappings for common cases. |
| 651 |
|
| 652 |
// The integer format map uses three bits (Q, size<1:0>) to encode the |
| 653 |
// "standard" set of NEON integer vector formats. |
| 654 |
static const NEONFormatMap* IntegerFormatMap() { |
| 655 |
static const NEONFormatMap map = { |
| 656 |
{23, 22, 30}, |
| 657 |
{NF_8B, NF_16B, NF_4H, NF_8H, NF_2S, NF_4S, NF_UNDEF, NF_2D} |
| 658 |
}; |
| 659 |
return ↦ |
| 660 |
} |
| 661 |
|
| 662 |
// The long integer format map uses two bits (size<1:0>) to encode the |
| 663 |
// long set of NEON integer vector formats. These are used in narrow, wide |
| 664 |
// and long operations. |
| 665 |
static const NEONFormatMap* LongIntegerFormatMap() { |
| 666 |
static const NEONFormatMap map = { |
| 667 |
{23, 22}, {NF_8H, NF_4S, NF_2D} |
| 668 |
}; |
| 669 |
return ↦ |
| 670 |
} |
| 671 |
|
| 672 |
// The FP format map uses two bits (Q, size<0>) to encode the NEON FP vector |
| 673 |
// formats: NF_2S, NF_4S, NF_2D. |
| 674 |
static const NEONFormatMap* FPFormatMap() { |
| 675 |
// The FP format map assumes two bits (Q, size<0>) are used to encode the |
| 676 |
// NEON FP vector formats: NF_2S, NF_4S, NF_2D. |
| 677 |
static const NEONFormatMap map = { |
| 678 |
{22, 30}, {NF_2S, NF_4S, NF_UNDEF, NF_2D} |
| 679 |
}; |
| 680 |
return ↦ |
| 681 |
} |
| 682 |
|
| 683 |
// The load/store format map uses three bits (Q, 11, 10) to encode the |
| 684 |
// set of NEON vector formats. |
| 685 |
static const NEONFormatMap* LoadStoreFormatMap() { |
| 686 |
static const NEONFormatMap map = { |
| 687 |
{11, 10, 30}, |
| 688 |
{NF_8B, NF_16B, NF_4H, NF_8H, NF_2S, NF_4S, NF_1D, NF_2D} |
| 689 |
}; |
| 690 |
return ↦ |
| 691 |
} |
| 692 |
|
| 693 |
// The logical format map uses one bit (Q) to encode the NEON vector format: |
| 694 |
// NF_8B, NF_16B. |
| 695 |
static const NEONFormatMap* LogicalFormatMap() { |
| 696 |
static const NEONFormatMap map = { |
| 697 |
{30}, {NF_8B, NF_16B} |
| 698 |
}; |
| 699 |
return ↦ |
| 700 |
} |
| 701 |
|
| 702 |
// The triangular format map uses between two and five bits to encode the NEON |
| 703 |
// vector format: |
| 704 |
// xxx10->8B, xxx11->16B, xx100->4H, xx101->8H |
| 705 |
// x1000->2S, x1001->4S, 10001->2D, all others undefined. |
| 706 |
static const NEONFormatMap* TriangularFormatMap() { |
| 707 |
static const NEONFormatMap map = { |
| 708 |
{19, 18, 17, 16, 30}, |
| 709 |
{NF_UNDEF, NF_UNDEF, NF_8B, NF_16B, NF_4H, NF_8H, NF_8B, NF_16B, NF_2S, |
| 710 |
NF_4S, NF_8B, NF_16B, NF_4H, NF_8H, NF_8B, NF_16B, NF_UNDEF, NF_2D, |
| 711 |
NF_8B, NF_16B, NF_4H, NF_8H, NF_8B, NF_16B, NF_2S, NF_4S, NF_8B, NF_16B, |
| 712 |
NF_4H, NF_8H, NF_8B, NF_16B} |
| 713 |
}; |
| 714 |
return ↦ |
| 715 |
} |
| 716 |
|
| 717 |
// The scalar format map uses two bits (size<1:0>) to encode the NEON scalar |
| 718 |
// formats: NF_B, NF_H, NF_S, NF_D. |
| 719 |
static const NEONFormatMap* ScalarFormatMap() { |
| 720 |
static const NEONFormatMap map = { |
| 721 |
{23, 22}, {NF_B, NF_H, NF_S, NF_D} |
| 722 |
}; |
| 723 |
return ↦ |
| 724 |
} |
| 725 |
|
| 726 |
// The long scalar format map uses two bits (size<1:0>) to encode the longer |
| 727 |
// NEON scalar formats: NF_H, NF_S, NF_D. |
| 728 |
static const NEONFormatMap* LongScalarFormatMap() { |
| 729 |
static const NEONFormatMap map = { |
| 730 |
{23, 22}, {NF_H, NF_S, NF_D} |
| 731 |
}; |
| 732 |
return ↦ |
| 733 |
} |
| 734 |
|
| 735 |
// The FP scalar format map assumes one bit (size<0>) is used to encode the |
| 736 |
// NEON FP scalar formats: NF_S, NF_D. |
| 737 |
static const NEONFormatMap* FPScalarFormatMap() { |
| 738 |
static const NEONFormatMap map = { |
| 739 |
{22}, {NF_S, NF_D} |
| 740 |
}; |
| 741 |
return ↦ |
| 742 |
} |
| 743 |
|
| 744 |
// The triangular scalar format map uses between one and four bits to encode |
| 745 |
// the NEON FP scalar formats: |
| 746 |
// xxx1->B, xx10->H, x100->S, 1000->D, all others undefined. |
| 747 |
static const NEONFormatMap* TriangularScalarFormatMap() { |
| 748 |
static const NEONFormatMap map = { |
| 749 |
{19, 18, 17, 16}, |
| 750 |
{NF_UNDEF, NF_B, NF_H, NF_B, NF_S, NF_B, NF_H, NF_B, |
| 751 |
NF_D, NF_B, NF_H, NF_B, NF_S, NF_B, NF_H, NF_B} |
| 752 |
}; |
| 753 |
return ↦ |
| 754 |
} |
| 755 |
|
| 756 |
private: |
| 757 |
// Get a pointer to a string that represents the format or placeholder for |
| 758 |
// the specified substitution index, based on the format map and instruction. |
| 759 |
const char* GetSubstitute(int index, SubstitutionMode mode) { |
| 760 |
if (mode == kFormat) { |
| 761 |
return NEONFormatAsString(GetNEONFormat(formats_[index])); |
| 762 |
} |
| 763 |
VIXL_ASSERT(mode == kPlaceholder); |
| 764 |
return NEONFormatAsPlaceholder(GetNEONFormat(formats_[index])); |
| 765 |
} |
| 766 |
|
| 767 |
// Get the NEONFormat enumerated value for bits obtained from the |
| 768 |
// instruction based on the specified format mapping. |
| 769 |
NEONFormat GetNEONFormat(const NEONFormatMap* format_map) { |
| 770 |
return format_map->map[PickBits(format_map->bits)]; |
| 771 |
} |
| 772 |
|
| 773 |
// Convert a NEONFormat into a string. |
| 774 |
static const char* NEONFormatAsString(NEONFormat format) { |
| 775 |
static const char* formats[] = { |
| 776 |
"undefined", |
| 777 |
"8b", "16b", "4h", "8h", "2s", "4s", "1d", "2d", |
| 778 |
"b", "h", "s", "d" |
| 779 |
}; |
| 780 |
VIXL_ASSERT(format < (sizeof(formats) / sizeof(formats[0]))); |
| 781 |
return formats[format]; |
| 782 |
} |
| 783 |
|
| 784 |
// Convert a NEONFormat into a register placeholder string. |
| 785 |
static const char* NEONFormatAsPlaceholder(NEONFormat format) { |
| 786 |
VIXL_ASSERT((format == NF_B) || (format == NF_H) || |
| 787 |
(format == NF_S) || (format == NF_D) || |
| 788 |
(format == NF_UNDEF)); |
| 789 |
static const char* formats[] = { |
| 790 |
"undefined", |
| 791 |
"undefined", "undefined", "undefined", "undefined", |
| 792 |
"undefined", "undefined", "undefined", "undefined", |
| 793 |
"'B", "'H", "'S", "'D" |
| 794 |
}; |
| 795 |
return formats[format]; |
| 796 |
} |
| 797 |
|
| 798 |
// Select bits from instrbits_ defined by the bits array, concatenate them, |
| 799 |
// and return the value. |
| 800 |
uint8_t PickBits(const uint8_t bits[]) { |
| 801 |
uint8_t result = 0; |
| 802 |
for (unsigned b = 0; b < kNEONFormatMaxBits; b++) { |
| 803 |
if (bits[b] == 0) break; |
| 804 |
result <<= 1; |
| 805 |
result |= ((instrbits_ & (1 << bits[b])) == 0) ? 0 : 1; |
| 806 |
} |
| 807 |
return result; |
| 808 |
} |
| 809 |
|
| 810 |
Instr instrbits_; |
| 811 |
const NEONFormatMap* formats_[3]; |
| 812 |
char form_buffer_[64]; |
| 813 |
char mne_buffer_[16]; |
| 814 |
}; |
| 815 |
} // namespace vixl |
| 816 |
|
| 817 |
#endif // VIXL_A64_INSTRUCTIONS_A64_H_ |