]> Git Repo - qemu.git/blob - disas/libvixl/a64/instructions-a64.h
ba9068ca8b4acb86d679eb3c9dcc61c473a83299
[qemu.git] / disas / libvixl / a64 / instructions-a64.h
1 // Copyright 2013, 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 "globals.h"
31 #include "utils.h"
32 #include "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 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;
75
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;
81
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);
86
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);
91
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);
95
96 enum LSDataSize {
97   LSByte        = 0,
98   LSHalfword    = 1,
99   LSWord        = 2,
100   LSDoubleWord  = 3
101 };
102
103 LSDataSize CalcLSPairDataSize(LoadStorePairOp op);
104
105 enum ImmBranchType {
106   UnknownBranchType = 0,
107   CondBranchType    = 1,
108   UncondBranchType  = 2,
109   CompareBranchType = 3,
110   TestBranchType    = 4
111 };
112
113 enum AddrMode {
114   Offset,
115   PreIndex,
116   PostIndex
117 };
118
119 enum FPRounding {
120   // The first four values are encodable directly by FPCR<RMode>.
121   FPTieEven = 0x0,
122   FPPositiveInfinity = 0x1,
123   FPNegativeInfinity = 0x2,
124   FPZero = 0x3,
125
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.
128   FPTieAway
129 };
130
131 enum Reg31Mode {
132   Reg31IsStackPointer,
133   Reg31IsZeroRegister
134 };
135
136 // Instructions. ---------------------------------------------------------------
137
138 class Instruction {
139  public:
140   inline Instr InstructionBits() const {
141     return *(reinterpret_cast<const Instr*>(this));
142   }
143
144   inline void SetInstructionBits(Instr new_instr) {
145     *(reinterpret_cast<Instr*>(this)) = new_instr;
146   }
147
148   inline int Bit(int pos) const {
149     return (InstructionBits() >> pos) & 1;
150   }
151
152   inline uint32_t Bits(int msb, int lsb) const {
153     return unsigned_bitextract_32(msb, lsb, InstructionBits());
154   }
155
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);
159   }
160
161   inline Instr Mask(uint32_t mask) const {
162     return InstructionBits() & mask;
163   }
164
165   #define DEFINE_GETTER(Name, HighBit, LowBit, Func)             \
166   inline int64_t Name() const { return Func(HighBit, LowBit); }
167   INSTRUCTION_FIELDS_LIST(DEFINE_GETTER)
168   #undef DEFINE_GETTER
169
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);
176   }
177
178   uint64_t ImmLogical();
179   float ImmFP32();
180   double ImmFP64();
181
182   inline LSDataSize SizeLSPair() const {
183     return CalcLSPairDataSize(
184              static_cast<LoadStorePairOp>(Mask(LoadStorePairMask)));
185   }
186
187   // Helpers.
188   inline bool IsCondBranchImm() const {
189     return Mask(ConditionalBranchFMask) == ConditionalBranchFixed;
190   }
191
192   inline bool IsUncondBranchImm() const {
193     return Mask(UnconditionalBranchFMask) == UnconditionalBranchFixed;
194   }
195
196   inline bool IsCompareBranch() const {
197     return Mask(CompareBranchFMask) == CompareBranchFixed;
198   }
199
200   inline bool IsTestBranch() const {
201     return Mask(TestBranchFMask) == TestBranchFixed;
202   }
203
204   inline bool IsPCRelAddressing() const {
205     return Mask(PCRelAddressingFMask) == PCRelAddressingFixed;
206   }
207
208   inline bool IsLogicalImmediate() const {
209     return Mask(LogicalImmediateFMask) == LogicalImmediateFixed;
210   }
211
212   inline bool IsAddSubImmediate() const {
213     return Mask(AddSubImmediateFMask) == AddSubImmediateFixed;
214   }
215
216   inline bool IsAddSubExtended() const {
217     return Mask(AddSubExtendedFMask) == AddSubExtendedFixed;
218   }
219
220   inline bool IsLoadOrStore() const {
221     return Mask(LoadStoreAnyFMask) == LoadStoreAnyFixed;
222   }
223
224   inline bool IsMovn() const {
225     return (Mask(MoveWideImmediateMask) == MOVN_x) ||
226            (Mask(MoveWideImmediateMask) == MOVN_w);
227   }
228
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;
240       } else {
241         return Reg31IsStackPointer;
242       }
243     }
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;
251       } else {
252         return Reg31IsStackPointer;
253       }
254     }
255     return Reg31IsZeroRegister;
256   }
257
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;
268     }
269     return Reg31IsZeroRegister;
270   }
271
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;
281     } else {
282       return UnknownBranchType;
283     }
284   }
285
286   // Find the target of this instruction. 'this' may be a branch or a
287   // PC-relative addressing instruction.
288   Instruction* ImmPCOffsetTarget();
289
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);
295
296   inline uint8_t* LiteralAddress() {
297     int offset = ImmLLiteral() << kLiteralEntrySizeLog2;
298     return reinterpret_cast<uint8_t*>(this) + offset;
299   }
300
301   inline uint32_t Literal32() {
302     uint32_t literal;
303     memcpy(&literal, LiteralAddress(), sizeof(literal));
304
305     return literal;
306   }
307
308   inline uint64_t Literal64() {
309     uint64_t literal;
310     memcpy(&literal, LiteralAddress(), sizeof(literal));
311
312     return literal;
313   }
314
315   inline float LiteralFP32() {
316     return rawbits_to_float(Literal32());
317   }
318
319   inline double LiteralFP64() {
320     return rawbits_to_double(Literal64());
321   }
322
323   inline Instruction* NextInstruction() {
324     return this + kInstructionSize;
325   }
326
327   inline Instruction* InstructionAtOffset(int64_t offset) {
328     ASSERT(IsWordAligned(this + offset));
329     return this + offset;
330   }
331
332   template<typename T> static inline Instruction* Cast(T src) {
333     return reinterpret_cast<Instruction*>(src);
334   }
335
336  private:
337   inline int ImmBranch() const;
338
339   void SetPCRelImmTarget(Instruction* target);
340   void SetBranchImmTarget(Instruction* target);
341 };
342 }  // namespace vixl
343
344 #endif  // VIXL_A64_INSTRUCTIONS_A64_H_
This page took 0.036932 seconds and 2 git commands to generate.