Coverage Report

Created: 2023-12-08 06:05

/src/capstonenext/arch/AArch64/AArch64Disassembler.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine, http://www.capstone-engine.org */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2022, */
3
/*    Rot127 <unisono@quyllur.org> 2022-2023 */
4
/* Automatically translated source file from LLVM. */
5
6
/* LLVM-commit: <commit> */
7
/* LLVM-tag: <tag> */
8
9
/* Only small edits allowed. */
10
/* For multiple similar edits, please create a Patch for the translator. */
11
12
/* Capstone's C++ file translator: */
13
/* https://github.com/capstone-engine/capstone/tree/next/suite/auto-sync */
14
15
//===- AArch64Disassembler.cpp - Disassembler for AArch64 -----------------===//
16
//
17
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
18
// See https://llvm.org/LICENSE.txt for license information.
19
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
20
//
21
//===----------------------------------------------------------------------===//
22
//
23
//
24
//===----------------------------------------------------------------------===//
25
26
#include <capstone/platform.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
31
#include "../../LEB128.h"
32
#include "../../MCDisassembler.h"
33
#include "../../MCFixedLenDisassembler.h"
34
#include "../../MCInst.h"
35
#include "../../MCInstrDesc.h"
36
#include "../../MCRegisterInfo.h"
37
#include "../../cs_priv.h"
38
#include "../../utils.h"
39
#include "AArch64AddressingModes.h"
40
#include "AArch64BaseInfo.h"
41
#include "AArch64DisassemblerExtension.h"
42
#include "AArch64Linkage.h"
43
#include "AArch64Mapping.h"
44
45
#define GET_INSTRINFO_MC_DESC
46
#include "AArch64GenInstrInfo.inc"
47
48
776
#define CONCAT(a, b) CONCAT_(a, b)
49
776
#define CONCAT_(a, b) a##_##b
50
51
#define DEBUG_TYPE "aarch64-disassembler"
52
53
static bool Check(DecodeStatus *Out, DecodeStatus In)
54
451k
{
55
451k
  switch (In) {
56
448k
  case MCDisassembler_Success:
57
    // Out stays the same.
58
448k
    return true;
59
648
  case MCDisassembler_SoftFail:
60
648
    *Out = In;
61
648
    return true;
62
2.32k
  case MCDisassembler_Fail:
63
2.32k
    *Out = In;
64
2.32k
    return false;
65
0
  default: // never reached
66
0
    return false;
67
451k
  }
68
451k
}
69
70
// Pull DecodeStatus and its enum values into the global namespace.
71
72
// Forward declare these because the autogenerated code will reference them.
73
// Definitions are further down.
74
static DecodeStatus DecodeFPR128RegisterClass(MCInst *Inst, unsigned RegNo,
75
                        uint64_t Address,
76
                        const void *Decoder);
77
static DecodeStatus DecodeFPR128_loRegisterClass(MCInst *Inst, unsigned RegNo,
78
                         uint64_t Address,
79
                         const void *Decoder);
80
static DecodeStatus DecodeFPR64RegisterClass(MCInst *Inst, unsigned RegNo,
81
                       uint64_t Address,
82
                       const void *Decoder);
83
static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, unsigned RegNo,
84
                       uint64_t Address,
85
                       const void *Decoder);
86
static DecodeStatus DecodeFPR16RegisterClass(MCInst *Inst, unsigned RegNo,
87
                       uint64_t Address,
88
                       const void *Decoder);
89
static DecodeStatus DecodeFPR8RegisterClass(MCInst *Inst, unsigned RegNo,
90
                      uint64_t Address,
91
                      const void *Decoder);
92
static DecodeStatus DecodeGPR64commonRegisterClass(MCInst *Inst, unsigned RegNo,
93
                           uint64_t Address,
94
                           const void *Decoder);
95
static DecodeStatus DecodeGPR64RegisterClass(MCInst *Inst, unsigned RegNo,
96
                       uint64_t Address,
97
                       const void *Decoder);
98
static DecodeStatus DecodeGPR64x8ClassRegisterClass(MCInst *Inst,
99
                          unsigned RegNo,
100
                          uint64_t Address,
101
                          const void *Decoder);
102
static DecodeStatus DecodeGPR64spRegisterClass(MCInst *Inst, unsigned RegNo,
103
                         uint64_t Address,
104
                         const void *Decoder);
105
static DecodeStatus
106
DecodeMatrixIndexGPR32_8_11RegisterClass(MCInst *Inst, unsigned RegNo,
107
                     uint64_t Address, const void *Decoder);
108
static DecodeStatus DecodeMatrixIndexGPR32_12_15RegisterClass(
109
  MCInst *Inst, unsigned RegNo, uint64_t Address, const void *Decoder);
110
static DecodeStatus DecodeGPR32RegisterClass(MCInst *Inst, unsigned RegNo,
111
                       uint64_t Address,
112
                       const void *Decoder);
113
static DecodeStatus DecodeGPR32spRegisterClass(MCInst *Inst, unsigned RegNo,
114
                         uint64_t Address,
115
                         const void *Decoder);
116
static DecodeStatus DecodeQQRegisterClass(MCInst *Inst, unsigned RegNo,
117
                      uint64_t Address,
118
                      const void *Decoder);
119
static DecodeStatus DecodeQQQRegisterClass(MCInst *Inst, unsigned RegNo,
120
                       uint64_t Address,
121
                       const void *Decoder);
122
static DecodeStatus DecodeQQQQRegisterClass(MCInst *Inst, unsigned RegNo,
123
                      uint64_t Address,
124
                      const void *Decoder);
125
static DecodeStatus DecodeDDRegisterClass(MCInst *Inst, unsigned RegNo,
126
                      uint64_t Address,
127
                      const void *Decoder);
128
static DecodeStatus DecodeDDDRegisterClass(MCInst *Inst, unsigned RegNo,
129
                       uint64_t Address,
130
                       const void *Decoder);
131
static DecodeStatus DecodeDDDDRegisterClass(MCInst *Inst, unsigned RegNo,
132
                      uint64_t Address,
133
                      const void *Decoder);
134
static DecodeStatus DecodeZPRRegisterClass(MCInst *Inst, unsigned RegNo,
135
                       uint64_t Address,
136
                       const void *Decoder);
137
static DecodeStatus DecodeZPR_4bRegisterClass(MCInst *Inst, unsigned RegNo,
138
                        uint64_t Address,
139
                        const void *Decoder);
140
static DecodeStatus DecodeZPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,
141
                        uint64_t Address,
142
                        const void *Decoder);
143
static DecodeStatus DecodeZPR2RegisterClass(MCInst *Inst, unsigned RegNo,
144
                      uint64_t Address,
145
                      const void *Decoder);
146
static DecodeStatus DecodeZPR3RegisterClass(MCInst *Inst, unsigned RegNo,
147
                      uint64_t Address,
148
                      const void *Decoder);
149
static DecodeStatus DecodeZPR4RegisterClass(MCInst *Inst, unsigned RegNo,
150
                      uint64_t Address,
151
                      const void *Decoder);
152
static DecodeStatus DecodeZPR2Mul2RegisterClass(MCInst *Inst, unsigned RegNo,
153
                        uint64_t Address,
154
                        const void *Decoder);
155
static DecodeStatus DecodeZPR4Mul4RegisterClass(MCInst *Inst, unsigned RegNo,
156
                        uint64_t Address,
157
                        const void *Decoder);
158
static DecodeStatus DecodeZPR2StridedRegisterClass(MCInst *Inst, unsigned RegNo,
159
                           uint64_t Address,
160
                           const void *Decoder);
161
static DecodeStatus DecodeZPR4StridedRegisterClass(MCInst *Inst, unsigned RegNo,
162
                           uint64_t Address,
163
                           const void *Decoder);
164
#define DECLARE_DecodeMatrixTile(NumBitsForTile)                               \
165
  static DecodeStatus CONCAT(DecodeMatrixTile, NumBitsForTile)(              \
166
    MCInst * Inst, unsigned RegNo, uint64_t Address, const void *Decoder);
167
DECLARE_DecodeMatrixTile(2);
168
DECLARE_DecodeMatrixTile(3);
169
DECLARE_DecodeMatrixTile(1);
170
DECLARE_DecodeMatrixTile(4);
171
172
static DecodeStatus DecodeMatrixTileListRegisterClass(MCInst *Inst,
173
                            unsigned RegMask,
174
                            uint64_t Address,
175
                            const void *Decoder);
176
static DecodeStatus DecodePPRRegisterClass(MCInst *Inst, unsigned RegNo,
177
                       uint64_t Address,
178
                       const void *Decoder);
179
static DecodeStatus DecodePPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,
180
                        uint64_t Address,
181
                        const void *Decoder);
182
static DecodeStatus DecodePPR_p8to15RegisterClass(MCInst *Inst, unsigned RegNo,
183
                          uint64_t Address,
184
                          const void *Decoder);
185
static DecodeStatus DecodePPR2RegisterClass(MCInst *Inst, unsigned RegNo,
186
                      uint64_t Address,
187
                      const void *Decoder);
188
static DecodeStatus DecodePPR2Mul2RegisterClass(MCInst *Inst, unsigned RegNo,
189
                        uint64_t Address,
190
                        const void *Decoder);
191
192
static DecodeStatus DecodeFixedPointScaleImm32(MCInst *Inst, unsigned Imm,
193
                         uint64_t Address,
194
                         const void *Decoder);
195
static DecodeStatus DecodeFixedPointScaleImm64(MCInst *Inst, unsigned Imm,
196
                         uint64_t Address,
197
                         const void *Decoder);
198
static DecodeStatus DecodePCRelLabel19(MCInst *Inst, unsigned Imm,
199
                     uint64_t Address, const void *Decoder);
200
static DecodeStatus DecodeMemExtend(MCInst *Inst, unsigned Imm,
201
                  uint64_t Address, const void *Decoder);
202
static DecodeStatus DecodeMRSSystemRegister(MCInst *Inst, unsigned Imm,
203
                      uint64_t Address,
204
                      const void *Decoder);
205
static DecodeStatus DecodeMSRSystemRegister(MCInst *Inst, unsigned Imm,
206
                      uint64_t Address,
207
                      const void *Decoder);
208
static DecodeStatus DecodeThreeAddrSRegInstruction(MCInst *Inst, uint32_t insn,
209
                           uint64_t Address,
210
                           const void *Decoder);
211
static DecodeStatus DecodeMoveImmInstruction(MCInst *Inst, uint32_t insn,
212
                       uint64_t Address,
213
                       const void *Decoder);
214
static DecodeStatus DecodeUnsignedLdStInstruction(MCInst *Inst, uint32_t insn,
215
                          uint64_t Address,
216
                          const void *Decoder);
217
static DecodeStatus DecodeSignedLdStInstruction(MCInst *Inst, uint32_t insn,
218
                        uint64_t Address,
219
                        const void *Decoder);
220
static DecodeStatus DecodeExclusiveLdStInstruction(MCInst *Inst, uint32_t insn,
221
                           uint64_t Address,
222
                           const void *Decoder);
223
static DecodeStatus DecodePairLdStInstruction(MCInst *Inst, uint32_t insn,
224
                        uint64_t Address,
225
                        const void *Decoder);
226
static DecodeStatus DecodeAuthLoadInstruction(MCInst *Inst, uint32_t insn,
227
                        uint64_t Address,
228
                        const void *Decoder);
229
static DecodeStatus DecodeAddSubERegInstruction(MCInst *Inst, uint32_t insn,
230
                        uint64_t Address,
231
                        const void *Decoder);
232
static DecodeStatus DecodeLogicalImmInstruction(MCInst *Inst, uint32_t insn,
233
                        uint64_t Address,
234
                        const void *Decoder);
235
static DecodeStatus DecodeModImmInstruction(MCInst *Inst, uint32_t insn,
236
                      uint64_t Address,
237
                      const void *Decoder);
238
static DecodeStatus DecodeModImmTiedInstruction(MCInst *Inst, uint32_t insn,
239
                        uint64_t Address,
240
                        const void *Decoder);
241
static DecodeStatus DecodeAdrInstruction(MCInst *Inst, uint32_t insn,
242
                     uint64_t Address, const void *Decoder);
243
static DecodeStatus DecodeAddSubImmShift(MCInst *Inst, uint32_t insn,
244
                     uint64_t Address, const void *Decoder);
245
static DecodeStatus DecodeUnconditionalBranch(MCInst *Inst, uint32_t insn,
246
                        uint64_t Address,
247
                        const void *Decoder);
248
static DecodeStatus DecodeSystemPStateImm0_15Instruction(MCInst *Inst,
249
                             uint32_t insn,
250
                             uint64_t Address,
251
                             const void *Decoder);
252
static DecodeStatus DecodeSystemPStateImm0_1Instruction(MCInst *Inst,
253
                            uint32_t insn,
254
                            uint64_t Address,
255
                            const void *Decoder);
256
static DecodeStatus DecodeTestAndBranch(MCInst *Inst, uint32_t insn,
257
                    uint64_t Address, const void *Decoder);
258
259
static DecodeStatus DecodeFMOVLaneInstruction(MCInst *Inst, unsigned Insn,
260
                        uint64_t Address,
261
                        const void *Decoder);
262
static DecodeStatus DecodeVecShiftR64Imm(MCInst *Inst, unsigned Imm,
263
                     uint64_t Addr, const void *Decoder);
264
static DecodeStatus DecodeVecShiftR64ImmNarrow(MCInst *Inst, unsigned Imm,
265
                         uint64_t Addr,
266
                         const void *Decoder);
267
static DecodeStatus DecodeVecShiftR32Imm(MCInst *Inst, unsigned Imm,
268
                     uint64_t Addr, const void *Decoder);
