1
// Copyright 2013, ARM Limited
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
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.
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.
27
#ifndef VIXL_A64_INSTRUCTIONS_A64_H_
28
#define VIXL_A64_INSTRUCTIONS_A64_H_
32
#include "a64/constants-a64.h"
35
// ISA constants. --------------------------------------------------------------
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;
44
const unsigned kWRegSize = 32;
45
const unsigned kWRegSizeLog2 = 5;
46
const unsigned kWRegSizeInBytes = kWRegSize / 8;
47
const unsigned kXRegSize = 64;
48
const unsigned kXRegSizeLog2 = 6;
49
const unsigned kXRegSizeInBytes = kXRegSize / 8;
50
const unsigned kSRegSize = 32;
51
const unsigned kSRegSizeLog2 = 5;
52
const unsigned kSRegSizeInBytes = kSRegSize / 8;
53
const unsigned kDRegSize = 64;
54
const unsigned kDRegSizeLog2 = 6;
55
const unsigned kDRegSizeInBytes = kDRegSize / 8;
56
const int64_t kWRegMask = 0x00000000ffffffffLL;
57
const int64_t kXRegMask = 0xffffffffffffffffLL;
58
const int64_t kSRegMask = 0x00000000ffffffffLL;
59
const int64_t kDRegMask = 0xffffffffffffffffLL;
60
const int64_t kXSignMask = 0x1LL << 63;
61
const int64_t kWSignMask = 0x1LL << 31;
62
const int64_t kByteMask = 0xffL;
63
const int64_t kHalfWordMask = 0xffffL;
64
const int64_t kWordMask = 0xffffffffLL;
65
const uint64_t kXMaxUInt = 0xffffffffffffffffULL;
66
const uint64_t kWMaxUInt = 0xffffffffULL;
67
const int64_t kXMaxInt = 0x7fffffffffffffffLL;
68
const int64_t kXMinInt = 0x8000000000000000LL;
69
const int32_t kWMaxInt = 0x7fffffff;
70
const int32_t kWMinInt = 0x80000000;
71
const unsigned kLinkRegCode = 30;
72
const unsigned kZeroRegCode = 31;
73
const unsigned kSPRegInternalCode = 63;
74
const unsigned kRegCodeMask = 0x1f;
76
// AArch64 floating-point specifics. These match IEEE-754.
77
const unsigned kDoubleMantissaBits = 52;
78
const unsigned kDoubleExponentBits = 11;
79
const unsigned kFloatMantissaBits = 23;
80
const unsigned kFloatExponentBits = 8;
82
const float kFP32PositiveInfinity = rawbits_to_float(0x7f800000);
83
const float kFP32NegativeInfinity = rawbits_to_float(0xff800000);
84
const double kFP64PositiveInfinity = rawbits_to_double(0x7ff0000000000000ULL);
85
const double kFP64NegativeInfinity = rawbits_to_double(0xfff0000000000000ULL);
87
// This value is a signalling NaN as both a double and as a float (taking the
88
// least-significant word).
89
static const double kFP64SignallingNaN = rawbits_to_double(0x7ff000007f800001ULL);
90
static const float kFP32SignallingNaN = rawbits_to_float(0x7f800001);
92
// A similar value, but as a quiet NaN.
93
static const double kFP64QuietNaN = rawbits_to_double(0x7ff800007fc00001ULL);
94
static const float kFP32QuietNaN = rawbits_to_float(0x7fc00001);
103
LSDataSize CalcLSPairDataSize(LoadStorePairOp op);
106
UnknownBranchType = 0,
108
UncondBranchType = 2,
109
CompareBranchType = 3,
120
// The first four values are encodable directly by FPCR<RMode>.
122
FPPositiveInfinity = 0x1,
123
FPNegativeInfinity = 0x2,
126
// The final rounding mode is only available when explicitly specified by the
127
// instruction (such as with fcvta). It cannot be set in FPCR.
136
// Instructions. ---------------------------------------------------------------
140
inline Instr InstructionBits() const {
141
return *(reinterpret_cast<const Instr*>(this));
144
inline void SetInstructionBits(Instr new_instr) {
145
*(reinterpret_cast<Instr*>(this)) = new_instr;
148
inline int Bit(int pos) const {
149
return (InstructionBits() >> pos) & 1;
152
inline uint32_t Bits(int msb, int lsb) const {
153
return unsigned_bitextract_32(msb, lsb, InstructionBits());
156
inline int32_t SignedBits(int msb, int lsb) const {
157
int32_t bits = *(reinterpret_cast<const int32_t*>(this));
158
return signed_bitextract_32(msb, lsb, bits);
161
inline Instr Mask(uint32_t mask) const {
162
return InstructionBits() & mask;
165
#define DEFINE_GETTER(Name, HighBit, LowBit, Func) \
166
inline int64_t Name() const { return Func(HighBit, LowBit); }
167
INSTRUCTION_FIELDS_LIST(DEFINE_GETTER)
170
// ImmPCRel is a compound field (not present in INSTRUCTION_FIELDS_LIST),
171
// formed from ImmPCRelLo and ImmPCRelHi.
172
int ImmPCRel() const {
173
int const offset = ((ImmPCRelHi() << ImmPCRelLo_width) | ImmPCRelLo());
174
int const width = ImmPCRelLo_width + ImmPCRelHi_width;
175
return signed_bitextract_32(width-1, 0, offset);
178
uint64_t ImmLogical();
182
inline LSDataSize SizeLSPair() const {
183
return CalcLSPairDataSize(
184
static_cast<LoadStorePairOp>(Mask(LoadStorePairMask)));
188
inline bool IsCondBranchImm() const {
189
return Mask(ConditionalBranchFMask) == ConditionalBranchFixed;
192
inline bool IsUncondBranchImm() const {
193
return Mask(UnconditionalBranchFMask) == UnconditionalBranchFixed;
196
inline bool IsCompareBranch() const {
197
return Mask(CompareBranchFMask) == CompareBranchFixed;
200
inline bool IsTestBranch() const {
201
return Mask(TestBranchFMask) == TestBranchFixed;
204
inline bool IsPCRelAddressing() const {
205
return Mask(PCRelAddressingFMask) == PCRelAddressingFixed;
208
inline bool IsLogicalImmediate() const {
209
return Mask(LogicalImmediateFMask) == LogicalImmediateFixed;
212
inline bool IsAddSubImmediate() const {
213
return Mask(AddSubImmediateFMask) == AddSubImmediateFixed;
216
inline bool IsAddSubExtended() const {
217
return Mask(AddSubExtendedFMask) == AddSubExtendedFixed;
220
inline bool IsLoadOrStore() const {
221
return Mask(LoadStoreAnyFMask) == LoadStoreAnyFixed;
224
inline bool IsMovn() const {
225
return (Mask(MoveWideImmediateMask) == MOVN_x) ||
226
(Mask(MoveWideImmediateMask) == MOVN_w);
229
// Indicate whether Rd can be the stack pointer or the zero register. This
230
// does not check that the instruction actually has an Rd field.
231
inline Reg31Mode RdMode() const {
232
// The following instructions use sp or wsp as Rd:
233
// Add/sub (immediate) when not setting the flags.
234
// Add/sub (extended) when not setting the flags.
235
// Logical (immediate) when not setting the flags.
236
// Otherwise, r31 is the zero register.
237
if (IsAddSubImmediate() || IsAddSubExtended()) {
238
if (Mask(AddSubSetFlagsBit)) {
239
return Reg31IsZeroRegister;
241
return Reg31IsStackPointer;
244
if (IsLogicalImmediate()) {
245
// Of the logical (immediate) instructions, only ANDS (and its aliases)
246
// can set the flags. The others can all write into sp.
247
// Note that some logical operations are not available to
248
// immediate-operand instructions, so we have to combine two masks here.
249
if (Mask(LogicalImmediateMask & LogicalOpMask) == ANDS) {
250
return Reg31IsZeroRegister;
252
return Reg31IsStackPointer;
255
return Reg31IsZeroRegister;
258
// Indicate whether Rn can be the stack pointer or the zero register. This
259
// does not check that the instruction actually has an Rn field.
260
inline Reg31Mode RnMode() const {
261
// The following instructions use sp or wsp as Rn:
262
// All loads and stores.
263
// Add/sub (immediate).
264
// Add/sub (extended).
265
// Otherwise, r31 is the zero register.
266
if (IsLoadOrStore() || IsAddSubImmediate() || IsAddSubExtended()) {
267
return Reg31IsStackPointer;
269
return Reg31IsZeroRegister;
272
inline ImmBranchType BranchType() const {
273
if (IsCondBranchImm()) {
274
return CondBranchType;
275
} else if (IsUncondBranchImm()) {
276
return UncondBranchType;
277
} else if (IsCompareBranch()) {
278
return CompareBranchType;
279
} else if (IsTestBranch()) {
280
return TestBranchType;
282
return UnknownBranchType;
286
// Find the target of this instruction. 'this' may be a branch or a
287
// PC-relative addressing instruction.
288
Instruction* ImmPCOffsetTarget();
290
// Patch a PC-relative offset to refer to 'target'. 'this' may be a branch or
291
// a PC-relative addressing instruction.
292
void SetImmPCOffsetTarget(Instruction* target);
293
// Patch a literal load instruction to load from 'source'.
294
void SetImmLLiteral(Instruction* source);
296
inline uint8_t* LiteralAddress() {
297
int offset = ImmLLiteral() << kLiteralEntrySizeLog2;
298
return reinterpret_cast<uint8_t*>(this) + offset;
301
inline uint32_t Literal32() {
303
memcpy(&literal, LiteralAddress(), sizeof(literal));
308
inline uint64_t Literal64() {
310
memcpy(&literal, LiteralAddress(), sizeof(literal));
315
inline float LiteralFP32() {
316
return rawbits_to_float(Literal32());
319
inline double LiteralFP64() {
320
return rawbits_to_double(Literal64());
323
inline Instruction* NextInstruction() {
324
return this + kInstructionSize;
327
inline Instruction* InstructionAtOffset(int64_t offset) {
328
ASSERT(IsWordAligned(this + offset));
329
return this + offset;
332
template<typename T> static inline Instruction* Cast(T src) {
333
return reinterpret_cast<Instruction*>(src);
337
inline int ImmBranch() const;
339
void SetPCRelImmTarget(Instruction* target);
340
void SetBranchImmTarget(Instruction* target);
344
#endif // VIXL_A64_INSTRUCTIONS_A64_H_