Coverage Report

Created: 2025-08-26 06:30

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