269
static DecodeStatus DecodeVecShiftR32ImmNarrow(MCInst *Inst, unsigned Imm,
270
                         uint64_t Addr,
271
                         const void *Decoder);
272
static DecodeStatus DecodeVecShiftR16Imm(MCInst *Inst, unsigned Imm,
273
                     uint64_t Addr, const void *Decoder);
274
static DecodeStatus DecodeVecShiftR16ImmNarrow(MCInst *Inst, unsigned Imm,
275
                         uint64_t Addr,
276
                         const void *Decoder);
277
static DecodeStatus DecodeVecShiftR8Imm(MCInst *Inst, unsigned Imm,
278
                    uint64_t Addr, const void *Decoder);
279
static DecodeStatus DecodeVecShiftL64Imm(MCInst *Inst, unsigned Imm,
280
                     uint64_t Addr, const void *Decoder);
281
static DecodeStatus DecodeVecShiftL32Imm(MCInst *Inst, unsigned Imm,
282
                     uint64_t Addr, const void *Decoder);
283
static DecodeStatus DecodeVecShiftL16Imm(MCInst *Inst, unsigned Imm,
284
                     uint64_t Addr, const void *Decoder);
285
static DecodeStatus DecodeVecShiftL8Imm(MCInst *Inst, unsigned Imm,
286
                    uint64_t Addr, const void *Decoder);
287
static DecodeStatus DecodeWSeqPairsClassRegisterClass(MCInst *Inst,
288
                            unsigned RegNo,
289
                            uint64_t Addr,
290
                            const void *Decoder);
291
static DecodeStatus DecodeXSeqPairsClassRegisterClass(MCInst *Inst,
292
                            unsigned RegNo,
293
                            uint64_t Addr,
294
                            const void *Decoder);
295
static DecodeStatus DecodeSyspXzrInstruction(MCInst *Inst, uint32_t insn,
296
                       uint64_t Addr,
297
                       const void *Decoder);
298
static DecodeStatus DecodeSVELogicalImmInstruction(MCInst *Inst, uint32_t insn,
299
                           uint64_t Address,
300
                           const void *Decoder);
301
#define DECLARE_DecodeSImm(Bits)                                               \
302
  static DecodeStatus CONCAT(DecodeSImm, Bits)(                              \
303
    MCInst * Inst, uint64_t Imm, uint64_t Address, const void *Decoder);
304
DECLARE_DecodeSImm(4);
305
DECLARE_DecodeSImm(5);
306
DECLARE_DecodeSImm(6);
307
DECLARE_DecodeSImm(8);
308
DECLARE_DecodeSImm(9);
309
DECLARE_DecodeSImm(10);
310
311
#define DECLARE_DecodeImm8OptLsl(ElementWidth)                                 \
312
  static DecodeStatus CONCAT(DecodeImm8OptLsl, ElementWidth)(                \
313
    MCInst * Inst, unsigned Imm, uint64_t Addr, const void *Decoder);
314
DECLARE_DecodeImm8OptLsl(8);
315
DECLARE_DecodeImm8OptLsl(16);
316
DECLARE_DecodeImm8OptLsl(32);
317
DECLARE_DecodeImm8OptLsl(64);
318
319
static DecodeStatus DecodeSVEIncDecImm(MCInst *Inst, unsigned Imm,
320
                     uint64_t Addr, const void *Decoder);
321
static DecodeStatus DecodeSVCROp(MCInst *Inst, unsigned Imm, uint64_t Address,
322
                 const void *Decoder);
323
static DecodeStatus DecodeCPYMemOpInstruction(MCInst *Inst, uint32_t insn,
324
                        uint64_t Addr,
325
                        const void *Decoder);
326
static DecodeStatus DecodeSETMemOpInstruction(MCInst *Inst, uint32_t insn,
327
                        uint64_t Addr,
328
                        const void *Decoder);
329
static DecodeStatus DecodePRFMRegInstruction(MCInst *Inst, uint32_t insn,
330
                       uint64_t Address,
331
                       const void *Decoder);
332
333
#include "AArch64GenDisassemblerTables.inc"
334
335
596k
#define Success MCDisassembler_Success
336
5.31k
#define Fail MCDisassembler_Fail
337
648
#define SoftFail MCDisassembler_SoftFail
338
339
static DecodeStatus getInstruction(csh handle, const uint8_t *Bytes, size_t ByteLen,
340
              MCInst *MI, uint16_t *Size, uint64_t Address,
341
              void *Info)
342
211k
{
343
211k
  *Size = 0;
344
  // We want to read exactly 4 bytes of data.
345
211k
  if (ByteLen < 4)
346
2.99k
    return Fail;
347
208k
  *Size = 4;
348
349
  // Encoded as a small-endian 32-bit word in the stream.
350
208k
  uint32_t Insn = readBytes32(MI, Bytes);
351
352
208k
  const uint8_t *Tables[] = {DecoderTable32, DecoderTableFallback32};
353
354
218k
  for (int i = 0; i < (sizeof(Tables) / sizeof(Tables[0])); ++i) {
355
214k
    void *Decoder = NULL;
356
214k
    DecodeStatus Result = decodeInstruction_4(Tables[i], MI, Insn, Address, Decoder);
357
358
214k
    const MCInstrDesc Desc = AArch64Insts[MCInst_getOpcode(MI)];
359
360
    // For Scalable Matrix Extension (SME) instructions that have an
361
    // implicit operand for the accumulator (ZA) or implicit immediate zero
362
    // which isn't encoded, manually insert operand.
363
912k
    for (unsigned i = 0; i < Desc.NumOperands; i++) {
364
697k
      if (Desc.OpInfo[i].OperandType == MCOI_OPERAND_REGISTER) {
365
500k
        switch (Desc.OpInfo[i].RegClass) {
366
495k
        default:
367
495k
          break;
368
495k
        case AArch64_MPRRegClassID:
369
3.52k
          MCInst_insert0(MI, i, MCOperand_CreateReg1(MI, AArch64_ZA));
370
3.52k
          break;
371
427
        case AArch64_MPR8RegClassID:
372
427
          MCInst_insert0(MI, i,
373
427
                MCOperand_CreateReg1(MI, AArch64_ZAB0));
374
427
          break;
375
249
        case AArch64_ZTRRegClassID:
376
249
          MCInst_insert0(MI, i, MCOperand_CreateReg1(MI, AArch64_ZT0));
377
249
          break;
378
500k
        }
379
500k
      } else if (Desc.OpInfo[i].OperandType ==
380
197k
             AArch64_OP_IMPLICIT_IMM_0) {
381
1.66k
        MCInst_insert0(MI, i, MCOperand_CreateImm1(MI, 0));
382
1.66k
      }
383
697k
    }
384
385
214k
    if (MCInst_getOpcode(MI) == AArch64_LDR_ZA ||
386
214k
      MCInst_getOpcode(MI) == AArch64_STR_ZA) {
387
      // Spill and fill instructions have a single immediate used for both
388
      // the vector select offset and optional memory offset. Replicate
389
      // the decoded immediate.
390
45
      MCOperand *Imm4Op = MCInst_getOperand(MI, (2));
391
392
45
      MCInst_addOperand2(MI, (Imm4Op));
393
45
    }
394
395
214k
    if (Result != MCDisassembler_Fail)
396
204k
      return Result;
397
214k
  }
398
399
3.90k
  return MCDisassembler_Fail;
400
208k
}
401
402
DecodeStatus AArch64_LLVM_getInstruction(csh handle, const uint8_t *Bytes,
403
                                         size_t ByteLen,  MCInst *MI, uint16_t *Size, uint64_t Address,
404
211k
                                         void *Info) {
405
211k
  DecodeStatus Result = MCDisassembler_Fail;
406
211k
  Result = getInstruction(handle, Bytes, ByteLen, MI, Size, Address, Info);
407
211k
  MCInst_handleWriteback(MI, AArch64Insts);
408
211k
  return Result;
409
211k
}
410
411
uint64_t suggestBytesToSkip(const uint8_t *Bytes, uint64_t Address)
412
0
{
413
  // AArch64 instructions are always 4 bytes wide, so there's no point
414
  // in skipping any smaller number of bytes if an instruction can't
415
  // be decoded.
416
0
  return 4;
417
0
}
418
419
static DecodeStatus DecodeFPR128RegisterClass(MCInst *Inst, unsigned RegNo,
420
                        uint64_t Addr,
421
                        const void *Decoder)
422
42.5k
{
423
42.5k
  if (RegNo > 31)
424
0
    return Fail;
425
426
42.5k
  unsigned Register =
427
42.5k
    AArch64MCRegisterClasses[AArch64_FPR128RegClassID].RegsBegin[RegNo];
428
42.5k
  MCOperand_CreateReg0(Inst, (Register));
429
42.5k
  return Success;
430
42.5k
}
431
432
static DecodeStatus DecodeFPR128_loRegisterClass(MCInst *Inst, unsigned RegNo,
433
                         uint64_t Addr,
434
                         const void *Decoder)
435
1.57k
{
436
1.57k
  if (RegNo > 15)
437
0
    return Fail;
438
1.57k
  return DecodeFPR128RegisterClass(Inst, RegNo, Addr, Decoder);
439
1.57k
}
440
441
static DecodeStatus DecodeFPR64RegisterClass(MCInst *Inst, unsigned RegNo,
442
                       uint64_t Addr, const void *Decoder)
443
24.6k
{
444
24.6k
  if (RegNo > 31)
445
0
    return Fail;
446
447
24.6k
  unsigned Register =
448
24.6k
    AArch64MCRegisterClasses[AArch64_FPR64RegClassID].RegsBegin[RegNo];
449
24.6k
  MCOperand_CreateReg0(Inst, (Register));
450
24.6k
  return Success;
451
24.6k
}
452
453
static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, unsigned RegNo,
454
                       uint64_t Addr, const void *Decoder)
455
17.4k
{
456
17.4k
  if (RegNo > 31)
457
0
    return Fail;
458
459
17.4k
  unsigned Register =
460
17.4k
    AArch64MCRegisterClasses[AArch64_FPR32RegClassID].RegsBegin[RegNo];
461
17.4k
  MCOperand_CreateReg0(Inst, (Register));
462
17.4k
  return Success;
463
17.4k
}
464
465
static DecodeStatus DecodeFPR16RegisterClass(MCInst *Inst, unsigned RegNo,
466
                       uint64_t Addr, const void *Decoder)
467
5.16k
{
468
5.16k
  if (RegNo > 31)
469
0
    return Fail;
470
471
5.16k
  unsigned Register =
472
5.16k
    AArch64MCRegisterClasses[AArch64_FPR16RegClassID].RegsBegin[RegNo];
473
5.16k
  MCOperand_CreateReg0(Inst, (Register));
474
5.16k
  return Success;
475
5.16k
}
476
477
static DecodeStatus DecodeFPR8RegisterClass(MCInst *Inst, unsigned RegNo,
478
                      uint64_t Addr, const void *Decoder)
479
2.46k
{
480
2.46k
  if (RegNo > 31)
481
0
    return Fail;
482
483
2.46k
  unsigned Register =
484
2.46k
    AArch64MCRegisterClasses[AArch64_FPR8RegClassID].RegsBegin[RegNo];
485
2.46k
  MCOperand_CreateReg0(Inst, (Register));
486
2.46k
  return Success;
487
2.46k
}
488
489
static DecodeStatus DecodeGPR64commonRegisterClass(MCInst *Inst, unsigned RegNo,
490
                           uint64_t Addr,
491
                           const void *Decoder)
492
1.88k
{
493
1.88k
  if (RegNo > 30)
494
12
    return Fail;
495
496
1.87k
  unsigned Register = AArch64MCRegisterClasses[AArch64_GPR64commonRegClassID]
497
1.87k
              .RegsBegin[RegNo];
498
1.87k
  MCOperand_CreateReg0(Inst, (Register));
499
1.87k
  return Success;
500
1.88k
}
501
502
static DecodeStatus DecodeGPR64RegisterClass(MCInst *Inst, unsigned RegNo,
503
                       uint64_t Addr, const void *Decoder)
504
74.1k
{
505
74.1k
  if (RegNo > 31)
506
0
    return Fail;
507
508
74.1k
  unsigned Register =
509
74.1k
    AArch64MCRegisterClasses[AArch64_GPR64RegClassID].RegsBegin[RegNo];
510
74.1k
  MCOperand_CreateReg0(Inst, (Register));
511
74.1k
  return Success;
512
74.1k
}
513
514
static DecodeStatus DecodeGPR64x8ClassRegisterClass(MCInst *Inst,
515
                          unsigned RegNo,
516
                          uint64_t Address,
517
                          const void *Decoder)
518
68
{
519
68
  if (RegNo > 22)
520
2
    return Fail;
521
66
  if (RegNo & 1)
522
2
    return Fail;
523
524
64
  unsigned Register = AArch64MCRegisterClasses[AArch64_GPR64x8ClassRegClassID]
525
64
              .RegsBegin[RegNo >> 1];
526
64
  MCOperand_CreateReg0(Inst, (Register));
527
64
  return Success;
528
66
}
529
530
static DecodeStatus DecodeGPR64spRegisterClass(MCInst *Inst, unsigned RegNo,
531
                         uint64_t Addr,
532
                         const void *Decoder)
533
92.3k
{
534
92.3k
  if (RegNo > 31)
535
0
    return Fail;
536
92.3k
  unsigned Register =
537
92.3k
    AArch64MCRegisterClasses[AArch64_GPR64spRegClassID].RegsBegin[RegNo];
538
92.3k
  MCOperand_CreateReg0(Inst, (Register));
539
92.3k
  return Success;
540
92.3k
}
541
542
static DecodeStatus
543
DecodeMatrixIndexGPR32_8_11RegisterClass(MCInst *Inst, unsigned RegNo,
544
                     uint64_t Addr, const void *Decoder)
545
1.74k
{
546
1.74k
  if (RegNo > 3)
547
0
    return Fail;
548
549
1.74k
  unsigned Register =
550
1.74k
    AArch64MCRegisterClasses[AArch64_MatrixIndexGPR32_8_11RegClassID]
551
1.74k
      .RegsBegin[RegNo];
552
1.74k
  MCOperand_CreateReg0(Inst, (Register));
553
1.74k
  return Success;
554
1.74k
}
555
556
static DecodeStatus
557
DecodeMatrixIndexGPR32_12_15RegisterClass(MCInst *Inst, unsigned RegNo,
558
                      uint64_t Addr, const void *Decoder)
559
3.40k
{
560
3.40k
  if (RegNo > 3)
561
0
    return Fail;
562
563
3.40k
  unsigned Register =
564
3.40k
    AArch64MCRegisterClasses[AArch64_MatrixIndexGPR32_12_15RegClassID]
565
3.40k
      .RegsBegin[RegNo];
566
3.40k
  MCOperand_CreateReg0(Inst, (Register));
567
3.40k
  return Success;
568
3.40k
}
569
570
static DecodeStatus DecodeGPR32RegisterClass(MCInst *Inst, unsigned RegNo,
571
                       uint64_t Addr, const void *Decoder)
572
52.9k
{
573
52.9k
  if (RegNo > 31)
574
0
    return Fail;
575
576
52.9k
  unsigned Register =
577
52.9k
    AArch64MCRegisterClasses[AArch64_GPR32RegClassID].RegsBegin[RegNo];
578
52.9k
  MCOperand_CreateReg0(Inst, (Register));
579
52.9k
  return Success;
580
52.9k
}
581
582
static DecodeStatus DecodeGPR32spRegisterClass(MCInst *Inst, unsigned RegNo,
583
                         uint64_t Addr,
584
                         const void *Decoder)
585
4.80k
{
586
4.80k
  if (RegNo > 31)
587
0
    return Fail;
588
589
4.80k
  unsigned Register =
590
4.80k
    AArch64MCRegisterClasses[AArch64_GPR32spRegClassID].RegsBegin[RegNo];
591
4.80k
  MCOperand_CreateReg0(Inst, (Register));
592
4.80k
  return Success;
593
4.80k
}
594
595
static DecodeStatus DecodeZPRRegisterClass(MCInst *Inst, unsigned RegNo,
596
                       uint64_t Address,
597
                       const void *Decoder)
598
97.2k
{
599
97.2k
  if (RegNo > 31)
600
0
    return Fail;
601
602
97.2k
  unsigned Register =
603
97.2k
    AArch64MCRegisterClasses[AArch64_ZPRRegClassID].RegsBegin[RegNo];
604
97.2k
  MCOperand_CreateReg0(Inst, (Register));
605
97.2k
  return Success;
606
97.2k
}
607
608
static DecodeStatus DecodeZPR_4bRegisterClass(MCInst *Inst, unsigned RegNo,
609
                        uint64_t Address,
610
                        const void *Decoder)
611
1.79k
{
612
1.79k
  if (RegNo > 15)
613
0
    return Fail;
614
1.79k
  return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);
615
1.79k
}
616
617
static DecodeStatus DecodeZPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,
618
                        uint64_t Address,
619
                        const void *Decoder)
620
747
{
621
747
  if (RegNo > 7)
622
0
    return Fail;
623
747
  return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);
624
747
}
625
626
static DecodeStatus DecodeZPR2RegisterClass(MCInst *Inst, unsigned RegNo,
627
                      uint64_t Address,
628
                      const void *Decoder)
629
1.74k
{
630
1.74k
  if (RegNo > 31)
631
0
    return Fail;
632
1.74k
  unsigned Register =
633
1.74k
    AArch64MCRegisterClasses[AArch64_ZPR2RegClassID].RegsBegin[RegNo];
634
1.74k
  MCOperand_CreateReg0(Inst, (Register));
635
1.74k
  return Success;
636
1.74k
}
637
638
static DecodeStatus DecodeZPR3RegisterClass(MCInst *Inst, unsigned RegNo,
639
                      uint64_t Address,
640
                      const void *Decoder)
641
278
{
642
278
  if (RegNo > 31)
643
0
    return Fail;
644
278
  unsigned Register =
645
278
    AArch64MCRegisterClasses[AArch64_ZPR3RegClassID].RegsBegin[RegNo];
646
278
  MCOperand_CreateReg0(Inst, (Register));
647
278
  return Success;
648
278
}
649
650
static DecodeStatus DecodeZPR4RegisterClass(MCInst *Inst, unsigned RegNo,
651
                      uint64_t Address,
652
                      const void *Decoder)
653
723
{
654
723
  if (RegNo > 31)
655
0
    return Fail;
656
723
  unsigned Register =
657
723
    AArch64MCRegisterClasses[AArch64_ZPR4RegClassID].RegsBegin[RegNo];
658
723
  MCOperand_CreateReg0(Inst, (Register));
659
723
  return Success;
660
723
}
661
662
static DecodeStatus DecodeZPR2Mul2RegisterClass(MCInst *Inst, unsigned RegNo,
663
                        uint64_t Address,
664
                        const void *Decoder)
665
1.49k
{
666
1.49k
  if (RegNo * 2 > 30)
667
0
    return Fail;
668
1.49k
  unsigned Register =
669
1.49k
    AArch64MCRegisterClasses[AArch64_ZPR2RegClassID].RegsBegin[RegNo * 2];
670
1.49k
  MCOperand_CreateReg0(Inst, (Register));
671
1.49k
  return Success;
672
1.49k
}
673
674
static DecodeStatus DecodeZPR4Mul4RegisterClass(MCInst *Inst, unsigned RegNo,
675
                        uint64_t Address,
676
                        const void *Decoder)
677
2.36k
{
678
2.36k
  if (RegNo * 4 > 28)
679
0
    return Fail;
680
2.36k
  unsigned Register =
681
2.36k
    AArch64MCRegisterClasses[AArch64_ZPR4RegClassID].RegsBegin[RegNo * 4];
682
2.36k
  MCOperand_CreateReg0(Inst, (Register));
683
2.36k
  return Success;
684
2.36k
}
685
686
static DecodeStatus DecodeZPR2StridedRegisterClass(MCInst *Inst, unsigned RegNo,
687
                           uint64_t Address,
688
                           const void *Decoder)
689
871
{
690
871
  if (RegNo > 15)
691
0
    return Fail;
692
871
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR2StridedRegClassID]
693
871
              .RegsBegin[RegNo];
694
871
  MCOperand_CreateReg0(Inst, (Register));
695
871
  return Success;
696
871
}
697
698
static DecodeStatus DecodeZPR4StridedRegisterClass(MCInst *Inst, unsigned RegNo,
699
                           uint64_t Address,
700
                           const void *Decoder)
701
477
{
702
477
  if (RegNo > 7)
703
0
    return Fail;
704
477
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR4StridedRegClassID]
705
477
              .RegsBegin[RegNo];
706
477
  MCOperand_CreateReg0(Inst, (Register));
707
477
  return Success;
708
477
}
709
710
static DecodeStatus DecodeMatrixTileListRegisterClass(MCInst *Inst,
711
                            unsigned RegMask,
712
                            uint64_t Address,
713
                            const void *Decoder)
714
47
{
715
47
  if (RegMask > 0xFF)
716
0
    return Fail;
717
47
  MCOperand_CreateImm0(Inst, (RegMask));
718
47
  return Success;
719
47
}
720
721
static const unsigned
722
  MatrixZATileDecoderTable[5][16] = {
723
    {AArch64_ZAB0},
724
    {AArch64_ZAH0, AArch64_ZAH1},
725
    {AArch64_ZAS0, AArch64_ZAS1, AArch64_ZAS2, AArch64_ZAS3},
726
    {AArch64_ZAD0, AArch64_ZAD1, AArch64_ZAD2, AArch64_ZAD3, AArch64_ZAD4,
727
     AArch64_ZAD5, AArch64_ZAD6, AArch64_ZAD7},
728
    {AArch64_ZAQ0, AArch64_ZAQ1, AArch64_ZAQ2, AArch64_ZAQ3, AArch64_ZAQ4,
729
     AArch64_ZAQ5, AArch64_ZAQ6, AArch64_ZAQ7, AArch64_ZAQ8, AArch64_ZAQ9,
730
     AArch64_ZAQ10, AArch64_ZAQ11, AArch64_ZAQ12, AArch64_ZAQ13,
731
     AArch64_ZAQ14, AArch64_ZAQ15}};
732
733
#define DEFINE_DecodeMatrixTile(NumBitsForTile)                                \
734
  static DecodeStatus CONCAT(DecodeMatrixTile, NumBitsForTile)(              \
735
    MCInst * Inst, unsigned RegNo, uint64_t Address, const void *Decoder)  \
736
6.18k
  {                                                                          \
737
6.18k
    unsigned LastReg = (1 << NumBitsForTile) - 1;                          \
738
6.18k
    if (RegNo > LastReg)                                                   \
739
6.18k
      return Fail;                                                       \
740
6.18k
    MCOperand_CreateReg0(                                                  \
741
6.18k
      Inst, (MatrixZATileDecoderTable[NumBitsForTile][RegNo]));          \
742
6.18k
    return Success;                                                        \
743
6.18k
  }
AArch64Disassembler.c:DecodeMatrixTile_2
Line
Count
Source
736
2.50k
  {                                                                          \
737
2.50k
    unsigned LastReg = (1 << NumBitsForTile) - 1;                          \
738
2.50k
    if (RegNo > LastReg)                                                   \
739
2.50k
      return Fail;                                                       \
740
2.50k
    MCOperand_CreateReg0(                                                  \
741
2.50k
      Inst, (MatrixZATileDecoderTable[NumBitsForTile][RegNo]));          \
742
2.50k
    return Success;                                                        \
743
2.50k
  }
AArch64Disassembler.c:DecodeMatrixTile_3
Line
Count
Source
736
2.41k
  {                                                                          \
737
2.41k
    unsigned LastReg = (1 << NumBitsForTile) - 1;                          \
738
2.41k
    if (RegNo > LastReg)                                                   \
739
2.41k
      return Fail;                                                       \
740
2.41k
    MCOperand_CreateReg0(                                                  \
741
2.41k
      Inst, (MatrixZATileDecoderTable[NumBitsForTile][RegNo]));          \
742
2.41k
    return Success;                                                        \
743
2.41k
  }
AArch64Disassembler.c:DecodeMatrixTile_1
Line
Count
Source
736
631
  {                                                                          \
737
631
    unsigned LastReg = (1 << NumBitsForTile) - 1;                          \
738
631
    if (RegNo > LastReg)                                                   \
739
631
      return Fail;                                                       \
740
631
    MCOperand_CreateReg0(                                                  \
741
631
      Inst, (MatrixZATileDecoderTable[NumBitsForTile][RegNo]));          \
742
631
    return Success;                                                        \
743
631
  }
AArch64Disassembler.c:DecodeMatrixTile_4
Line
Count
Source
736
638
  {                                                                          \
737
638
    unsigned LastReg = (1 << NumBitsForTile) - 1;                          \
738
638
    if (RegNo > LastReg)                                                   \
739
638
      return Fail;                                                       \
740
638
    MCOperand_CreateReg0(                                                  \
741
638
      Inst, (MatrixZATileDecoderTable[NumBitsForTile][RegNo]));          \
742
638
    return Success;                                                        \
743
638
  }
744
DEFINE_DecodeMatrixTile(2);
745
DEFINE_DecodeMatrixTile(3);
746
DEFINE_DecodeMatrixTile(1);
747
DEFINE_DecodeMatrixTile(4);
748
749
static DecodeStatus DecodePPRRegisterClass(MCInst *Inst, unsigned RegNo,
750
                       uint64_t Addr, const void *Decoder)
751
50.2k
{
752
50.2k
  if (RegNo > 15)
753
0
    return Fail;
754
755
50.2k
  unsigned Register =
756
50.2k
    AArch64MCRegisterClasses[AArch64_PPRRegClassID].RegsBegin[RegNo];
757
50.2k
  MCOperand_CreateReg0(Inst, (Register));
758
50.2k
  return Success;
759
50.2k
}
760
761
static DecodeStatus DecodePPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,
762
                        uint64_t Addr,
763
                        const void *Decoder)
764
33.9k
{
765
33.9k
  if (RegNo > 7)
766
0
    return Fail;
767
768
  // Just reuse the PPR decode table
769
33.9k
  return DecodePPRRegisterClass(Inst, RegNo, Addr, Decoder);
770
33.9k
}
771
772
static DecodeStatus DecodePPR_p8to15RegisterClass(MCInst *Inst, unsigned RegNo,
773
                          uint64_t Addr,
774
                          const void *Decoder)
775
2.04k
{
776
2.04k
  if (RegNo > 7)
777
0
    return Fail;
778
779
  // Just reuse the PPR decode table
780
2.04k
  return DecodePPRRegisterClass(Inst, RegNo + 8, Addr, Decoder);
781
2.04k
}
782
783
static DecodeStatus DecodePPR2RegisterClass(MCInst *Inst, unsigned RegNo,
784
                      uint64_t Address,
785
                      const void *Decoder)
786
250
{
787
250
  if (RegNo > 15)
788
0
    return Fail;
789
790
250
  unsigned Register =
791
250
    AArch64MCRegisterClasses[AArch64_PPR2RegClassID].RegsBegin[RegNo];
792
250
  MCOperand_CreateReg0(Inst, (Register));
793
250
  return Success;
794
250
}
795
796
static DecodeStatus DecodePPR2Mul2RegisterClass(MCInst *Inst, unsigned RegNo,
797
                        uint64_t Address,
798
                        const void *Decoder)
799
140
{
800
140
  if ((RegNo * 2) > 14)
801
0
    return Fail;
802
140
  unsigned Register =
803
140
    AArch64MCRegisterClasses[AArch64_PPR2RegClassID].RegsBegin[RegNo * 2];
804
140
  MCOperand_CreateReg0(Inst, (Register));
805
140
  return Success;
806
140
}
807
808
static DecodeStatus DecodeQQRegisterClass(MCInst *Inst, unsigned RegNo,
809
                      uint64_t Addr, const void *Decoder)
810
2.77k
{
811
2.77k
  if (RegNo > 31)
812
0
    return Fail;
813
2.77k
  unsigned Register =
814
2.77k
    AArch64MCRegisterClasses[AArch64_QQRegClassID].RegsBegin[RegNo];
815
2.77k
  MCOperand_CreateReg0(Inst, (Register));
816
2.77k
  return Success;
817
2.77k
}
818
819
static DecodeStatus DecodeQQQRegisterClass(MCInst *Inst, unsigned RegNo,
820
                       uint64_t Addr, const void *Decoder)
821
4.56k
{
822
4.56k
  if (RegNo > 31)
823
0
    return Fail;
824
4.56k
  unsigned Register =
825
4.56k
    AArch64MCRegisterClasses[AArch64_QQQRegClassID].RegsBegin[RegNo];
826
4.56k
  MCOperand_CreateReg0(Inst, (Register));
827
4.56k
  return Success;
828
4.56k
}
829
830
static DecodeStatus DecodeQQQQRegisterClass(MCInst *Inst, unsigned RegNo,
831
                      uint64_t Addr, const void *Decoder)
832
4.74k
{
833
4.74k
  if (RegNo > 31)
834
0
    return Fail;
835
4.74k
  unsigned Register =
836
4.74k
    AArch64MCRegisterClasses[AArch64_QQQQRegClassID].RegsBegin[RegNo];
837
4.74k
  MCOperand_CreateReg0(Inst, (Register));
838
4.74k
  return Success;
839
4.74k
}
840
841
static DecodeStatus DecodeDDRegisterClass(MCInst *Inst, unsigned RegNo,
842
                      uint64_t Addr, const void *Decoder)
843
810
{
844
810
  if (RegNo > 31)
845
0
    return Fail;
846
810
  unsigned Register =
847
810
    AArch64MCRegisterClasses[AArch64_DDRegClassID].RegsBegin[RegNo];
848
810
  MCOperand_CreateReg0(Inst, (Register));
849
810
  return Success;
850
810
}
851
852
static DecodeStatus DecodeDDDRegisterClass(MCInst *Inst, unsigned RegNo,
853
                       uint64_t Addr, const void *Decoder)
854
1.11k
{
855
1.11k
  if (RegNo > 31)
856
0
    return Fail;
857
1.11k
  unsigned Register =
858
1.11k
    AArch64MCRegisterClasses[AArch64_DDDRegClassID].RegsBegin[RegNo];
859
1.11k
  MCOperand_CreateReg0(Inst, (Register));
860
1.11k
  return Success;
861
1.11k
}
862
863
static DecodeStatus DecodeDDDDRegisterClass(MCInst *Inst, unsigned RegNo,
864
                      uint64_t Addr, const void *Decoder)
865
1.10k
{
866
1.10k
  if (RegNo > 31)
867
0
    return Fail;
868
1.10k
  unsigned Register =
869
1.10k
    AArch64MCRegisterClasses[AArch64_DDDDRegClassID].RegsBegin[RegNo];
870
1.10k
  MCOperand_CreateReg0(Inst, (Register));
871
1.10k
  return Success;
872
1.10k
}
873
874
static DecodeStatus DecodeFixedPointScaleImm32(MCInst *Inst, unsigned Imm,
875
                         uint64_t Addr,
876
                         const void *Decoder)
877
267
{
878
  // scale{5} is asserted as 1 in tblgen.
879
267
  Imm |= 0x20;
880
267
  MCOperand_CreateImm0(Inst, (64 - Imm));
881
267
  return Success;
882
267
}
883
884
static DecodeStatus DecodeFixedPointScaleImm64(MCInst *Inst, unsigned Imm,
885
                         uint64_t Addr,
886
                         const void *Decoder)
887
113
{
888
113
  MCOperand_CreateImm0(Inst, (64 - Imm));
889
113
  return Success;
890
113
}
891
892
static DecodeStatus DecodePCRelLabel19(MCInst *Inst, unsigned Imm,
893
                     uint64_t Addr, const void *Decoder)
894
9.61k
{
895
9.61k
  int64_t ImmVal = Imm;
896
897
  // Sign-extend 19-bit immediate.
898
9.61k
  if (ImmVal & (1 << (19 - 1)))
899
2.64k
    ImmVal |= ~((1LL << 19) - 1);
900
901
  // No symbols supported in Capstone
902
  // if (!Decoder->tryAddingSymbolicOperand(
903
  //    Inst, ImmVal * 4, Addr, MCInst_getOpcode(Inst) != AArch64_LDRXl, 0,
904
  //    0, 4))
905
9.61k
  MCOperand_CreateImm0(Inst, (ImmVal));
906
9.61k
  return Success;
907
9.61k
}
908
909
static DecodeStatus DecodeMemExtend(MCInst *Inst, unsigned Imm,
910
                  uint64_t Address, const void *Decoder)
911
1.57k
{
912
1.57k
  MCOperand_CreateImm0(Inst, ((Imm >> 1) & 1));
913
1.57k
  MCOperand_CreateImm0(Inst, (Imm & 1));
914
1.57k
  return Success;
915
1.57k
}
916
917
static DecodeStatus DecodeMRSSystemRegister(MCInst *Inst, unsigned Imm,
918
                      uint64_t Address,
919
                      const void *Decoder)
920
585
{
921
585
  MCOperand_CreateImm0(Inst, (Imm));
922
923
  // Every system register in the encoding space is valid with the syntax
924
  // S<op0>_<op1>_<Cn>_<Cm>_<op2>, so decoding system registers always
925
  // succeeds.
926
585
  return Success;
927
585
}
928
929
static DecodeStatus DecodeMSRSystemRegister(MCInst *Inst, unsigned Imm,
930
                      uint64_t Address,
931
                      const void *Decoder)
932
1.90k
{
933
1.90k
  MCOperand_CreateImm0(Inst, (Imm));
934
935
1.90k
  return Success;
936
1.90k
}
937
938
static DecodeStatus DecodeFMOVLaneInstruction(MCInst *Inst, unsigned Insn,
939
                        uint64_t Address,
940
                        const void *Decoder)
941
84
{
942
  // This decoder exists to add the dummy Lane operand to the MCInst, which
943
  // must be 1 in assembly but has no other real manifestation.
944
84
  unsigned Rd = fieldFromInstruction_4(Insn, 0, 5);
945
84
  unsigned Rn = fieldFromInstruction_4(Insn, 5, 5);
946
84
  unsigned IsToVec = fieldFromInstruction_4(Insn, 16, 1);
947
948
84
  if (IsToVec) {
949
71
    DecodeFPR128RegisterClass(Inst, Rd, Address, Decoder);
950
71
    DecodeGPR64RegisterClass(Inst, Rn, Address, Decoder);
951
71
  } else {
952
13
    DecodeGPR64RegisterClass(Inst, Rd, Address, Decoder);
953
13
    DecodeFPR128RegisterClass(Inst, Rn, Address, Decoder);
954
13
  }
955
956
  // Add the lane
957
84
  MCOperand_CreateImm0(Inst, (1));
958
959
84
  return Success;
960
84
}
961
962
static DecodeStatus DecodeVecShiftRImm(MCInst *Inst, unsigned Imm, unsigned Add)
963
1.80k
{
964
1.80k
  MCOperand_CreateImm0(Inst, (Add - Imm));
965
1.80k
  return Success;
966
1.80k
}
967
968
static DecodeStatus DecodeVecShiftLImm(MCInst *Inst, unsigned Imm, unsigned Add)
969
1.90k
{
970
1.90k
  MCOperand_CreateImm0(Inst, ((Imm + Add) & (Add - 1)));
971
1.90k
  return Success;
972
1.90k
}
973
974
static DecodeStatus DecodeVecShiftR64Imm(MCInst *Inst, unsigned Imm,
975
                     uint64_t Addr, const void *Decoder)
976
354
{
977
354
  return DecodeVecShiftRImm(Inst, Imm, 64);
978
354
}
979
980
static DecodeStatus DecodeVecShiftR64ImmNarrow(MCInst *Inst, unsigned Imm,
981
                         uint64_t Addr,
982
                         const void *Decoder)
983
45
{
984
45
  return DecodeVecShiftRImm(Inst, Imm | 0x20, 64);
985
45
}
986
987
static DecodeStatus DecodeVecShiftR32Imm(MCInst *Inst, unsigned Imm,
988
                     uint64_t Addr, const void *Decoder)
989
350
{
990
350
  return DecodeVecShiftRImm(Inst, Imm, 32);
991
350
}
992
993
static DecodeStatus DecodeVecShiftR32ImmNarrow(MCInst *Inst, unsigned Imm,
994
                         uint64_t Addr,
995
                         const void *Decoder)
996
42
{
997
42
  return DecodeVecShiftRImm(Inst, Imm | 0x10, 32);
998
42
}
999
1000
static DecodeStatus DecodeVecShiftR16Imm(MCInst *Inst, unsigned Imm,
1001
                     uint64_t Addr, const void *Decoder)
1002
352
{
1003
352
  return DecodeVecShiftRImm(Inst, Imm, 16);
1004
352
}
1005
1006
static DecodeStatus DecodeVecShiftR16ImmNarrow(MCInst *Inst, unsigned Imm,
1007
                         uint64_t Addr,
1008
                         const void *Decoder)
1009
314
{
1010
314
  return DecodeVecShiftRImm(Inst, Imm | 0x8, 16);
1011
314
}
1012
1013
static DecodeStatus DecodeVecShiftR8Imm(MCInst *Inst, unsigned Imm,
1014
                    uint64_t Addr, const void *Decoder)
1015
352
{
1016
352
  return DecodeVecShiftRImm(Inst, Imm, 8);
1017
352
}
1018
1019
static DecodeStatus DecodeVecShiftL64Imm(MCInst *Inst, unsigned Imm,
1020
                     uint64_t Addr, const void *Decoder)
1021
335
{
1022
335
  return DecodeVecShiftLImm(Inst, Imm, 64);
1023
335
}
1024
1025
static DecodeStatus DecodeVecShiftL32Imm(MCInst *Inst, unsigned Imm,
1026
                     uint64_t Addr, const void *Decoder)
1027
677
{
1028
677
  return DecodeVecShiftLImm(Inst, Imm, 32);
1029
677
}
1030
1031
static DecodeStatus DecodeVecShiftL16Imm(MCInst *Inst, unsigned Imm,
1032
                     uint64_t Addr, const void *Decoder)
1033
519
{
1034
519
  return DecodeVecShiftLImm(Inst, Imm, 16);
1035
519
}
1036
1037
static DecodeStatus DecodeVecShiftL8Imm(MCInst *Inst, unsigned Imm,
1038
                    uint64_t Addr, const void *Decoder)
1039
377
{
1040
377
  return DecodeVecShiftLImm(Inst, Imm, 8);
1041
377
}
1042
1043
static DecodeStatus DecodeThreeAddrSRegInstruction(MCInst *Inst, uint32_t insn,
1044
                           uint64_t Addr,
1045
                           const void *Decoder)
1046
6.20k
{
1047
6.20k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1048
6.20k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1049
6.20k
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
1050
6.20k
  unsigned shiftHi = fieldFromInstruction_4(insn, 22, 2);
1051
6.20k
  unsigned shiftLo = fieldFromInstruction_4(insn, 10, 6);
1052
6.20k
  unsigned shift = (shiftHi << 6) | shiftLo;
1053
6.20k
  switch (MCInst_getOpcode(Inst)) {
1054
0
  default:
1055
0
    return Fail;
1056
625
  case AArch64_ADDWrs:
1057
947
  case AArch64_ADDSWrs:
1058
1.24k
  case AArch64_SUBWrs:
1059
1.35k
  case AArch64_SUBSWrs:
1060
    // if shift == '11' then ReservedValue()
1061
1.35k
    if (shiftHi == 0x3)
1062
8
      return Fail;
1063
    // fall through
1064
2.17k
  case AArch64_ANDWrs:
1065
2.26k
  case AArch64_ANDSWrs:
1066
2.40k
  case AArch64_BICWrs:
1067
2.87k
  case AArch64_BICSWrs:
1068
3.09k
  case AArch64_ORRWrs:
1069
3.49k
  case AArch64_ORNWrs:
1070
3.76k
  case AArch64_EORWrs:
1071
3.89k
  case AArch64_EONWrs: {
1072
    // if sf == '0' and imm6<5> == '1' then ReservedValue()
1073
3.89k
    if (shiftLo >> 5 == 1)
1074
42
      return Fail;
1075
3.84k
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1076
3.84k
    DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);
1077
3.84k
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1078
3.84k
    break;
1079
3.89k
  }
1080
389
  case AArch64_ADDXrs:
1081
451
  case AArch64_ADDSXrs:
1082
575
  case AArch64_SUBXrs:
1083
705
  case AArch64_SUBSXrs:
1084
    // if shift == '11' then ReservedValue()
1085
705
    if (shiftHi == 0x3)
1086
8
      return Fail;
1087
    // fall through
1088
823
  case AArch64_ANDXrs:
1089
909
  case AArch64_ANDSXrs:
1090
1.28k
  case AArch64_BICXrs:
1091
1.42k
  case AArch64_BICSXrs:
1092
1.61k
  case AArch64_ORRXrs:
1093
2.00k
  case AArch64_ORNXrs:
1094
2.20k
  case AArch64_EORXrs:
1095
2.30k
  case AArch64_EONXrs:
1096
2.30k
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1097
2.30k
    DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);
1098
2.30k
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1099
2.30k
    break;
1100
6.20k
  }
1101
1102
6.15k
  MCOperand_CreateImm0(Inst, (shift));
1103
6.15k
  return Success;
1104
6.20k
}
1105
1106
static DecodeStatus DecodeMoveImmInstruction(MCInst *Inst, uint32_t insn,
1107
                       uint64_t Addr, const void *Decoder)
1108
2.40k
{
1109
2.40k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1110
2.40k
  unsigned imm = fieldFromInstruction_4(insn, 5, 16);
1111
2.40k
  unsigned shift = fieldFromInstruction_4(insn, 21, 2);
1112
2.40k
  shift <<= 4;
1113
2.40k
  switch (MCInst_getOpcode(Inst)) {
1114
0
  default:
1115
0
    return Fail;
1116
454
  case AArch64_MOVZWi:
1117
1.08k
  case AArch64_MOVNWi:
1118
1.16k
  case AArch64_MOVKWi:
1119
1.16k
    if (shift & (1U << 5))
1120
6
      return Fail;
1121
1.16k
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1122
1.16k
    break;
1123
331
  case AArch64_MOVZXi:
1124
895
  case AArch64_MOVNXi:
1125
1.23k
  case AArch64_MOVKXi:
1126
1.23k
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1127
1.23k
    break;
1128
2.40k
  }
1129
1130
2.39k
  if (MCInst_getOpcode(Inst) == AArch64_MOVKWi ||
1131
2.39k
    MCInst_getOpcode(Inst) == AArch64_MOVKXi)
1132
420
    MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
1133
1134
2.39k
  MCOperand_CreateImm0(Inst, (imm));
1135
2.39k
  MCOperand_CreateImm0(Inst, (shift));
1136
2.39k
  return Success;
1137
2.40k
}
1138
1139
static DecodeStatus DecodeUnsignedLdStInstruction(MCInst *Inst, uint32_t insn,
1140
                          uint64_t Addr,
1141
                          const void *Decoder)
1142
6.39k
{
1143
6.39k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1144
6.39k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1145
6.39k
  unsigned offset = fieldFromInstruction_4(insn, 10, 12);
1146
1147
6.39k
  switch (MCInst_getOpcode(Inst)) {
1148
0
  default:
1149
0
    return Fail;
1150
396
  case AArch64_PRFMui:
1151
    // Rt is an immediate in prefetch.
1152
396
    MCOperand_CreateImm0(Inst, (Rt));
1153
396
    break;
1154
679
  case AArch64_STRBBui:
1155
746
  case AArch64_LDRBBui:
1156
773
  case AArch64_LDRSBWui:
1157
1.19k
  case AArch64_STRHHui:
1158
2.20k
  case AArch64_LDRHHui:
1159
2.25k
  case AArch64_LDRSHWui:
1160
2.35k
  case AArch64_STRWui:
1161
2.44k
  case AArch64_LDRWui:
1162
2.44k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1163
2.44k
    break;
1164
109
  case AArch64_LDRSBXui:
1165
945
  case AArch64_LDRSHXui:
1166
1.29k
  case AArch64_LDRSWui:
1167
1.55k
  case AArch64_STRXui:
1168
1.65k
  case AArch64_LDRXui:
1169
1.65k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1170
1.65k
    break;
1171
95
  case AArch64_LDRQui:
1172
155
  case AArch64_STRQui:
1173
155
    DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
1174
155
    break;
1175
180
  case AArch64_LDRDui:
1176
302
  case AArch64_STRDui:
1177
302
    DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
1178
302
    break;
1179
68
  case AArch64_LDRSui:
1180
162
  case AArch64_STRSui:
1181
162
    DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
1182
162
    break;
1183
119
  case AArch64_LDRHui:
1184
379
  case AArch64_STRHui:
1185
379
    DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);
1186
379
    break;
1187
130
  case AArch64_LDRBui:
1188
908
  case AArch64_STRBui:
1189
908
    DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);
1190
908
    break;
1191
6.39k
  }
1192
1193
6.39k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1194
  // No symbols supported in Capstone
1195
  // if (!Decoder->tryAddingSymbolicOperand(Inst, offset, Addr, Fail, 0, 0, 4))
1196
6.39k
  MCOperand_CreateImm0(Inst, (offset));
1197
6.39k
  return Success;
1198
6.39k
}
1199
1200
static DecodeStatus DecodeSignedLdStInstruction(MCInst *Inst, uint32_t insn,
1201
                        uint64_t Addr,
1202
                        const void *Decoder)
1203
8.56k
{
1204
8.56k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1205
8.56k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1206
8.56k
  int64_t offset = fieldFromInstruction_4(insn, 12, 9);
1207
1208
  // offset is a 9-bit signed immediate, so sign extend it to
1209
  // fill the unsigned.
1210
8.56k
  if (offset & (1 << (9 - 1)))
1211
3.93k
    offset |= ~((1LL << 9) - 1);
1212
1213
  // First operand is always the writeback to the address register, if needed.
1214
8.56k
  switch (MCInst_getOpcode(Inst)) {
1215
4.36k
  default:
1216
4.36k
    break;
1217
4.36k
  case AArch64_LDRSBWpre:
1218
564
  case AArch64_LDRSHWpre:
1219
593
  case AArch64_STRBBpre:
1220
609
  case AArch64_LDRBBpre:
1221
647
  case AArch64_STRHHpre:
1222
687
  case AArch64_LDRHHpre:
1223
724
  case AArch64_STRWpre:
1224
745
  case AArch64_LDRWpre:
1225
774
  case AArch64_LDRSBWpost:
1226
908
  case AArch64_LDRSHWpost:
1227
945
  case AArch64_STRBBpost:
1228
966
  case AArch64_LDRBBpost:
1229
1.12k
  case AArch64_STRHHpost:
1230
1.43k
  case AArch64_LDRHHpost:
1231
1.45k
  case AArch64_STRWpost:
1232
1.49k
  case AArch64_LDRWpost:
1233
1.58k
  case AArch64_LDRSBXpre:
1234
1.63k
  case AArch64_LDRSHXpre:
1235
1.73k
  case AArch64_STRXpre:
1236
1.74k
  case AArch64_LDRSWpre:
1237
1.80k
  case AArch64_LDRXpre:
1238
1.83k
  case AArch64_LDRSBXpost:
1239
1.91k
  case AArch64_LDRSHXpost:
1240
1.97k
  case AArch64_STRXpost:
1241
1.98k
  case AArch64_LDRSWpost:
1242
2.05k
  case AArch64_LDRXpost:
1243
2.07k
  case AArch64_LDRQpre:
1244
2.17k
  case AArch64_STRQpre:
1245
2.21k
  case AArch64_LDRQpost:
1246
2.30k
  case AArch64_STRQpost:
1247
2.34k
  case AArch64_LDRDpre:
1248
2.56k
  case AArch64_STRDpre:
1249
2.58k
  case AArch64_LDRDpost:
1250
2.60k
  case AArch64_STRDpost:
1251
2.64k
  case AArch64_LDRSpre:
1252
2.97k
  case AArch64_STRSpre:
1253
2.99k
  case AArch64_LDRSpost:
1254
3.20k
  case AArch64_STRSpost:
1255
3.27k
  case AArch64_LDRHpre:
1256
3.35k
  case AArch64_STRHpre:
1257
3.40k
  case AArch64_LDRHpost:
1258
3.42k
  case AArch64_STRHpost:
1259
3.54k
  case AArch64_LDRBpre:
1260
3.59k
  case AArch64_STRBpre:
1261
3.60k
  case AArch64_LDRBpost:
1262
4.20k
  case AArch64_STRBpost:
1263
4.20k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1264
4.20k
    break;
1265
8.56k
  }
1266
1267
8.56k
  switch (MCInst_getOpcode(Inst)) {
1268
0
  default:
1269
0
    return Fail;
1270
42
  case AArch64_PRFUMi:
1271
    // Rt is an immediate in prefetch.
1272
42
    MCOperand_CreateImm0(Inst, (Rt));
1273
42
    break;
1274
44
  case AArch64_STURBBi:
1275
142
  case AArch64_LDURBBi:
1276
193
  case AArch64_LDURSBWi:
1277
342
  case AArch64_STURHHi:
1278
513
  case AArch64_LDURHHi:
1279
604
  case AArch64_LDURSHWi:
1280
701
  case AArch64_STURWi:
1281
727
  case AArch64_LDURWi:
1282
774
  case AArch64_LDTRSBWi:
1283
893
  case AArch64_LDTRSHWi:
1284
1.05k
  case AArch64_STTRWi:
1285
1.14k
  case AArch64_LDTRWi:
1286
1.24k
  case AArch64_STTRHi:
1287
1.26k
  case AArch64_LDTRHi:
1288
1.28k
  case AArch64_LDTRBi:
1289
1.36k
  case AArch64_STTRBi:
1290
1.52k
  case AArch64_LDRSBWpre:
1291
1.93k
  case AArch64_LDRSHWpre:
1292
1.96k
  case AArch64_STRBBpre:
1293
1.97k
  case AArch64_LDRBBpre:
1294
2.01k
  case AArch64_STRHHpre:
1295
2.05k
  case AArch64_LDRHHpre:
1296
2.09k
  case AArch64_STRWpre:
1297
2.11k
  case AArch64_LDRWpre:
1298
2.14k
  case AArch64_LDRSBWpost:
1299
2.27k
  case AArch64_LDRSHWpost:
1300
2.31k
  case AArch64_STRBBpost:
1301
2.33k
  case AArch64_LDRBBpost:
1302
2.49k
  case AArch64_STRHHpost:
1303
2.80k
  case AArch64_LDRHHpost:
1304
2.82k
  case AArch64_STRWpost:
1305
2.86k
  case AArch64_LDRWpost:
1306
2.91k
  case AArch64_STLURBi:
1307
3.00k
  case AArch64_STLURHi:
1308
3.18k
  case AArch64_STLURWi:
1309
3.20k
  case AArch64_LDAPURBi:
1310
3.25k
  case AArch64_LDAPURSBWi:
1311
3.33k
  case AArch64_LDAPURHi:
1312
3.39k
  case AArch64_LDAPURSHWi:
1313
3.45k
  case AArch64_LDAPURi:
1314
3.45k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1315
3.45k
    break;
1316
33
  case AArch64_LDURSBXi:
1317
278
  case AArch64_LDURSHXi:
1318
307
  case AArch64_LDURSWi:
1319
420
  case AArch64_STURXi:
1320
527
  case AArch64_LDURXi:
1321
559
  case AArch64_LDTRSBXi:
1322
684
  case AArch64_LDTRSHXi:
1323
769
  case AArch64_LDTRSWi:
1324
1.09k
  case AArch64_STTRXi:
1325
1.22k
  case AArch64_LDTRXi:
1326
1.31k
  case AArch64_LDRSBXpre:
1327
1.36k
  case AArch64_LDRSHXpre:
1328
1.46k
  case AArch64_STRXpre:
1329
1.47k
  case AArch64_LDRSWpre:
1330
1.53k
  case AArch64_LDRXpre:
1331
1.56k
  case AArch64_LDRSBXpost:
1332
1.65k
  case AArch64_LDRSHXpost:
1333
1.70k
  case AArch64_STRXpost:
1334
1.71k
  case AArch64_LDRSWpost:
1335
1.78k
  case AArch64_LDRXpost:
1336
1.84k
  case AArch64_LDAPURSWi:
1337
1.86k
  case AArch64_LDAPURSHXi:
1338
1.90k
  case AArch64_LDAPURSBXi:
1339
1.95k
  case AArch64_STLURXi:
1340
1.97k
  case AArch64_LDAPURXi:
1341
1.97k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1342
1.97k
    break;
1343
13
  case AArch64_LDURQi:
1344
118
  case AArch64_STURQi:
1345
139
  case AArch64_LDRQpre:
1346
242
  case AArch64_STRQpre:
1347
283
  case AArch64_LDRQpost:
1348
366
  case AArch64_STRQpost:
1349
366
    DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
1350
366
    break;
1351
271
  case AArch64_LDURDi:
1352
419
  case AArch64_STURDi:
1353
463
  case AArch64_LDRDpre:
1354
679
  case AArch64_STRDpre:
1355
704
  case AArch64_LDRDpost:
1356
725
  case AArch64_STRDpost:
1357
725
    DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
1358
725
    break;
1359
19
  case AArch64_LDURSi:
1360
40
  case AArch64_STURSi:
1361
82
  case AArch64_LDRSpre:
1362
406
  case AArch64_STRSpre:
1363
427
  case AArch64_LDRSpost:
1364
636
  case AArch64_STRSpost:
1365
636
    DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
1366
636
    break;
1367
14
  case AArch64_LDURHi:
1368
171
  case AArch64_STURHi:
1369
243
  case AArch64_LDRHpre:
1370
323
  case AArch64_STRHpre:
1371
376
  case AArch64_LDRHpost:
1372
394
  case AArch64_STRHpost:
1373
394
    DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);
1374
394
    break;
1375
151
  case AArch64_LDURBi:
1376
194
  case AArch64_STURBi:
1377
309
  case AArch64_LDRBpre:
1378
359
  case AArch64_STRBpre:
1379
369
  case AArch64_LDRBpost:
1380
973
  case AArch64_STRBpost:
1381
973
    DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);
1382
973
    break;
1383
8.56k
  }
1384
1385
8.56k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1386
8.56k
  MCOperand_CreateImm0(Inst, (offset));
1387
1388
8.56k
  bool IsLoad = fieldFromInstruction_4(insn, 22, 1);
1389
8.56k
  bool IsIndexed = fieldFromInstruction_4(insn, 10, 2) != 0;
1390
8.56k
  bool IsFP = fieldFromInstruction_4(insn, 26, 1);
1391
1392
  // Cannot write back to a transfer register (but xzr != sp).
1393
8.56k
  if (IsLoad && IsIndexed && !IsFP && Rn != 31 && Rt == Rn)
1394
52
    return SoftFail;
1395
1396
8.51k
  return Success;
1397
8.56k
}
1398
1399
static DecodeStatus DecodeExclusiveLdStInstruction(MCInst *Inst, uint32_t insn,
1400
                           uint64_t Addr,
1401
                           const void *Decoder)
1402
5.57k
{
1403
5.57k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1404
5.57k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1405
5.57k
  unsigned Rt2 = fieldFromInstruction_4(insn, 10, 5);
1406
5.57k
  unsigned Rs = fieldFromInstruction_4(insn, 16, 5);
1407
1408
5.57k
  unsigned Opcode = MCInst_getOpcode(Inst);
1409
5.57k
  switch (Opcode) {
1410
0
  default:
1411
0
    return Fail;
1412
225
  case AArch64_STLXRW:
1413
352
  case AArch64_STLXRB:
1414
364
  case AArch64_STLXRH:
1415
462
  case AArch64_STXRW:
1416
662
  case AArch64_STXRB:
1417
746
  case AArch64_STXRH:
1418
746
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1419
    // fall through
1420
768
  case AArch64_LDARW:
1421
796
  case AArch64_LDARB:
1422
828
  case AArch64_LDARH:
1423
843
  case AArch64_LDAXRW:
1424
917
  case AArch64_LDAXRB:
1425
946
  case AArch64_LDAXRH:
1426
972
  case AArch64_LDXRW:
1427
1.12k
  case AArch64_LDXRB:
1428
1.31k
  case AArch64_LDXRH:
1429
1.56k
  case AArch64_STLRW:
1430
1.65k
  case AArch64_STLRB:
1431
1.73k
  case AArch64_STLRH:
1432
1.80k
  case AArch64_STLLRW:
1433
2.00k
  case AArch64_STLLRB:
1434
2.04k
  case AArch64_STLLRH:
1435
2.26k
  case AArch64_LDLARW:
1436
2.43k
  case AArch64_LDLARB:
1437
2.49k
  case AArch64_LDLARH:
1438
2.49k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1439
2.49k
    break;
1440
65
  case AArch64_STLXRX:
1441
137
  case AArch64_STXRX:
1442
137
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1443
    // fall through
1444
730
  case AArch64_LDARX:
1445
775
  case AArch64_LDAXRX:
1446
810
  case AArch64_LDXRX:
1447
879
  case AArch64_STLRX:
1448
994
  case AArch64_LDLARX:
1449
1.10k
  case AArch64_STLLRX:
1450
1.10k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1451
1.10k
    break;
1452
552
  case AArch64_STLXPW:
1453
1.44k
  case AArch64_STXPW:
1454
1.44k
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1455
    // fall through
1456
1.69k
  case AArch64_LDAXPW:
1457
1.76k
  case AArch64_LDXPW:
1458
1.76k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1459
1.76k
    DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);
1460
1.76k
    break;
1461
51
  case AArch64_STLXPX:
1462
137
  case AArch64_STXPX:
1463
137
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1464
    // fall through
1465
187
  case AArch64_LDAXPX:
1466
208
  case AArch64_LDXPX:
1467
208
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1468
208
    DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);
1469
208
    break;
1470
5.57k
  }
1471
1472
5.57k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1473
1474
  // You shouldn't load to the same register twice in an instruction...
1475
5.57k
  if ((Opcode == AArch64_LDAXPW || Opcode == AArch64_LDXPW ||
1476
5.57k
     Opcode == AArch64_LDAXPX || Opcode == AArch64_LDXPX) &&
1477
5.57k
    Rt == Rt2)
1478
22
    return SoftFail;
1479
1480
5.55k
  return Success;
1481
5.57k
}
1482
1483
static DecodeStatus DecodePairLdStInstruction(MCInst *Inst, uint32_t insn,
1484
                        uint64_t Addr,
1485
                        const void *Decoder)
1486
12.8k
{
1487
12.8k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1488
12.8k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1489
12.8k
  unsigned Rt2 = fieldFromInstruction_4(insn, 10, 5);
1490
12.8k
  int64_t offset = fieldFromInstruction_4(insn, 15, 7);
1491
12.8k
  bool IsLoad = fieldFromInstruction_4(insn, 22, 1);
1492
1493
  // offset is a 7-bit signed immediate, so sign extend it to
1494
  // fill the unsigned.
1495
12.8k
  if (offset & (1 << (7 - 1)))
1496
8.71k
    offset |= ~((1LL << 7) - 1);
1497
1498
12.8k
  unsigned Opcode = MCInst_getOpcode(Inst);
1499
12.8k
  bool NeedsDisjointWritebackTransfer = false;
1500
1501
  // First operand is always writeback of base register.
1502
12.8k
  switch (Opcode) {
1503
5.94k
  default:
1504
5.94k
    break;
1505
5.94k
  case AArch64_LDPXpost:
1506
811
  case AArch64_STPXpost:
1507
877
  case AArch64_LDPSWpost:
1508
924
  case AArch64_LDPXpre:
1509
1.13k
  case AArch64_STPXpre:
1510
1.26k
  case AArch64_LDPSWpre:
1511
1.56k
  case AArch64_LDPWpost:
1512
1.62k
  case AArch64_STPWpost:
1513
1.72k
  case AArch64_LDPWpre:
1514
1.80k
  case AArch64_STPWpre:
1515
1.90k
  case AArch64_LDPQpost:
1516
4.53k
  case AArch64_STPQpost:
1517
4.62k
  case AArch64_LDPQpre:
1518
5.18k
  case AArch64_STPQpre:
1519
5.40k
  case AArch64_LDPDpost:
1520
5.47k
  case AArch64_STPDpost:
1521
5.56k
  case AArch64_LDPDpre:
1522
5.66k
  case AArch64_STPDpre:
1523
5.75k
  case AArch64_LDPSpost:
1524
5.90k
  case AArch64_STPSpost:
1525
6.05k
  case AArch64_LDPSpre:
1526
6.36k
  case AArch64_STPSpre:
1527
6.43k
  case AArch64_STGPpre:
1528
6.91k
  case AArch64_STGPpost:
1529
6.91k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1530
6.91k
    break;
1531
12.8k
  }
1532
1533
12.8k
  switch (Opcode) {
1534
0
  default:
1535
0
    return Fail;
1536
240
  case AArch64_LDPXpost:
1537
811
  case AArch64_STPXpost:
1538
877
  case AArch64_LDPSWpost:
1539
924
  case AArch64_LDPXpre:
1540
1.13k
  case AArch64_STPXpre:
1541
1.26k
  case AArch64_LDPSWpre:
1542
1.33k
  case AArch64_STGPpre:
1543
1.81k
  case AArch64_STGPpost:
1544
1.81k
    NeedsDisjointWritebackTransfer = true;
1545
    // fall through
1546
1.92k
  case AArch64_LDNPXi:
1547
2.11k
  case AArch64_STNPXi:
1548
2.28k
  case AArch64_LDPXi:
1549
2.65k
  case AArch64_STPXi:
1550
2.80k
  case AArch64_LDPSWi:
1551
2.86k
  case AArch64_STGPi:
1552
2.86k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1553
2.86k
    DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);
1554
2.86k
    break;
1555
301
  case AArch64_LDPWpost:
1556
357
  case AArch64_STPWpost:
1557
457
  case AArch64_LDPWpre:
1558
539
  case AArch64_STPWpre:
1559
539
    NeedsDisjointWritebackTransfer = true;
1560
    // fall through
1561
783
  case AArch64_LDNPWi:
1562
937
  case AArch64_STNPWi:
1563
1.07k
  case AArch64_LDPWi:
1564
1.40k
  case AArch64_STPWi:
1565
1.40k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1566
1.40k
    DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);
1567
1.40k
    break;
1568
329
  case AArch64_LDNPQi:
1569
517
  case AArch64_STNPQi:
1570
617
  case AArch64_LDPQpost:
1571
3.24k
  case AArch64_STPQpost:
1572
3.34k
  case AArch64_LDPQi:
1573
3.37k
  case AArch64_STPQi:
1574
3.47k
  case AArch64_LDPQpre:
1575
4.02k
  case AArch64_STPQpre:
1576
4.02k
    DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
1577
4.02k
    DecodeFPR128RegisterClass(Inst, Rt2, Addr, Decoder);
1578
4.02k
    break;
1579
657
  case AArch64_LDNPDi:
1580
955
  case AArch64_STNPDi:
1581
1.17k
  case AArch64_LDPDpost:
1582
1.24k
  case AArch64_STPDpost:
1583
1.71k
  case AArch64_LDPDi:
1584
1.80k
  case AArch64_STPDi:
1585
1.90k
  case AArch64_LDPDpre:
1586
2.00k
  case AArch64_STPDpre:
1587
2.00k
    DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
1588
2.00k
    DecodeFPR64RegisterClass(Inst, Rt2, Addr, Decoder);
1589
2.00k
    break;
1590
505
  case AArch64_LDNPSi:
1591
856
  case AArch64_STNPSi:
1592
938
  case AArch64_LDPSpost:
1593
1.09k
  case AArch64_STPSpost:
1594
1.38k
  case AArch64_LDPSi:
1595
2.09k
  case AArch64_STPSi:
1596
2.24k
  case AArch64_LDPSpre:
1597
2.55k
  case AArch64_STPSpre:
1598
2.55k
    DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
1599
2.55k
    DecodeFPR32RegisterClass(Inst, Rt2, Addr, Decoder);
1600
2.55k
    break;
1601
12.8k
  }
1602
1603
12.8k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1604
12.8k
  MCOperand_CreateImm0(Inst, (offset));
1605
1606
  // You shouldn't load to the same register twice in an instruction...
1607
12.8k
  if (IsLoad && Rt == Rt2)
1608
194
    return SoftFail;
1609
1610
  // ... or do any operation that writes-back to a transfer register. But note
1611
  // that "stp xzr, xzr, [sp], #4" is fine because xzr and sp are different.
1612
12.6k
  if (NeedsDisjointWritebackTransfer && Rn != 31 && (Rt == Rn || Rt2 == Rn))
1613
340
    return SoftFail;
1614
1615
12.3k
  return Success;
1616
12.6k
}
1617
1618
static DecodeStatus DecodeAuthLoadInstruction(MCInst *Inst, uint32_t insn,
1619
                        uint64_t Addr,
1620
                        const void *Decoder)
1621
776
{
1622
776
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1623
776
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1624
776
  uint64_t offset = fieldFromInstruction_4(insn, 22, 1) << 9 |
1625
776
            fieldFromInstruction_4(insn, 12, 9);
1626
776
  unsigned writeback = fieldFromInstruction_4(insn, 11, 1);
1627
1628
776
  switch (MCInst_getOpcode(Inst)) {
1629
0
  default:
1630
0
    return Fail;
1631
88
  case AArch64_LDRAAwriteback:
1632
409
  case AArch64_LDRABwriteback:
1633
409
    DecodeGPR64spRegisterClass(Inst, Rn /* writeback register */, Addr,
1634
409
                   Decoder);
1635
409
    break;
1636
111
  case AArch64_LDRAAindexed:
1637
367
  case AArch64_LDRABindexed:
1638
367
    break;
1639
776
  }
1640
1641
776
  DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1642
776
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1643
776
  CONCAT(DecodeSImm, 10)(Inst, offset, Addr, Decoder);
1644
1645
776
  if (writeback && Rt == Rn && Rn != 31) {
1646
40
    return SoftFail;
1647
40
  }
1648
1649
736
  return Success;
1650
776
}
1651
1652
static DecodeStatus DecodeAddSubERegInstruction(MCInst *Inst, uint32_t insn,
1653
                        uint64_t Addr,
1654
                        const void *Decoder)
1655
1.99k
{
1656
1.99k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1657
1.99k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1658
1.99k
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
1659
1.99k
  unsigned extend = fieldFromInstruction_4(insn, 10, 6);
1660
1661
1.99k
  unsigned shift = extend & 0x7;
1662
1.99k
  if (shift > 4)
1663
4
    return Fail;
1664
1665
1.98k
  switch (MCInst_getOpcode(Inst)) {
1666
0
  default:
1667
0
    return Fail;
1668
173
  case AArch64_ADDWrx:
1669
391
  case AArch64_SUBWrx:
1670
391
    DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1671
391
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1672
391
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1673
391
    break;
1674
396
  case AArch64_ADDSWrx:
1675
590
  case AArch64_SUBSWrx:
1676
590
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1677
590
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1678
590
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1679
590
    break;
1680
304
  case AArch64_ADDXrx:
1681
388
  case AArch64_SUBXrx:
1682
388
    DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1683
388
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1684
388
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1685
388
    break;
1686
150
  case AArch64_ADDSXrx:
1687
188
  case AArch64_SUBSXrx:
1688
188
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1689
188
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1690
188
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1691
188
    break;
1692
373
  case AArch64_ADDXrx64:
1693
407
  case AArch64_SUBXrx64:
1694
407
    DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1695
407
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1696
407
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1697
407
    break;
1698
11
  case AArch64_SUBSXrx64:
1699
24
  case AArch64_ADDSXrx64:
1700
24
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1701
24
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1702
24
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1703
24
    break;
1704
1.98k
  }
1705
1706
1.98k
  MCOperand_CreateImm0(Inst, (extend));
1707
1.98k
  return Success;
1708
1.98k
}
1709
1710
static DecodeStatus DecodeLogicalImmInstruction(MCInst *Inst, uint32_t insn,
1711
                        uint64_t Addr,
1712
                        const void *Decoder)
1713
3.30k
{
1714
3.30k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1715
3.30k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1716
3.30k
  unsigned Datasize = fieldFromInstruction_4(insn, 31, 1);
1717
3.30k
  unsigned imm;
1718
1719
3.30k
  if (Datasize) {
1720
1.03k
    if (MCInst_getOpcode(Inst) == AArch64_ANDSXri)
1721
229
      DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1722
803
    else
1723
803
      DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1724
1.03k
    DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);
1725
1.03k
    imm = fieldFromInstruction_4(insn, 10, 13);
1726
1.03k
    if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 64))
1727
5
      return Fail;
1728
2.27k
  } else {
1729
2.27k
    if (MCInst_getOpcode(Inst) == AArch64_ANDSWri)
1730
280
      DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1731
1.99k
    else
1732
1.99k
      DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1733
2.27k
    DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);
1734
2.27k
    imm = fieldFromInstruction_4(insn, 10, 12);
1735
2.27k
    if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 32))
1736
4
      return Fail;
1737
2.27k
  }
1738
3.29k
  MCOperand_CreateImm0(Inst, (imm));
1739
3.29k
  return Success;
1740
3.30k
}
1741
1742
static DecodeStatus DecodeModImmInstruction(MCInst *Inst, uint32_t insn,
1743
                      uint64_t Addr, const void *Decoder)
1744
1.14k
{
1745
1.14k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1746
1.14k
  unsigned cmode = fieldFromInstruction_4(insn, 12, 4);
1747
1.14k
  unsigned imm = fieldFromInstruction_4(insn, 16, 3) << 5;
1748
1.14k
  imm |= fieldFromInstruction_4(insn, 5, 5);
1749
1750
1.14k
  if (MCInst_getOpcode(Inst) == AArch64_MOVID)
1751
131
    DecodeFPR64RegisterClass(Inst, Rd, Addr, Decoder);
1752
1.01k
  else
1753
1.01k
    DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1754
1755
1.14k
  MCOperand_CreateImm0(Inst, (imm));
1756
1757
1.14k
  switch (MCInst_getOpcode(Inst)) {
1758
197
  default:
1759
197
    break;
1760
389
  case AArch64_MOVIv4i16:
1761
401
  case AArch64_MOVIv8i16:
1762
421
  case AArch64_MVNIv4i16:
1763
427
  case AArch64_MVNIv8i16:
1764
482
  case AArch64_MOVIv2i32:
1765
815
  case AArch64_MOVIv4i32:
1766
845
  case AArch64_MVNIv2i32:
1767
886
  case AArch64_MVNIv4i32:
1768
886
    MCOperand_CreateImm0(Inst, ((cmode & 6) << 2));
1769
886
    break;
1770
20
  case AArch64_MOVIv2s_msl:
1771
30
  case AArch64_MOVIv4s_msl:
1772
43
  case AArch64_MVNIv2s_msl:
1773
63
  case AArch64_MVNIv4s_msl:
1774
63
    MCOperand_CreateImm0(Inst, ((cmode & 1) ? 0x110 : 0x108));
1775
63
    break;
1776
1.14k
  }
1777
1778
1.14k
  return Success;
1779
1.14k
}
1780
1781
static DecodeStatus DecodeModImmTiedInstruction(MCInst *Inst, uint32_t insn,
1782
                        uint64_t Addr,
1783
                        const void *Decoder)
1784
37
{
1785
37
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1786
37
  unsigned cmode = fieldFromInstruction_4(insn, 12, 4);
1787
37
  unsigned imm = fieldFromInstruction_4(insn, 16, 3) << 5;
1788
37
  imm |= fieldFromInstruction_4(insn, 5, 5);
1789
1790
  // Tied operands added twice.
1791
37
  DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1792
37
  DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1793
1794
37
  MCOperand_CreateImm0(Inst, (imm));
1795
37
  MCOperand_CreateImm0(Inst, ((cmode & 6) << 2));
1796
1797
37
  return Success;
1798
37
}
1799
1800
static DecodeStatus DecodeAdrInstruction(MCInst *Inst, uint32_t insn,
1801
                     uint64_t Addr, const void *Decoder)
1802
4.55k
{
1803
4.55k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1804
4.55k
  int64_t imm = fieldFromInstruction_4(insn, 5, 19) << 2;
1805
4.55k
  imm |= fieldFromInstruction_4(insn, 29, 2);
1806
1807
  // Sign-extend the 21-bit immediate.
1808
4.55k
  if (imm & (1 << (21 - 1)))
1809
1.60k
    imm |= ~((1LL << 21) - 1);
1810
1811
4.55k
  DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1812
  // No symbols supported in Capstone
1813
  // if (!Decoder->tryAddingSymbolicOperand(Inst, imm, Addr, Fail, 0, 0, 4))
1814
4.55k
  MCOperand_CreateImm0(Inst, (imm));
1815
1816
4.55k
  return Success;
1817
4.55k
}
1818
1819
static DecodeStatus DecodeAddSubImmShift(MCInst *Inst, uint32_t insn,
1820
                     uint64_t Addr, const void *Decoder)
1821
1.99k
{
1822
1.99k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1823
1.99k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1824
1.99k
  unsigned Imm = fieldFromInstruction_4(insn, 10, 14);
1825
1.99k
  unsigned S = fieldFromInstruction_4(insn, 29, 1);
1826
1.99k
  unsigned Datasize = fieldFromInstruction_4(insn, 31, 1);
1827
1828
1.99k
  unsigned ShifterVal = (Imm >> 12) & 3;
1829
1.99k
  unsigned ImmVal = Imm & 0xFFF;
1830
1831
1.99k
  if (ShifterVal != 0 && ShifterVal != 1)
1832
47
    return Fail;
1833
1834
1.95k
  if (Datasize) {
1835
582
    if (Rd == 31 && !S)
1836
200
      DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1837
382
    else
1838
382
      DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1839
582
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1840
1.36k
  } else {
1841
1.36k
    if (Rd == 31 && !S)
1842
8
      DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1843
1.36k
    else
1844
1.36k
      DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1845
1.36k
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1846
1.36k
  }
1847
1848
  // No symbols supported in Capstone
1849
  // if (!Decoder->tryAddingSymbolicOperand(Inst, Imm, Addr, Fail, 0, 0, 4))
1850
1.95k
  MCOperand_CreateImm0(Inst, (ImmVal));
1851
1.95k
  MCOperand_CreateImm0(Inst, (12 * ShifterVal));
1852
1.95k
  return Success;
1853
1.99k
}
1854
1855
static DecodeStatus DecodeUnconditionalBranch(MCInst *Inst, uint32_t insn,
1856
                        uint64_t Addr,
1857
                        const void *Decoder)
1858
2.86k
{
1859
2.86k
  int64_t imm = fieldFromInstruction_4(insn, 0, 26);
1860
1861
  // Sign-extend the 26-bit immediate.
1862
2.86k
  if (imm & (1 << (26 - 1)))
1863
1.15k
    imm |= ~((1LL << 26) - 1);
1864
1865
  // No symbols supported in Capstone
1866
  // if (!Decoder->tryAddingSymbolicOperand(Inst, imm * 4, Addr, true, 0, 0, 4))
1867
2.86k
  MCOperand_CreateImm0(Inst, (imm));
1868
1869
2.86k
  return Success;
1870
2.86k
}
1871
1872
static bool isInvalidPState(uint64_t Op1, uint64_t Op2)
1873
1.55k
{
1874
1.55k
  return Op1 == 0b000 && (Op2 == 0b000 || // CFINV
1875
685
              Op2 == 0b001 || // XAFlag
1876
685
              Op2 == 0b010); // AXFlag
1877
1.55k
}
1878
1879
static DecodeStatus DecodeSystemPStateImm0_15Instruction(MCInst *Inst,
1880
                             uint32_t insn,
1881
                             uint64_t Addr,
1882
                             const void *Decoder)
1883
478
{
1884
478
  uint64_t op1 = fieldFromInstruction_4(insn, 16, 3);
1885
478
  uint64_t op2 = fieldFromInstruction_4(insn, 5, 3);
1886
478
  uint64_t imm = fieldFromInstruction_4(insn, 8, 4);
1887
478
  uint64_t pstate_field = (op1 << 3) | op2;
1888
1889
478
  if (isInvalidPState(op1, op2))
1890
245
    return Fail;
1891
1892
233
  MCOperand_CreateImm0(Inst, (pstate_field));
1893
233
  MCOperand_CreateImm0(Inst, (imm));
1894
1895
233
  const AArch64PState_PStateImm0_15 *PState = AArch64PState_lookupPStateImm0_15ByEncoding(pstate_field);
1896
233
  if (PState &&
1897
233
    AArch64_testFeatureList(Inst->csh->mode, PState->FeaturesRequired))
1898
195
    return Success;
1899
38
  return Fail;
1900
233
}
1901
1902
static DecodeStatus DecodeSystemPStateImm0_1Instruction(MCInst *Inst,
1903
                            uint32_t insn,
1904
                            uint64_t Addr,
1905
                            const void *Decoder)
1906
1.07k
{
1907
1.07k
  uint64_t op1 = fieldFromInstruction_4(insn, 16, 3);
1908
1.07k
  uint64_t op2 = fieldFromInstruction_4(insn, 5, 3);
1909
1.07k
  uint64_t crm_high = fieldFromInstruction_4(insn, 9, 3);
1910
1.07k
  uint64_t imm = fieldFromInstruction_4(insn, 8, 1);
1911
1.07k
  uint64_t pstate_field = (crm_high << 6) | (op1 << 3) | op2;
1912
1913
1.07k
  if (isInvalidPState(op1, op2))
1914
245
    return Fail;
1915
1916
827
  MCOperand_CreateImm0(Inst, (pstate_field));
1917
827
  MCOperand_CreateImm0(Inst, (imm));
1918
1919
827
  const AArch64PState_PStateImm0_1 *PState = AArch64PState_lookupPStateImm0_1ByEncoding(pstate_field);
1920
827
  if (PState &&
1921
827
    AArch64_testFeatureList(Inst->csh->mode, PState->FeaturesRequired))
1922
29
    return Success;
1923
798
  return Fail;
1924
827
}
1925
1926
static DecodeStatus DecodeTestAndBranch(MCInst *Inst, uint32_t insn,
1927
                    uint64_t Addr, const void *Decoder)
1928
1.32k
{
1929
1.32k
  uint64_t Rt = fieldFromInstruction_4(insn, 0, 5);
1930
1.32k
  uint64_t bit = fieldFromInstruction_4(insn, 31, 1) << 5;
1931
1.32k
  bit |= fieldFromInstruction_4(insn, 19, 5);
1932
1.32k
  int64_t dst = fieldFromInstruction_4(insn, 5, 14);
1933
1934
  // Sign-extend 14-bit immediate.
1935
1.32k
  if (dst & (1 << (14 - 1)))
1936
1.00k
    dst |= ~((1LL << 14) - 1);
1937
1938
1.32k
  if (fieldFromInstruction_4(insn, 31, 1) == 0)
1939
898
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1940
423
  else
1941
423
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1942
1.32k
  MCOperand_CreateImm0(Inst, (bit));
1943
  // No symbols supported in Capstone
1944
  // if (!Decoder->tryAddingSymbolicOperand(Inst, dst * 4, Addr, true, 0, 0, 4))
1945
1.32k
  MCOperand_CreateImm0(Inst, (dst));
1946
1947
1.32k
  return Success;
1948
1.32k
}
1949
1950
static DecodeStatus DecodeGPRSeqPairsClassRegisterClass(MCInst *Inst,
1951
                            unsigned RegClassID,
1952
                            unsigned RegNo,
1953
                            uint64_t Addr,
1954
                            const void *Decoder)
1955
1.66k
{
1956
  // Register number must be even (see CASP instruction)
1957
1.66k
  if (RegNo & 0x1)
1958
13
    return Fail;
1959
1960
1.64k
  unsigned Reg = AArch64MCRegisterClasses[RegClassID].RegsBegin[RegNo / 2];
1961
1.64k
  MCOperand_CreateReg0(Inst, (Reg));
1962
1.64k
  return Success;
1963
1.66k
}
1964
1965
static DecodeStatus DecodeWSeqPairsClassRegisterClass(MCInst *Inst,
1966
                            unsigned RegNo,
1967
                            uint64_t Addr,
1968
                            const void *Decoder)
1969
107
{
1970
107
  return DecodeGPRSeqPairsClassRegisterClass(
1971
107
    Inst, AArch64_WSeqPairsClassRegClassID, RegNo, Addr, Decoder);
1972
107
}
1973
1974
static DecodeStatus DecodeXSeqPairsClassRegisterClass(MCInst *Inst,
1975
                            unsigned RegNo,
1976
                            uint64_t Addr,
1977
                            const void *Decoder)
1978
1.55k
{
1979
1.55k
  return DecodeGPRSeqPairsClassRegisterClass(
1980
1.55k
    Inst, AArch64_XSeqPairsClassRegClassID, RegNo, Addr, Decoder);
1981
1.55k
}
1982
1983
static DecodeStatus DecodeSyspXzrInstruction(MCInst *Inst, uint32_t insn,
1984
                       uint64_t Addr, const void *Decoder)
1985
57
{
1986
57
  unsigned op1 = fieldFromInstruction_4(insn, 16, 3);
1987
57
  unsigned CRn = fieldFromInstruction_4(insn, 12, 4);
1988
57
  unsigned CRm = fieldFromInstruction_4(insn, 8, 4);
1989
57
  unsigned op2 = fieldFromInstruction_4(insn, 5, 3);
1990
57
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1991
57
  if (Rt != 0b11111)
1992
0
    return Fail;
1993
1994
57
  MCOperand_CreateImm0(Inst, (op1));
1995
57
  MCOperand_CreateImm0(Inst, (CRn));
1996
57
  MCOperand_CreateImm0(Inst, (CRm));
1997
57
  MCOperand_CreateImm0(Inst, (op2));
1998
57
  DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1999
2000
57
  return Success;
2001
57
}
2002
2003
static DecodeStatus DecodeSVELogicalImmInstruction(MCInst *Inst, uint32_t insn,
2004
                           uint64_t Addr,
2005
                           const void *Decoder)
2006
4.56k
{
2007
4.56k
  unsigned Zdn = fieldFromInstruction_4(insn, 0, 5);
2008
4.56k
  unsigned imm = fieldFromInstruction_4(insn, 5, 13);
2009
4.56k
  if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 64))
2010
4
    return Fail;
2011
2012
  // The same (tied) operand is added twice to the instruction.
2013
4.56k
  DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);
2014
4.56k
  if (MCInst_getOpcode(Inst) != AArch64_DUPM_ZI)
2015
698
    DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);
2016
4.56k
  MCOperand_CreateImm0(Inst, (imm));
2017
4.56k
  return Success;
2018
4.56k
}
2019
2020
#define DEFINE_DecodeSImm(Bits)                                                \
2021
  static DecodeStatus CONCAT(DecodeSImm, Bits)(                              \
2022
    MCInst * Inst, uint64_t Imm, uint64_t Address, const void *Decoder)    \
2023
7.42k
  {                                                                          \
2024
7.42k
    if (Imm & ~((1LL << Bits) - 1))                                        \
2025
7.42k
      return Fail;                                                       \
2026
7.42k
                                                                               \
2027
7.42k
    if (Imm & (1 << (Bits - 1)))                                           \
2028
7.42k
      Imm |= ~((1LL << Bits) - 1);                                       \
2029
7.42k
                                                                               \
2030
7.42k
    MCOperand_CreateImm0(Inst, (Imm));                                     \
2031
7.42k
    return Success;                                                        \
2032
7.42k
  }
AArch64Disassembler.c:DecodeSImm_4
Line
Count
Source
2023
3.29k
  {                                                                          \
2024
3.29k
    if (Imm & ~((1LL << Bits) - 1))                                        \
2025
3.29k
      return Fail;                                                       \
2026
3.29k
                                                                               \
2027
3.29k
    if (Imm & (1 << (Bits - 1)))                                           \
2028
3.29k
      Imm |= ~((1LL << Bits) - 1);                                       \
2029
3.29k
                                                                               \
2030
3.29k
    MCOperand_CreateImm0(Inst, (Imm));                                     \
2031
3.29k
    return Success;                                                        \
2032
3.29k
  }
AArch64Disassembler.c:DecodeSImm_5
Line
Count
Source
2023
1.61k
  {                                                                          \
2024
1.61k
    if (Imm & ~((1LL << Bits) - 1))                                        \
2025
1.61k
      return Fail;                                                       \
2026
1.61k
                                                                               \
2027
1.61k
    if (Imm & (1 << (Bits - 1)))                                           \
2028
1.61k
      Imm |= ~((1LL << Bits) - 1);                                       \
2029
1.61k
                                                                               \
2030
1.61k
    MCOperand_CreateImm0(Inst, (Imm));                                     \
2031
1.61k
    return Success;                                                        \
2032
1.61k
  }
AArch64Disassembler.c:DecodeSImm_6
Line
Count
Source
2023
489
  {                                                                          \
2024
489
    if (Imm & ~((1LL << Bits) - 1))                                        \
2025
489
      return Fail;                                                       \
2026
489
                                                                               \
2027
489
    if (Imm & (1 << (Bits - 1)))                                           \
2028
489
      Imm |= ~((1LL << Bits) - 1);                                       \
2029
489
                                                                               \
2030
489
    MCOperand_CreateImm0(Inst, (Imm));                                     \
2031
489
    return Success;                                                        \
2032
489
  }
AArch64Disassembler.c:DecodeSImm_8
Line
Count
Source
2023
336
  {                                                                          \
2024
336
    if (Imm & ~((1LL << Bits) - 1))                                        \
2025
336
      return Fail;                                                       \
2026
336
                                                                               \
2027
336
    if (Imm & (1 << (Bits - 1)))                                           \
2028
336
      Imm |= ~((1LL << Bits) - 1);                                       \
2029
336
                                                                               \
2030
336
    MCOperand_CreateImm0(Inst, (Imm));                                     \
2031
336
    return Success;                                                        \
2032
336
  }
AArch64Disassembler.c:DecodeSImm_9
Line
Count
Source
2023
921
  {                                                                          \
2024
921
    if (Imm & ~((1LL << Bits) - 1))                                        \
2025
921
      return Fail;                                                       \
2026
921
                                                                               \
2027
921
    if (Imm & (1 << (Bits - 1)))                                           \
2028
921
      Imm |= ~((1LL << Bits) - 1);                                       \
2029
921
                                                                               \
2030
921
    MCOperand_CreateImm0(Inst, (Imm));                                     \
2031
921
    return Success;                                                        \
2032
921
  }
AArch64Disassembler.c:DecodeSImm_10
Line
Count
Source
2023
776
  {                                                                          \
2024
776
    if (Imm & ~((1LL << Bits) - 1))                                        \
2025
776
      return Fail;                                                       \
2026
776
                                                                               \
2027
776
    if (Imm & (1 << (Bits - 1)))                                           \
2028
776
      Imm |= ~((1LL << Bits) - 1);                                       \
2029
776
                                                                               \
2030
776
    MCOperand_CreateImm0(Inst, (Imm));                                     \
2031
776
    return Success;                                                        \
2032
776
  }
2033
DEFINE_DecodeSImm(4);
2034
DEFINE_DecodeSImm(5);
2035
DEFINE_DecodeSImm(6);
2036
DEFINE_DecodeSImm(8);
2037
DEFINE_DecodeSImm(9);
2038
DEFINE_DecodeSImm(10);
2039
2040
// Decode 8-bit signed/unsigned immediate for a given element width.
2041
#define DEFINE_DecodeImm8OptLsl(ElementWidth)                                  \
2042
  static DecodeStatus CONCAT(DecodeImm8OptLsl, ElementWidth)(                \
2043
    MCInst * Inst, unsigned Imm, uint64_t Addr, const void *Decoder)       \
2044
1.95k
  {                                                                          \
2045
1.95k
    unsigned Val = (uint8_t)Imm;                                           \
2046
1.95k
    unsigned Shift = (Imm & 0x100) ? 8 : 0;                                \
2047
1.95k
    if (ElementWidth == 8 && Shift)                                        \
2048
1.95k
      return Fail;                                                       \
2049
1.95k
    MCOperand_CreateImm0(Inst, (Val));                                     \
2050
1.94k
    MCOperand_CreateImm0(Inst, (Shift));                                   \
2051
1.94k
    return Success;                                                        \
2052
1.95k
  }
AArch64Disassembler.c:DecodeImm8OptLsl_8
Line
Count
Source
2044
201
  {                                                                          \
2045
201
    unsigned Val = (uint8_t)Imm;                                           \
2046
201
    unsigned Shift = (Imm & 0x100) ? 8 : 0;                                \
2047
201
    if (ElementWidth == 8 && Shift)                                        \
2048
201
      return Fail;                                                       \
2049
201
    MCOperand_CreateImm0(Inst, (Val));                                     \
2050
194
    MCOperand_CreateImm0(Inst, (Shift));                                   \
2051
194
    return Success;                                                        \
2052
201
  }
AArch64Disassembler.c:DecodeImm8OptLsl_16
Line
Count
Source
2044
924
  {                                                                          \
2045
924
    unsigned Val = (uint8_t)Imm;                                           \
2046
924
    unsigned Shift = (Imm & 0x100) ? 8 : 0;                                \
2047
924
    if (ElementWidth == 8 && Shift)                                        \
2048
924
      return Fail;                                                       \
2049
924
    MCOperand_CreateImm0(Inst, (Val));                                     \
2050
924
    MCOperand_CreateImm0(Inst, (Shift));                                   \
2051
924
    return Success;                                                        \
2052
924
  }
AArch64Disassembler.c:DecodeImm8OptLsl_32
Line
Count
Source
2044
336
  {                                                                          \
2045
336
    unsigned Val = (uint8_t)Imm;                                           \
2046
336
    unsigned Shift = (Imm & 0x100) ? 8 : 0;                                \
2047
336
    if (ElementWidth == 8 && Shift)                                        \
2048
336
      return Fail;                                                       \
2049
336
    MCOperand_CreateImm0(Inst, (Val));                                     \
2050
336
    MCOperand_CreateImm0(Inst, (Shift));                                   \
2051
336
    return Success;                                                        \
2052
336
  }
AArch64Disassembler.c:DecodeImm8OptLsl_64
Line
Count
Source
2044
491
  {                                                                          \
2045
491
    unsigned Val = (uint8_t)Imm;                                           \
2046
491
    unsigned Shift = (Imm & 0x100) ? 8 : 0;                                \
2047
491
    if (ElementWidth == 8 && Shift)                                        \
2048
491
      return Fail;                                                       \
2049
491
    MCOperand_CreateImm0(Inst, (Val));                                     \
2050
491
    MCOperand_CreateImm0(Inst, (Shift));                                   \
2051
491
    return Success;                                                        \
2052
491
  }
2053
DEFINE_DecodeImm8OptLsl(8);
2054
DEFINE_DecodeImm8OptLsl(16);
2055
DEFINE_DecodeImm8OptLsl(32);
2056
DEFINE_DecodeImm8OptLsl(64);
2057
2058
// Decode uimm4 ranged from 1-16.
2059
static DecodeStatus DecodeSVEIncDecImm(MCInst *Inst, unsigned Imm,
2060
                     uint64_t Addr, const void *Decoder)
2061
2.36k
{
2062
2.36k
  MCOperand_CreateImm0(Inst, (Imm + 1));
2063
2.36k
  return Success;
2064
2.36k
}
2065
2066
static DecodeStatus DecodeSVCROp(MCInst *Inst, unsigned Imm, uint64_t Address,
2067
                 const void *Decoder)
2068
804
{
2069
804
  if (AArch64SVCR_lookupSVCRByEncoding(Imm)) {
2070
15
    MCOperand_CreateImm0(Inst, (Imm));
2071
15
    return Success;
2072
15
  }
2073
789
  return Fail;
2074
804
}
2075
2076
static DecodeStatus DecodeCPYMemOpInstruction(MCInst *Inst, uint32_t insn,
2077
                        uint64_t Addr,
2078
                        const void *Decoder)
2079
45
{
2080
45
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
2081
45
  unsigned Rs = fieldFromInstruction_4(insn, 16, 5);
2082
45
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
2083
2084
  // None of the registers may alias: if they do, then the instruction is not
2085
  // merely unpredictable but actually entirely unallocated.
2086
45
  if (Rd == Rs || Rs == Rn || Rd == Rn)
2087
3
    return MCDisassembler_Fail;
2088
2089
  // All three register operands are written back, so they all appear
2090
  // twice in the operand list, once as outputs and once as inputs.
2091
42
  if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2092
42
    !DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||
2093
42
    !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2094
42
    !DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2095
42
    !DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||
2096
42
    !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder))
2097
2
    return MCDisassembler_Fail;
2098
2099
40
  return MCDisassembler_Success;
2100
42
}
2101
2102
static DecodeStatus DecodeSETMemOpInstruction(MCInst *Inst, uint32_t insn,
2103
                        uint64_t Addr,
2104
                        const void *Decoder)
2105
23
{
2106
23
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
2107
23
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
2108
23
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
2109
2110
  // None of the registers may alias: if they do, then the instruction is not
2111
  // merely unpredictable but actually entirely unallocated.
2112
23
  if (Rd == Rm || Rm == Rn || Rd == Rn)
2113
3
    return MCDisassembler_Fail;
2114
2115
  // Rd and Rn (not Rm) register operands are written back, so they appear
2116
  // twice in the operand list, once as outputs and once as inputs.
2117
20
  if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2118
20
    !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2119
20
    !DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2120
20
    !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2121
20
    !DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder))
2122
2
    return MCDisassembler_Fail;
2123
2124
18
  return MCDisassembler_Success;
2125
20
}
2126
2127
static DecodeStatus DecodePRFMRegInstruction(MCInst *Inst, uint32_t insn,
2128
                       uint64_t Addr, const void *Decoder)
2129
161
{
2130
  // PRFM with Rt = '11xxx' should be decoded as RPRFM.
2131
  // Fail to decode and defer to fallback decoder table to decode RPRFM.
2132
161
  unsigned Mask = 0x18;
2133
161
  uint64_t Rt = fieldFromInstruction_4(insn, 0, 5);
2134
161
  if ((Rt & Mask) == Mask)
2135
41
    return Fail;
2136
2137
120
  uint64_t Rn = fieldFromInstruction_4(insn, 5, 5);
2138
120
  uint64_t Shift = fieldFromInstruction_4(insn, 12, 1);
2139
120
  uint64_t Extend = fieldFromInstruction_4(insn, 15, 1);
2140
120
  uint64_t Rm = fieldFromInstruction_4(insn, 16, 5);
2141
2142
120
  MCOperand_CreateImm0(Inst, (Rt));
2143
120
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
2144
2145
120
  switch (MCInst_getOpcode(Inst)) {
2146
0
  default:
2147
0
    return Fail;
2148
43
  case AArch64_PRFMroW:
2149
43
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
2150
43
    break;
2151
77
  case AArch64_PRFMroX:
2152
77
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
2153
77
    break;
2154
120
  }
2155
2156
120
  DecodeMemExtend(Inst, (Extend << 1) | Shift, Addr, Decoder);
2157
2158
120
  return Success;
2159
120
}