Coverage Report

Created: 2025-07-11 06:32

/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
4.08k
#define CONCAT(a, b) CONCAT_(a, b)
49
4.08k
#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.68M
#define Success MCDisassembler_Success
331
8.36k
#define Fail MCDisassembler_Fail
332
1.18k
#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
288k
{
338
288k
  *Size = 0;
339
  // We want to read exactly 4 bytes of data.
340
288k
  if (ByteLen < 4)
341
3.69k
    return Fail;
342
285k
  *Size = 4;
343
344
  // Encoded as a small-endian 32-bit word in the stream.
345
285k
  uint32_t Insn = readBytes32(MI, Bytes);
346
347
285k
  const uint8_t *Tables[] = { DecoderTable32, DecoderTableFallback32 };
348
349
295k
  for (int i = 0; i < (sizeof(Tables) / sizeof(Tables[0])); ++i) {
350
293k
    void *Decoder = NULL;
351
293k
    DecodeStatus Result = decodeInstruction_4(Tables[i], MI, Insn,
352
293k
                Address, Decoder);
353
354
    // Table is indexed backwards
355
293k
    const MCInstrDesc Desc =
356
293k
      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.31M
    for (unsigned j = 0; j < Desc.NumOperands; j++) {
362
1.02M
      if (Desc.OpInfo[j].OperandType ==
363
1.02M
          MCOI_OPERAND_REGISTER) {
364
724k
        switch (Desc.OpInfo[j].RegClass) {
365
700k
        default:
366
700k
          break;
367
700k
        case AArch64_MPRRegClassID:
368
21.2k
          MCInst_insert0(MI, j,
369
21.2k
                   MCOperand_CreateReg1(
370
21.2k
                     MI, AArch64_ZA));
371
21.2k
          break;
372
2.12k
        case AArch64_MPR8RegClassID:
373
2.12k
          MCInst_insert0(MI, j,
374
2.12k
                   MCOperand_CreateReg1(
375
2.12k
                     MI,
376
2.12k
                     AArch64_ZAB0));
377
2.12k
          break;
378
919
        case AArch64_ZTRRegClassID:
379
919
          MCInst_insert0(MI, j,
380
919
                   MCOperand_CreateReg1(
381
919
                     MI,
382
919
                     AArch64_ZT0));
383
919
          break;
384
724k
        }
385
724k
      } else if (Desc.OpInfo[j].OperandType ==
386
300k
           AARCH64_OP_IMPLICIT_IMM_0) {
387
1.94k
        MCInst_insert0(MI, j,
388
1.94k
                 MCOperand_CreateImm1(MI, 0));
389
1.94k
      }
390
1.02M
    }
391
392
293k
    if (MCInst_getOpcode(MI) == AArch64_LDR_ZA ||
393
293k
        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
177
      MCOperand *Imm4Op = MCInst_getOperand(MI, (2));
398
399
177
      MCInst_addOperand2(MI, (Imm4Op));
400
177
    }
401
402
293k
    if (Result != MCDisassembler_Fail)
403
282k
      return Result;
404
293k
  }
405
406
2.42k
  return MCDisassembler_Fail;
407
285k
}
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
288k
{
414
288k
  DecodeStatus Result = MCDisassembler_Fail;
415
288k
  Result =
416
288k
    getInstruction(handle, Bytes, ByteLen, MI, Size, Address, Info);
417
288k
  MCInst_handleWriteback(MI, AArch64Descs.Insts, ARR_SIZE(AArch64Descs.Insts));
418
288k
  return Result;
419
288k
}
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
124k
{
433
124k
  if (RegNo > 31)
434
0
    return Fail;
435
436
124k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR128RegClassID]
437
124k
            .RegsBegin[RegNo];
438
124k
  MCOperand_CreateReg0(Inst, (Register));
439
124k
  return Success;
440
124k
}
441
442
static DecodeStatus DecodeFPR128_loRegisterClass(MCInst *Inst, unsigned RegNo,
443
             uint64_t Addr,
444
             const void *Decoder)
445
3.31k
{
446
3.31k
  if (RegNo > 15)
447
0
    return Fail;
448
3.31k
  return DecodeFPR128RegisterClass(Inst, RegNo, Addr, Decoder);
449
3.31k
}
450
451
static DecodeStatus DecodeFPR128_0to7RegisterClass(MCInst *Inst, unsigned RegNo,
452
               uint64_t Addr,
453
               const void *Decoder)
454
81
{
455
81
  if (RegNo > 7)
456
0
    return Fail;
457
81
  return DecodeFPR128RegisterClass(Inst, RegNo, Addr, Decoder);
458
81
}
459
460
static DecodeStatus DecodeFPR64RegisterClass(MCInst *Inst, unsigned RegNo,
461
               uint64_t Addr, const void *Decoder)
462
80.9k
{
463
80.9k
  if (RegNo > 31)
464
0
    return Fail;
465
466
80.9k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR64RegClassID]
467
80.9k
            .RegsBegin[RegNo];
468
80.9k
  MCOperand_CreateReg0(Inst, (Register));
469
80.9k
  return Success;
470
80.9k
}
471
472
static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, unsigned RegNo,
473
               uint64_t Addr, const void *Decoder)
474
34.8k
{
475
34.8k
  if (RegNo > 31)
476
0
    return Fail;
477
478
34.8k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR32RegClassID]
479
34.8k
            .RegsBegin[RegNo];
480
34.8k
  MCOperand_CreateReg0(Inst, (Register));
481
34.8k
  return Success;
482
34.8k
}
483
484
static DecodeStatus DecodeFPR16RegisterClass(MCInst *Inst, unsigned RegNo,
485
               uint64_t Addr, const void *Decoder)
486
23.6k
{
487
23.6k
  if (RegNo > 31)
488
0
    return Fail;
489
490
23.6k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR16RegClassID]
491
23.6k
            .RegsBegin[RegNo];
492
23.6k
  MCOperand_CreateReg0(Inst, (Register));
493
23.6k
  return Success;
494
23.6k
}
495
496
static DecodeStatus DecodeFPR8RegisterClass(MCInst *Inst, unsigned RegNo,
497
              uint64_t Addr, const void *Decoder)
498
12.4k
{
499
12.4k
  if (RegNo > 31)
500
0
    return Fail;
501
502
12.4k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR8RegClassID]
503
12.4k
            .RegsBegin[RegNo];
504
12.4k
  MCOperand_CreateReg0(Inst, (Register));
505
12.4k
  return Success;
506
12.4k
}
507
508
static DecodeStatus DecodeGPR64commonRegisterClass(MCInst *Inst, unsigned RegNo,
509
               uint64_t Addr,
510
               const void *Decoder)
511
10.8k
{
512
10.8k
  if (RegNo > 30)
513
22
    return Fail;
514
515
10.7k
  unsigned Register =
516
10.7k
    AArch64MCRegisterClasses[AArch64_GPR64commonRegClassID]
517
10.7k
      .RegsBegin[RegNo];
518
10.7k
  MCOperand_CreateReg0(Inst, (Register));
519
10.7k
  return Success;
520
10.8k
}
521
522
static DecodeStatus DecodeGPR64RegisterClass(MCInst *Inst, unsigned RegNo,
523
               uint64_t Addr, const void *Decoder)
524
295k
{
525
295k
  if (RegNo > 31)
526
0
    return Fail;
527
528
295k
  unsigned Register = AArch64MCRegisterClasses[AArch64_GPR64RegClassID]
529
295k
            .RegsBegin[RegNo];
530
295k
  MCOperand_CreateReg0(Inst, (Register));
531
295k
  return Success;
532
295k
}
533
534
static DecodeStatus DecodeGPR64x8ClassRegisterClass(MCInst *Inst,
535
                unsigned RegNo,
536
                uint64_t Address,
537
                const void *Decoder)
538
1.04k
{
539
1.04k
  if (RegNo > 22)
540
5
    return Fail;
541
1.03k
  if (RegNo & 1)
542
4
    return Fail;
543
544
1.03k
  unsigned Register =
545
1.03k
    AArch64MCRegisterClasses[AArch64_GPR64x8ClassRegClassID]
546
1.03k
      .RegsBegin[RegNo >> 1];
547
1.03k
  MCOperand_CreateReg0(Inst, (Register));
548
1.03k
  return Success;
549
1.03k
}
550
551
static DecodeStatus DecodeGPR64spRegisterClass(MCInst *Inst, unsigned RegNo,
552
                 uint64_t Addr,
553
                 const void *Decoder)
554
133k
{
555
133k
  if (RegNo > 31)
556
0
    return Fail;
557
133k
  unsigned Register = AArch64MCRegisterClasses[AArch64_GPR64spRegClassID]
558
133k
            .RegsBegin[RegNo];
559
133k
  MCOperand_CreateReg0(Inst, (Register));
560
133k
  return Success;
561
133k
}
562
563
static DecodeStatus
564
DecodeMatrixIndexGPR32_8_11RegisterClass(MCInst *Inst, unsigned RegNo,
565
           uint64_t Addr, const void *Decoder)
566
10.6k
{
567
10.6k
  if (RegNo > 3)
568
0
    return Fail;
569
570
10.6k
  unsigned Register =
571
10.6k
    AArch64MCRegisterClasses[AArch64_MatrixIndexGPR32_8_11RegClassID]
572
10.6k
      .RegsBegin[RegNo];
573
10.6k
  MCOperand_CreateReg0(Inst, (Register));
574
10.6k
  return Success;
575
10.6k
}
576
577
static DecodeStatus
578
DecodeMatrixIndexGPR32_12_15RegisterClass(MCInst *Inst, unsigned RegNo,
579
            uint64_t Addr, const void *Decoder)
580
16.2k
{
581
16.2k
  if (RegNo > 3)
582
0
    return Fail;
583
584
16.2k
  unsigned Register =
585
16.2k
    AArch64MCRegisterClasses[AArch64_MatrixIndexGPR32_12_15RegClassID]
586
16.2k
      .RegsBegin[RegNo];
587
16.2k
  MCOperand_CreateReg0(Inst, (Register));
588
16.2k
  return Success;
589
16.2k
}
590
591
static DecodeStatus DecodeGPR32RegisterClass(MCInst *Inst, unsigned RegNo,
592
               uint64_t Addr, const void *Decoder)
593
128k
{
594
128k
  if (RegNo > 31)
595
0
    return Fail;
596
597
128k
  unsigned Register = AArch64MCRegisterClasses[AArch64_GPR32RegClassID]
598
128k
            .RegsBegin[RegNo];
599
128k
  MCOperand_CreateReg0(Inst, (Register));
600
128k
  return Success;
601
128k
}
602
603
static DecodeStatus DecodeGPR32spRegisterClass(MCInst *Inst, unsigned RegNo,
604
                 uint64_t Addr,
605
                 const void *Decoder)
606
5.92k
{
607
5.92k
  if (RegNo > 31)
608
0
    return Fail;
609
610
5.92k
  unsigned Register = AArch64MCRegisterClasses[AArch64_GPR32spRegClassID]
611
5.92k
            .RegsBegin[RegNo];
612
5.92k
  MCOperand_CreateReg0(Inst, (Register));
613
5.92k
  return Success;
614
5.92k
}
615
616
static DecodeStatus DecodeZPRRegisterClass(MCInst *Inst, unsigned RegNo,
617
             uint64_t Address,
618
             const void *Decoder)
619
259k
{
620
259k
  if (RegNo > 31)
621
0
    return Fail;
622
623
259k
  unsigned Register =
624
259k
    AArch64MCRegisterClasses[AArch64_ZPRRegClassID].RegsBegin[RegNo];
625
259k
  MCOperand_CreateReg0(Inst, (Register));
626
259k
  return Success;
627
259k
}
628
629
static DecodeStatus DecodeZPR_4bRegisterClass(MCInst *Inst, unsigned RegNo,
630
                uint64_t Address,
631
                const void *Decoder)
632
10.4k
{
633
10.4k
  if (RegNo > 15)
634
0
    return Fail;
635
10.4k
  return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);
636
10.4k
}
637
638
static DecodeStatus DecodeZPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,
639
                uint64_t Address,
640
                const void *Decoder)
641
2.74k
{
642
2.74k
  if (RegNo > 7)
643
0
    return Fail;
644
2.74k
  return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);
645
2.74k
}
646
647
static DecodeStatus DecodeZPR2RegisterClass(MCInst *Inst, unsigned RegNo,
648
              uint64_t Address,
649
              const void *Decoder)
650
3.57k
{
651
3.57k
  if (RegNo > 31)
652
0
    return Fail;
653
3.57k
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR2RegClassID]
654
3.57k
            .RegsBegin[RegNo];
655
3.57k
  MCOperand_CreateReg0(Inst, (Register));
656
3.57k
  return Success;
657
3.57k
}
658
659
static DecodeStatus DecodeZPR3RegisterClass(MCInst *Inst, unsigned RegNo,
660
              uint64_t Address,
661
              const void *Decoder)
662
2.25k
{
663
2.25k
  if (RegNo > 31)
664
0
    return Fail;
665
2.25k
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR3RegClassID]
666
2.25k
            .RegsBegin[RegNo];
667
2.25k
  MCOperand_CreateReg0(Inst, (Register));
668
2.25k
  return Success;
669
2.25k
}
670
671
static DecodeStatus DecodeZPR4RegisterClass(MCInst *Inst, unsigned RegNo,
672
              uint64_t Address,
673
              const void *Decoder)
674
3.32k
{
675
3.32k
  if (RegNo > 31)
676
0
    return Fail;
677
3.32k
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR4RegClassID]
678
3.32k
            .RegsBegin[RegNo];
679
3.32k
  MCOperand_CreateReg0(Inst, (Register));
680
3.32k
  return Success;
681
3.32k
}
682
683
static DecodeStatus DecodeZPR2Mul2RegisterClass(MCInst *Inst, unsigned RegNo,
684
            uint64_t Address,
685
            const void *Decoder)
686
9.41k
{
687
9.41k
  if (RegNo * 2 > 30)
688
0
    return Fail;
689
9.41k
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR2RegClassID]
690
9.41k
            .RegsBegin[RegNo * 2];
691
9.41k
  MCOperand_CreateReg0(Inst, (Register));
692
9.41k
  return Success;
693
9.41k
}
694
695
static DecodeStatus DecodeZPR4Mul4RegisterClass(MCInst *Inst, unsigned RegNo,
696
            uint64_t Address,
697
            const void *Decoder)
698
6.84k
{
699
6.84k
  if (RegNo * 4 > 28)
700
0
    return Fail;
701
6.84k
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR4RegClassID]
702
6.84k
            .RegsBegin[RegNo * 4];
703
6.84k
  MCOperand_CreateReg0(Inst, (Register));
704
6.84k
  return Success;
705
6.84k
}
706
707
static DecodeStatus DecodeZPR2StridedRegisterClass(MCInst *Inst, unsigned RegNo,
708
               uint64_t Address,
709
               const void *Decoder)
710
1.87k
{
711
1.87k
  if (RegNo > 15)
712
0
    return Fail;
713
1.87k
  unsigned Register =
714
1.87k
    AArch64MCRegisterClasses[AArch64_ZPR2StridedRegClassID]
715
1.87k
      .RegsBegin[RegNo];
716
1.87k
  MCOperand_CreateReg0(Inst, (Register));
717
1.87k
  return Success;
718
1.87k
}
719
720
static DecodeStatus DecodeZPR4StridedRegisterClass(MCInst *Inst, unsigned RegNo,
721
               uint64_t Address,
722
               const void *Decoder)
723
1.20k
{
724
1.20k
  if (RegNo > 7)
725
0
    return Fail;
726
1.20k
  unsigned Register =
727
1.20k
    AArch64MCRegisterClasses[AArch64_ZPR4StridedRegClassID]
728
1.20k
      .RegsBegin[RegNo];
729
1.20k
  MCOperand_CreateReg0(Inst, (Register));
730
1.20k
  return Success;
731
1.20k
}
732
733
static DecodeStatus DecodeMatrixTileListRegisterClass(MCInst *Inst,
734
                  unsigned RegMask,
735
                  uint64_t Address,
736
                  const void *Decoder)
737
2.52k
{
738
2.52k
  if (RegMask > 0xFF)
739
0
    return Fail;
740
2.52k
  MCOperand_CreateImm0(Inst, (RegMask));
741
2.52k
  return Success;
742
2.52k
}
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
10.1k
  { \
761
10.1k
    unsigned LastReg = (1 << NumBitsForTile) - 1; \
762
10.1k
    if (RegNo > LastReg) \
763
10.1k
      return Fail; \
764
10.1k
    MCOperand_CreateReg0( \
765
10.1k
      Inst, \
766
10.1k
      (MatrixZATileDecoderTable[NumBitsForTile][RegNo])); \
767
10.1k
    return Success; \
768
10.1k
  }
AArch64Disassembler.c:DecodeMatrixTile_2
Line
Count
Source
760
3.55k
  { \
761
3.55k
    unsigned LastReg = (1 << NumBitsForTile) - 1; \
762
3.55k
    if (RegNo > LastReg) \
763
3.55k
      return Fail; \
764
3.55k
    MCOperand_CreateReg0( \
765
3.55k
      Inst, \
766
3.55k
      (MatrixZATileDecoderTable[NumBitsForTile][RegNo])); \
767
3.55k
    return Success; \
768
3.55k
  }
AArch64Disassembler.c:DecodeMatrixTile_1
Line
Count
Source
760
1.03k
  { \
761
1.03k
    unsigned LastReg = (1 << NumBitsForTile) - 1; \
762
1.03k
    if (RegNo > LastReg) \
763
1.03k
      return Fail; \
764
1.03k
    MCOperand_CreateReg0( \
765
1.03k
      Inst, \
766
1.03k
      (MatrixZATileDecoderTable[NumBitsForTile][RegNo])); \
767
1.03k
    return Success; \
768
1.03k
  }
AArch64Disassembler.c:DecodeMatrixTile_3
Line
Count
Source
760
4.56k
  { \
761
4.56k
    unsigned LastReg = (1 << NumBitsForTile) - 1; \
762
4.56k
    if (RegNo > LastReg) \
763
4.56k
      return Fail; \
764
4.56k
    MCOperand_CreateReg0( \
765
4.56k
      Inst, \
766
4.56k
      (MatrixZATileDecoderTable[NumBitsForTile][RegNo])); \
767
4.56k
    return Success; \
768
4.56k
  }
AArch64Disassembler.c:DecodeMatrixTile_4
Line
Count
Source
760
1.02k
  { \
761
1.02k
    unsigned LastReg = (1 << NumBitsForTile) - 1; \
762
1.02k
    if (RegNo > LastReg) \
763
1.02k
      return Fail; \
764
1.02k
    MCOperand_CreateReg0( \
765
1.02k
      Inst, \
766
1.02k
      (MatrixZATileDecoderTable[NumBitsForTile][RegNo])); \
767
1.02k
    return Success; \
768
1.02k
  }
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
116k
{
777
116k
  if (RegNo > 15)
778
0
    return Fail;
779
780
116k
  unsigned Register =
781
116k
    AArch64MCRegisterClasses[AArch64_PPRRegClassID].RegsBegin[RegNo];
782
116k
  MCOperand_CreateReg0(Inst, (Register));
783
116k
  return Success;
784
116k
}
785
786
static DecodeStatus DecodePNRRegisterClass(MCInst *Inst, unsigned RegNo,
787
             uint64_t Addr, const void *Decoder)
788
6.43k
{
789
6.43k
  if (RegNo > 15)
790
0
    return Fail;
791
792
6.43k
  unsigned Register =
793
6.43k
    AArch64MCRegisterClasses[AArch64_PNRRegClassID].RegsBegin[RegNo];
794
6.43k
  MCOperand_CreateReg0(Inst, (Register));
795
6.43k
  return Success;
796
6.43k
}
797
798
static DecodeStatus DecodePPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,
799
                uint64_t Addr,
800
                const void *Decoder)
801
81.3k
{
802
81.3k
  if (RegNo > 7)
803
0
    return Fail;
804
805
  // Just reuse the PPR decode table
806
81.3k
  return DecodePPRRegisterClass(Inst, RegNo, Addr, Decoder);
807
81.3k
}
808
809
static DecodeStatus DecodePNR_p8to15RegisterClass(MCInst *Inst, unsigned RegNo,
810
              uint64_t Addr,
811
              const void *Decoder)
812
6.24k
{
813
6.24k
  if (RegNo > 7)
814
0
    return Fail;
815
816
  // Just reuse the PPR decode table
817
6.24k
  return DecodePNRRegisterClass(Inst, RegNo + 8, Addr, Decoder);
818
6.24k
}
819
820
static DecodeStatus DecodePPR2RegisterClass(MCInst *Inst, unsigned RegNo,
821
              uint64_t Address,
822
              const void *Decoder)
823
353
{
824
353
  if (RegNo > 15)
825
0
    return Fail;
826
827
353
  unsigned Register = AArch64MCRegisterClasses[AArch64_PPR2RegClassID]
828
353
            .RegsBegin[RegNo];
829
353
  MCOperand_CreateReg0(Inst, (Register));
830
353
  return Success;
831
353
}
832
833
static DecodeStatus DecodePPR2Mul2RegisterClass(MCInst *Inst, unsigned RegNo,
834
            uint64_t Address,
835
            const void *Decoder)
836
197
{
837
197
  if ((RegNo * 2) > 14)
838
0
    return Fail;
839
197
  unsigned Register = AArch64MCRegisterClasses[AArch64_PPR2RegClassID]
840
197
            .RegsBegin[RegNo * 2];
841
197
  MCOperand_CreateReg0(Inst, (Register));
842
197
  return Success;
843
197
}
844
845
static DecodeStatus DecodeQQRegisterClass(MCInst *Inst, unsigned RegNo,
846
            uint64_t Addr, const void *Decoder)
847
34.2k
{
848
34.2k
  if (RegNo > 31)
849
0
    return Fail;
850
34.2k
  unsigned Register =
851
34.2k
    AArch64MCRegisterClasses[AArch64_QQRegClassID].RegsBegin[RegNo];
852
34.2k
  MCOperand_CreateReg0(Inst, (Register));
853
34.2k
  return Success;
854
34.2k
}
855
856
static DecodeStatus DecodeQQQRegisterClass(MCInst *Inst, unsigned RegNo,
857
             uint64_t Addr, const void *Decoder)
858
40.9k
{
859
40.9k
  if (RegNo > 31)
860
0
    return Fail;
861
40.9k
  unsigned Register =
862
40.9k
    AArch64MCRegisterClasses[AArch64_QQQRegClassID].RegsBegin[RegNo];
863
40.9k
  MCOperand_CreateReg0(Inst, (Register));
864
40.9k
  return Success;
865
40.9k
}
866
867
static DecodeStatus DecodeQQQQRegisterClass(MCInst *Inst, unsigned RegNo,
868
              uint64_t Addr, const void *Decoder)
869
35.6k
{
870
35.6k
  if (RegNo > 31)
871
0
    return Fail;
872
35.6k
  unsigned Register = AArch64MCRegisterClasses[AArch64_QQQQRegClassID]
873
35.6k
            .RegsBegin[RegNo];
874
35.6k
  MCOperand_CreateReg0(Inst, (Register));
875
35.6k
  return Success;
876
35.6k
}
877
878
static DecodeStatus DecodeDDRegisterClass(MCInst *Inst, unsigned RegNo,
879
            uint64_t Addr, const void *Decoder)
880
5.27k
{
881
5.27k
  if (RegNo > 31)
882
0
    return Fail;
883
5.27k
  unsigned Register =
884
5.27k
    AArch64MCRegisterClasses[AArch64_DDRegClassID].RegsBegin[RegNo];
885
5.27k
  MCOperand_CreateReg0(Inst, (Register));
886
5.27k
  return Success;
887
5.27k
}
888
889
static DecodeStatus DecodeDDDRegisterClass(MCInst *Inst, unsigned RegNo,
890
             uint64_t Addr, const void *Decoder)
891
8.39k
{
892
8.39k
  if (RegNo > 31)
893
0
    return Fail;
894
8.39k
  unsigned Register =
895
8.39k
    AArch64MCRegisterClasses[AArch64_DDDRegClassID].RegsBegin[RegNo];
896
8.39k
  MCOperand_CreateReg0(Inst, (Register));
897
8.39k
  return Success;
898
8.39k
}
899
900
static DecodeStatus DecodeDDDDRegisterClass(MCInst *Inst, unsigned RegNo,
901
              uint64_t Addr, const void *Decoder)
902
7.26k
{
903
7.26k
  if (RegNo > 31)
904
0
    return Fail;
905
7.26k
  unsigned Register = AArch64MCRegisterClasses[AArch64_DDDDRegClassID]
906
7.26k
            .RegsBegin[RegNo];
907
7.26k
  MCOperand_CreateReg0(Inst, (Register));
908
7.26k
  return Success;
909
7.26k
}
910
911
static DecodeStatus DecodeFixedPointScaleImm32(MCInst *Inst, unsigned Imm,
912
                 uint64_t Addr,
913
                 const void *Decoder)
914
694
{
915
  // scale{5} is asserted as 1 in tblgen.
916
694
  Imm |= 0x20;
917
694
  MCOperand_CreateImm0(Inst, (64 - Imm));
918
694
  return Success;
919
694
}
920
921
static DecodeStatus DecodeFixedPointScaleImm64(MCInst *Inst, unsigned Imm,
922
                 uint64_t Addr,
923
                 const void *Decoder)
924
635
{
925
635
  MCOperand_CreateImm0(Inst, (64 - Imm));
926
635
  return Success;
927
635
}
928
929
static DecodeStatus DecodePCRelLabel16(MCInst *Inst, unsigned Imm,
930
               uint64_t Addr, const void *Decoder)
931
192
{
932
  // Immediate is encoded as the top 16-bits of an unsigned 18-bit negative
933
  // PC-relative offset.
934
192
  uint64_t ImmVal = Imm;
935
192
  if (ImmVal > (1 << 16))
936
0
    return Fail;
937
  // Symbols are not supported by Capstone
938
192
  return Success;
939
192
}
940
941
static DecodeStatus DecodePCRelLabel19(MCInst *Inst, unsigned Imm,
942
               uint64_t Addr, const void *Decoder)
943
15.2k
{
944
15.2k
  int64_t ImmVal = Imm;
945
946
  // Sign-extend 19-bit immediate.
947
15.2k
  if (ImmVal & (1 << (19 - 1)))
948
5.83k
    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
15.2k
  MCOperand_CreateImm0(Inst, (ImmVal));
955
15.2k
  return Success;
956
15.2k
}
957
958
static DecodeStatus DecodeMemExtend(MCInst *Inst, unsigned Imm,
959
            uint64_t Address, const void *Decoder)
960
11.1k
{
961
11.1k
  MCOperand_CreateImm0(Inst, ((Imm >> 1) & 1));
962
11.1k
  MCOperand_CreateImm0(Inst, (Imm & 1));
963
11.1k
  return Success;
964
11.1k
}
965
966
static DecodeStatus DecodeMRSSystemRegister(MCInst *Inst, unsigned Imm,
967
              uint64_t Address,
968
              const void *Decoder)
969
4.92k
{
970
4.92k
  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
4.92k
  return Success;
976
4.92k
}
977
978
static DecodeStatus DecodeMSRSystemRegister(MCInst *Inst, unsigned Imm,
979
              uint64_t Address,
980
              const void *Decoder)
981
7.75k
{
982
7.75k
  MCOperand_CreateImm0(Inst, (Imm));
983
984
7.75k
  return Success;
985
7.75k
}
986
987
static DecodeStatus DecodeFMOVLaneInstruction(MCInst *Inst, unsigned Insn,
988
                uint64_t Address,
989
                const void *Decoder)
990
635
{
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
635
  unsigned Rd = fieldFromInstruction_4(Insn, 0, 5);
994
635
  unsigned Rn = fieldFromInstruction_4(Insn, 5, 5);
995
635
  unsigned IsToVec = fieldFromInstruction_4(Insn, 16, 1);
996
997
635
  if (IsToVec) {
998
392
    DecodeFPR128RegisterClass(Inst, Rd, Address, Decoder);
999
392
    DecodeGPR64RegisterClass(Inst, Rn, Address, Decoder);
1000
392
  } else {
1001
243
    DecodeGPR64RegisterClass(Inst, Rd, Address, Decoder);
1002
243
    DecodeFPR128RegisterClass(Inst, Rn, Address, Decoder);
1003
243
  }
1004
1005
  // Add the lane
1006
635
  MCOperand_CreateImm0(Inst, (1));
1007
1008
635
  return Success;
1009
635
}
1010
1011
static DecodeStatus DecodeVecShiftRImm(MCInst *Inst, unsigned Imm, unsigned Add)
1012
9.58k
{
1013
9.58k
  MCOperand_CreateImm0(Inst, (Add - Imm));
1014
9.58k
  return Success;
1015
9.58k
}
1016
1017
static DecodeStatus DecodeVecShiftLImm(MCInst *Inst, unsigned Imm, unsigned Add)
1018
13.2k
{
1019
13.2k
  MCOperand_CreateImm0(Inst, ((Imm + Add) & (Add - 1)));
1020
13.2k
  return Success;
1021
13.2k
}
1022
1023
static DecodeStatus DecodeVecShiftR64Imm(MCInst *Inst, unsigned Imm,
1024
           uint64_t Addr, const void *Decoder)
1025
1.42k
{
1026
1.42k
  return DecodeVecShiftRImm(Inst, Imm, 64);
1027
1.42k
}
1028
1029
static DecodeStatus DecodeVecShiftR64ImmNarrow(MCInst *Inst, unsigned Imm,
1030
                 uint64_t Addr,
1031
                 const void *Decoder)
1032
829
{
1033
829
  return DecodeVecShiftRImm(Inst, Imm | 0x20, 64);
1034
829
}
1035
1036
static DecodeStatus DecodeVecShiftR32Imm(MCInst *Inst, unsigned Imm,
1037
           uint64_t Addr, const void *Decoder)
1038
2.04k
{
1039
2.04k
  return DecodeVecShiftRImm(Inst, Imm, 32);
1040
2.04k
}
1041
1042
static DecodeStatus DecodeVecShiftR32ImmNarrow(MCInst *Inst, unsigned Imm,
1043
                 uint64_t Addr,
1044
                 const void *Decoder)
1045
113
{
1046
113
  return DecodeVecShiftRImm(Inst, Imm | 0x10, 32);
1047
113
}
1048
1049
static DecodeStatus DecodeVecShiftR16Imm(MCInst *Inst, unsigned Imm,
1050
           uint64_t Addr, const void *Decoder)
1051
2.51k
{
1052
2.51k
  return DecodeVecShiftRImm(Inst, Imm, 16);
1053
2.51k
}
1054
1055
static DecodeStatus DecodeVecShiftR16ImmNarrow(MCInst *Inst, unsigned Imm,
1056
                 uint64_t Addr,
1057
                 const void *Decoder)
1058
248
{
1059
248
  return DecodeVecShiftRImm(Inst, Imm | 0x8, 16);
1060
248
}
1061
1062
static DecodeStatus DecodeVecShiftR8Imm(MCInst *Inst, unsigned Imm,
1063
          uint64_t Addr, const void *Decoder)
1064
2.40k
{
1065
2.40k
  return DecodeVecShiftRImm(Inst, Imm, 8);
1066
2.40k
}
1067
1068
static DecodeStatus DecodeVecShiftL64Imm(MCInst *Inst, unsigned Imm,
1069
           uint64_t Addr, const void *Decoder)
1070
1.68k
{
1071
1.68k
  return DecodeVecShiftLImm(Inst, Imm, 64);
1072
1.68k
}
1073
1074
static DecodeStatus DecodeVecShiftL32Imm(MCInst *Inst, unsigned Imm,
1075
           uint64_t Addr, const void *Decoder)
1076
2.82k
{
1077
2.82k
  return DecodeVecShiftLImm(Inst, Imm, 32);
1078
2.82k
}
1079
1080
static DecodeStatus DecodeVecShiftL16Imm(MCInst *Inst, unsigned Imm,
1081
           uint64_t Addr, const void *Decoder)
1082
3.64k
{
1083
3.64k
  return DecodeVecShiftLImm(Inst, Imm, 16);
1084
3.64k
}
1085
1086
static DecodeStatus DecodeVecShiftL8Imm(MCInst *Inst, unsigned Imm,
1087
          uint64_t Addr, const void *Decoder)
1088
5.04k
{
1089
5.04k
  return DecodeVecShiftLImm(Inst, Imm, 8);
1090
5.04k
}
1091
1092
static DecodeStatus DecodeThreeAddrSRegInstruction(MCInst *Inst, uint32_t insn,
1093
               uint64_t Addr,
1094
               const void *Decoder)
1095
13.8k
{
1096
13.8k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1097
13.8k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1098
13.8k
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
1099
13.8k
  unsigned shiftHi = fieldFromInstruction_4(insn, 22, 2);
1100
13.8k
  unsigned shiftLo = fieldFromInstruction_4(insn, 10, 6);
1101
13.8k
  unsigned shift = (shiftHi << 6) | shiftLo;
1102
13.8k
  switch (MCInst_getOpcode(Inst)) {
1103
0
  default:
1104
0
    return Fail;
1105
438
  case AArch64_ADDWrs:
1106
797
  case AArch64_ADDSWrs:
1107
1.53k
  case AArch64_SUBWrs:
1108
1.74k
  case AArch64_SUBSWrs:
1109
    // if shift == '11' then ReservedValue()
1110
1.74k
    if (shiftHi == 0x3)
1111
6
      return Fail;
1112
    // fall through
1113
2.53k
  case AArch64_ANDWrs:
1114
2.85k
  case AArch64_ANDSWrs:
1115
3.27k
  case AArch64_BICWrs:
1116
3.86k
  case AArch64_BICSWrs:
1117
4.31k
  case AArch64_ORRWrs:
1118
4.92k
  case AArch64_ORNWrs:
1119
5.78k
  case AArch64_EORWrs:
1120
6.25k
  case AArch64_EONWrs: {
1121
    // if sf == '0' and imm6<5> == '1' then ReservedValue()
1122
6.25k
    if (shiftLo >> 5 == 1)
1123
50
      return Fail;
1124
6.20k
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1125
6.20k
    DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);
1126
6.20k
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1127
6.20k
    break;
1128
6.25k
  }
1129
785
  case AArch64_ADDXrs:
1130
1.18k
  case AArch64_ADDSXrs:
1131
1.41k
  case AArch64_SUBXrs:
1132
1.89k
  case AArch64_SUBSXrs:
1133
    // if shift == '11' then ReservedValue()
1134
1.89k
    if (shiftHi == 0x3)
1135
16
      return Fail;
1136
    // fall through
1137
2.86k
  case AArch64_ANDXrs:
1138
3.84k
  case AArch64_ANDSXrs:
1139
4.34k
  case AArch64_BICXrs:
1140
5.39k
  case AArch64_BICSXrs:
1141
5.80k
  case AArch64_ORRXrs:
1142
6.46k
  case AArch64_ORNXrs:
1143
7.12k
  case AArch64_EORXrs:
1144
7.57k
  case AArch64_EONXrs:
1145
7.57k
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1146
7.57k
    DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);
1147
7.57k
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1148
7.57k
    break;
1149
13.8k
  }
1150
1151
13.7k
  MCOperand_CreateImm0(Inst, (shift));
1152
13.7k
  return Success;
1153
13.8k
}
1154
1155
static DecodeStatus DecodeMoveImmInstruction(MCInst *Inst, uint32_t insn,
1156
               uint64_t Addr, const void *Decoder)
1157
5.61k
{
1158
5.61k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1159
5.61k
  unsigned imm = fieldFromInstruction_4(insn, 5, 16);
1160
5.61k
  unsigned shift = fieldFromInstruction_4(insn, 21, 2);
1161
5.61k
  shift <<= 4;
1162
5.61k
  switch (MCInst_getOpcode(Inst)) {
1163
0
  default:
1164
0
    return Fail;
1165
213
  case AArch64_MOVZWi:
1166
907
  case AArch64_MOVNWi:
1167
1.21k
  case AArch64_MOVKWi:
1168
1.21k
    if (shift & (1U << 5))
1169
11
      return Fail;
1170
1.20k
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1171
1.20k
    break;
1172
846
  case AArch64_MOVZXi:
1173
2.19k
  case AArch64_MOVNXi:
1174
4.40k
  case AArch64_MOVKXi:
1175
4.40k
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1176
4.40k
    break;
1177
5.61k
  }
1178
1179
5.60k
  if (MCInst_getOpcode(Inst) == AArch64_MOVKWi ||
1180
5.60k
      MCInst_getOpcode(Inst) == AArch64_MOVKXi)
1181
2.50k
    MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
1182
1183
5.60k
  MCOperand_CreateImm0(Inst, (imm));
1184
5.60k
  MCOperand_CreateImm0(Inst, (shift));
1185
5.60k
  return Success;
1186
5.61k
}
1187
1188
static DecodeStatus DecodeUnsignedLdStInstruction(MCInst *Inst, uint32_t insn,
1189
              uint64_t Addr,
1190
              const void *Decoder)
1191
15.4k
{
1192
15.4k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1193
15.4k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1194
15.4k
  unsigned offset = fieldFromInstruction_4(insn, 10, 12);
1195
1196
15.4k
  switch (MCInst_getOpcode(Inst)) {
1197
0
  default:
1198
0
    return Fail;
1199
343
  case AArch64_PRFMui:
1200
    // Rt is an immediate in prefetch.
1201
343
    MCOperand_CreateImm0(Inst, (Rt));
1202
343
    break;
1203
1.33k
  case AArch64_STRBBui:
1204
1.93k
  case AArch64_LDRBBui:
1205
2.38k
  case AArch64_LDRSBWui:
1206
3.19k
  case AArch64_STRHHui:
1207
4.31k
  case AArch64_LDRHHui:
1208
4.62k
  case AArch64_LDRSHWui:
1209
4.93k
  case AArch64_STRWui:
1210
5.09k
  case AArch64_LDRWui:
1211
5.09k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1212
5.09k
    break;
1213
161
  case AArch64_LDRSBXui:
1214
471
  case AArch64_LDRSHXui:
1215
933
  case AArch64_LDRSWui:
1216
1.63k
  case AArch64_STRXui:
1217
2.34k
  case AArch64_LDRXui:
1218
2.34k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1219
2.34k
    break;
1220
1.13k
  case AArch64_LDRQui:
1221
1.67k
  case AArch64_STRQui:
1222
1.67k
    DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
1223
1.67k
    break;
1224
337
  case AArch64_LDRDui:
1225
700
  case AArch64_STRDui:
1226
700
    DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
1227
700
    break;
1228
303
  case AArch64_LDRSui:
1229
681
  case AArch64_STRSui:
1230
681
    DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
1231
681
    break;
1232
824
  case AArch64_LDRHui:
1233
1.29k
  case AArch64_STRHui:
1234
1.29k
    DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);
1235
1.29k
    break;
1236
1.15k
  case AArch64_LDRBui:
1237
3.31k
  case AArch64_STRBui:
1238
3.31k
    DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);
1239
3.31k
    break;
1240
15.4k
  }
1241
1242
15.4k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1243
  // No symbols supported in Capstone
1244
  // if (!Decoder->tryAddingSymbolicOperand(Inst, offset, Addr, Fail, 0, 0, 4))
1245
15.4k
  MCOperand_CreateImm0(Inst, (offset));
1246
15.4k
  return Success;
1247
15.4k
}
1248
1249
static DecodeStatus DecodeSignedLdStInstruction(MCInst *Inst, uint32_t insn,
1250
            uint64_t Addr,
1251
            const void *Decoder)
1252
14.2k
{
1253
14.2k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1254
14.2k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1255
14.2k
  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
14.2k
  if (offset & (1 << (9 - 1)))
1260
5.08k
    offset |= ~((1LL << 9) - 1);
1261
1262
  // First operand is always the writeback to the address register, if needed.
1263
14.2k
  switch (MCInst_getOpcode(Inst)) {
1264
7.19k
  default:
1265
7.19k
    break;
1266
7.19k
  case AArch64_LDRSBWpre:
1267
172
  case AArch64_LDRSHWpre:
1268
187
  case AArch64_STRBBpre:
1269
225
  case AArch64_LDRBBpre:
1270
317
  case AArch64_STRHHpre:
1271
850
  case AArch64_LDRHHpre:
1272
918
  case AArch64_STRWpre:
1273
940
  case AArch64_LDRWpre:
1274
1.00k
  case AArch64_LDRSBWpost:
1275
1.39k
  case AArch64_LDRSHWpost:
1276
1.45k
  case AArch64_STRBBpost:
1277
1.55k
  case AArch64_LDRBBpost:
1278
1.63k
  case AArch64_STRHHpost:
1279
1.72k
  case AArch64_LDRHHpost:
1280
1.78k
  case AArch64_STRWpost:
1281
2.50k
  case AArch64_LDRWpost:
1282
2.51k
  case AArch64_LDRSBXpre:
1283
2.53k
  case AArch64_LDRSHXpre:
1284
2.63k
  case AArch64_STRXpre:
1285
2.71k
  case AArch64_LDRSWpre:
1286
2.89k
  case AArch64_LDRXpre:
1287
2.94k
  case AArch64_LDRSBXpost:
1288
3.02k
  case AArch64_LDRSHXpost:
1289
3.16k
  case AArch64_STRXpost:
1290
3.19k
  case AArch64_LDRSWpost:
1291
3.92k
  case AArch64_LDRXpost:
1292
4.38k
  case AArch64_LDRQpre:
1293
4.70k
  case AArch64_STRQpre:
1294
4.74k
  case AArch64_LDRQpost:
1295
5.18k
  case AArch64_STRQpost:
1296
5.26k
  case AArch64_LDRDpre:
1297
5.34k
  case AArch64_STRDpre:
1298
5.37k
  case AArch64_LDRDpost:
1299
5.44k
  case AArch64_STRDpost:
1300
5.53k
  case AArch64_LDRSpre:
1301
5.57k
  case AArch64_STRSpre:
1302
5.82k
  case AArch64_LDRSpost:
1303
5.89k
  case AArch64_STRSpost:
1304
5.96k
  case AArch64_LDRHpre:
1305
6.06k
  case AArch64_STRHpre:
1306
6.39k
  case AArch64_LDRHpost:
1307
6.51k
  case AArch64_STRHpost:
1308
6.65k
  case AArch64_LDRBpre:
1309
6.97k
  case AArch64_STRBpre:
1310
6.99k
  case AArch64_LDRBpost:
1311
7.09k
  case AArch64_STRBpost:
1312
7.09k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1313
7.09k
    break;
1314
14.2k
  }
1315
1316
14.2k
  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
391
  case AArch64_STURBBi:
1324
465
  case AArch64_LDURBBi:
1325
502
  case AArch64_LDURSBWi:
1326
997
  case AArch64_STURHHi:
1327
1.07k
  case AArch64_LDURHHi:
1328
1.29k
  case AArch64_LDURSHWi:
1329
1.50k
  case AArch64_STURWi:
1330
1.54k
  case AArch64_LDURWi:
1331
1.57k
  case AArch64_LDTRSBWi:
1332
1.67k
  case AArch64_LDTRSHWi:
1333
1.93k
  case AArch64_STTRWi:
1334
2.01k
  case AArch64_LDTRWi:
1335
2.21k
  case AArch64_STTRHi:
1336
2.58k
  case AArch64_LDTRHi:
1337
2.64k
  case AArch64_LDTRBi:
1338
2.68k
  case AArch64_STTRBi:
1339
2.74k
  case AArch64_LDRSBWpre:
1340
2.85k
  case AArch64_LDRSHWpre:
1341
2.86k
  case AArch64_STRBBpre:
1342
2.90k
  case AArch64_LDRBBpre:
1343
2.99k
  case AArch64_STRHHpre:
1344
3.53k
  case AArch64_LDRHHpre:
1345
3.59k
  case AArch64_STRWpre:
1346
3.62k
  case AArch64_LDRWpre:
1347
3.68k
  case AArch64_LDRSBWpost:
1348
4.07k
  case AArch64_LDRSHWpost:
1349
4.13k
  case AArch64_STRBBpost:
1350
4.23k
  case AArch64_LDRBBpost:
1351
4.31k
  case AArch64_STRHHpost:
1352
4.40k
  case AArch64_LDRHHpost:
1353
4.46k
  case AArch64_STRWpost:
1354
5.18k
  case AArch64_LDRWpost:
1355
5.40k
  case AArch64_STLURBi:
1356
5.44k
  case AArch64_STLURHi:
1357
5.57k
  case AArch64_STLURWi:
1358
5.65k
  case AArch64_LDAPURBi:
1359
5.66k
  case AArch64_LDAPURSBWi:
1360
5.72k
  case AArch64_LDAPURHi:
1361
5.85k
  case AArch64_LDAPURSHWi:
1362
6.04k
  case AArch64_LDAPURi:
1363
6.04k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1364
6.04k
    break;
1365
30
  case AArch64_LDURSBXi:
1366
112
  case AArch64_LDURSHXi:
1367
155
  case AArch64_LDURSWi:
1368
185
  case AArch64_STURXi:
1369
308
  case AArch64_LDURXi:
1370
361
  case AArch64_LDTRSBXi:
1371
433
  case AArch64_LDTRSHXi:
1372
519
  case AArch64_LDTRSWi:
1373
845
  case AArch64_STTRXi:
1374
928
  case AArch64_LDTRXi:
1375
947
  case AArch64_LDRSBXpre:
1376
965
  case AArch64_LDRSHXpre:
1377
1.06k
  case AArch64_STRXpre:
1378
1.14k
  case AArch64_LDRSWpre:
1379
1.32k
  case AArch64_LDRXpre:
1380
1.37k
  case AArch64_LDRSBXpost:
1381
1.44k
  case AArch64_LDRSHXpost:
1382
1.59k
  case AArch64_STRXpost:
1383
1.62k
  case AArch64_LDRSWpost:
1384
2.35k
  case AArch64_LDRXpost:
1385
2.40k
  case AArch64_LDAPURSWi:
1386
2.49k
  case AArch64_LDAPURSHXi:
1387
3.42k
  case AArch64_LDAPURSBXi:
1388
3.70k
  case AArch64_STLURXi:
1389
3.72k
  case AArch64_LDAPURXi:
1390
3.72k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1391
3.72k
    break;
1392
227
  case AArch64_LDURQi:
1393
589
  case AArch64_STURQi:
1394
1.05k
  case AArch64_LDRQpre:
1395
1.37k
  case AArch64_STRQpre:
1396
1.41k
  case AArch64_LDRQpost:
1397
1.84k
  case AArch64_STRQpost:
1398
1.84k
    DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
1399
1.84k
    break;
1400
96
  case AArch64_LDURDi:
1401
207
  case AArch64_STURDi:
1402
289
  case AArch64_LDRDpre:
1403
367
  case AArch64_STRDpre:
1404
405
  case AArch64_LDRDpost:
1405
474
  case AArch64_STRDpost:
1406
474
    DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
1407
474
    break;
1408
51
  case AArch64_LDURSi:
1409
135
  case AArch64_STURSi:
1410
222
  case AArch64_LDRSpre:
1411
261
  case AArch64_STRSpre:
1412
510
  case AArch64_LDRSpost:
1413
579
  case AArch64_STRSpost:
1414
579
    DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
1415
579
    break;
1416
73
  case AArch64_LDURHi:
1417
130
  case AArch64_STURHi:
1418
204
  case AArch64_LDRHpre:
1419
304
  case AArch64_STRHpre:
1420
636
  case AArch64_LDRHpost:
1421
755
  case AArch64_STRHpost:
1422
755
    DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);
1423
755
    break;
1424
83
  case AArch64_LDURBi:
1425
218
  case AArch64_STURBi:
1426
353
  case AArch64_LDRBpre:
1427
680
  case AArch64_STRBpre:
1428
700
  case AArch64_LDRBpost:
1429
798
  case AArch64_STRBpost:
1430
798
    DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);
1431
798
    break;
1432
14.2k
  }
1433
1434
14.2k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1435
14.2k
  MCOperand_CreateImm0(Inst, (offset));
1436
1437
14.2k
  bool IsLoad = fieldFromInstruction_4(insn, 22, 1);
1438
14.2k
  bool IsIndexed = fieldFromInstruction_4(insn, 10, 2) != 0;
1439
14.2k
  bool IsFP = fieldFromInstruction_4(insn, 26, 1);
1440
1441
  // Cannot write back to a transfer register (but xzr != sp).
1442
14.2k
  if (IsLoad && IsIndexed && !IsFP && Rn != 31 && Rt == Rn)
1443
145
    return SoftFail;
1444
1445
14.1k
  return Success;
1446
14.2k
}
1447
1448
static DecodeStatus DecodeExclusiveLdStInstruction(MCInst *Inst, uint32_t insn,
1449
               uint64_t Addr,
1450
               const void *Decoder)
1451
16.9k
{
1452
16.9k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1453
16.9k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1454
16.9k
  unsigned Rt2 = fieldFromInstruction_4(insn, 10, 5);
1455
16.9k
  unsigned Rs = fieldFromInstruction_4(insn, 16, 5);
1456
1457
16.9k
  unsigned Opcode = MCInst_getOpcode(Inst);
1458
16.9k
  switch (Opcode) {
1459
0
  default:
1460
0
    return Fail;
1461
234
  case AArch64_STLXRW:
1462
1.26k
  case AArch64_STLXRB:
1463
1.68k
  case AArch64_STLXRH:
1464
1.89k
  case AArch64_STXRW:
1465
3.12k
  case AArch64_STXRB:
1466
3.30k
  case AArch64_STXRH:
1467
3.30k
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1468
    // fall through
1469
3.69k
  case AArch64_LDARW:
1470
3.86k
  case AArch64_LDARB:
1471
3.96k
  case AArch64_LDARH:
1472
4.56k
  case AArch64_LDAXRW:
1473
4.64k
  case AArch64_LDAXRB:
1474
4.91k
  case AArch64_LDAXRH:
1475
5.72k
  case AArch64_LDXRW:
1476
5.83k
  case AArch64_LDXRB:
1477
6.09k
  case AArch64_LDXRH:
1478
6.37k
  case AArch64_STLRW:
1479
6.60k
  case AArch64_STLRB:
1480
6.70k
  case AArch64_STLRH:
1481
6.96k
  case AArch64_STLLRW:
1482
7.13k
  case AArch64_STLLRB:
1483
7.24k
  case AArch64_STLLRH:
1484
7.43k
  case AArch64_LDLARW:
1485
7.57k
  case AArch64_LDLARB:
1486
7.71k
  case AArch64_LDLARH:
1487
7.71k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1488
7.71k
    break;
1489
626
  case AArch64_STLXRX:
1490
844
  case AArch64_STXRX:
1491
844
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1492
    // fall through
1493
1.55k
  case AArch64_LDARX:
1494
2.32k
  case AArch64_LDAXRX:
1495
3.02k
  case AArch64_LDXRX:
1496
4.19k
  case AArch64_STLRX:
1497
4.35k
  case AArch64_LDLARX:
1498
4.44k
  case AArch64_STLLRX:
1499
4.44k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1500
4.44k
    break;
1501
403
  case AArch64_STLXPW:
1502
1.09k
  case AArch64_STXPW:
1503
1.09k
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1504
    // fall through
1505
1.65k
  case AArch64_LDAXPW:
1506
2.08k
  case AArch64_LDXPW:
1507
2.08k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1508
2.08k
    DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);
1509
2.08k
    break;
1510
1.35k
  case AArch64_STLXPX:
1511
1.78k
  case AArch64_STXPX:
1512
1.78k
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1513
    // fall through
1514
2.13k
  case AArch64_LDAXPX:
1515
2.75k
  case AArch64_LDXPX:
1516
2.75k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1517
2.75k
    DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);
1518
2.75k
    break;
1519
16.9k
  }
1520
1521
16.9k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1522
1523
  // You shouldn't load to the same register twice in an instruction...
1524
16.9k
  if ((Opcode == AArch64_LDAXPW || Opcode == AArch64_LDXPW ||
1525
16.9k
       Opcode == AArch64_LDAXPX || Opcode == AArch64_LDXPX) &&
1526
16.9k
      Rt == Rt2)
1527
41
    return SoftFail;
1528
1529
16.9k
  return Success;
1530
16.9k
}
1531
1532
static DecodeStatus DecodePairLdStInstruction(MCInst *Inst, uint32_t insn,
1533
                uint64_t Addr,
1534
                const void *Decoder)
1535
14.3k
{
1536
14.3k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1537
14.3k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1538
14.3k
  unsigned Rt2 = fieldFromInstruction_4(insn, 10, 5);
1539
14.3k
  int64_t offset = fieldFromInstruction_4(insn, 15, 7);
1540
14.3k
  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
14.3k
  if (offset & (1 << (7 - 1)))
1545
8.99k
    offset |= ~((1LL << 7) - 1);
1546
1547
14.3k
  unsigned Opcode = MCInst_getOpcode(Inst);
1548
14.3k
  bool NeedsDisjointWritebackTransfer = false;
1549
1550
  // First operand is always writeback of base register.
1551
14.3k
  switch (Opcode) {
1552
8.40k
  default:
1553
8.40k
    break;
1554
8.40k
  case AArch64_LDPXpost:
1555
607
  case AArch64_STPXpost:
1556
868
  case AArch64_LDPSWpost:
1557
1.00k
  case AArch64_LDPXpre:
1558
1.37k
  case AArch64_STPXpre:
1559
1.45k
  case AArch64_LDPSWpre:
1560
2.14k
  case AArch64_LDPWpost:
1561
2.62k
  case AArch64_STPWpost:
1562
2.78k
  case AArch64_LDPWpre:
1563
3.02k
  case AArch64_STPWpre:
1564
3.27k
  case AArch64_LDPQpost:
1565
3.55k
  case AArch64_STPQpost:
1566
3.66k
  case AArch64_LDPQpre:
1567
3.90k
  case AArch64_STPQpre:
1568
4.14k
  case AArch64_LDPDpost:
1569
4.29k
  case AArch64_STPDpost:
1570
4.48k
  case AArch64_LDPDpre:
1571
4.56k
  case AArch64_STPDpre:
1572
4.78k
  case AArch64_LDPSpost:
1573
5.15k
  case AArch64_STPSpost:
1574
5.36k
  case AArch64_LDPSpre:
1575
5.60k
  case AArch64_STPSpre:
1576
5.87k
  case AArch64_STGPpre:
1577
5.98k
  case AArch64_STGPpost:
1578
5.98k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1579
5.98k
    break;
1580
14.3k
  }
1581
1582
14.3k
  switch (Opcode) {
1583
0
  default:
1584
0
    return Fail;
1585
225
  case AArch64_LDPXpost:
1586
607
  case AArch64_STPXpost:
1587
868
  case AArch64_LDPSWpost:
1588
1.00k
  case AArch64_LDPXpre:
1589
1.37k
  case AArch64_STPXpre:
1590
1.45k
  case AArch64_LDPSWpre:
1591
1.72k
  case AArch64_STGPpre:
1592
1.82k
  case AArch64_STGPpost:
1593
1.82k
    NeedsDisjointWritebackTransfer = true;
1594
    // fall through
1595
1.88k
  case AArch64_LDNPXi:
1596
2.10k
  case AArch64_STNPXi:
1597
2.31k
  case AArch64_LDPXi:
1598
2.37k
  case AArch64_STPXi:
1599
3.09k
  case AArch64_LDPSWi:
1600
3.37k
  case AArch64_STGPi:
1601
3.37k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1602
3.37k
    DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);
1603
3.37k
    break;
1604
693
  case AArch64_LDPWpost:
1605
1.17k
  case AArch64_STPWpost:
1606
1.32k
  case AArch64_LDPWpre:
1607
1.57k
  case AArch64_STPWpre:
1608
1.57k
    NeedsDisjointWritebackTransfer = true;
1609
    // fall through
1610
1.72k
  case AArch64_LDNPWi:
1611
2.11k
  case AArch64_STNPWi:
1612
2.38k
  case AArch64_LDPWi:
1613
2.74k
  case AArch64_STPWi:
1614
2.74k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1615
2.74k
    DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);
1616
2.74k
    break;
1617
151
  case AArch64_LDNPQi:
1618
804
  case AArch64_STNPQi:
1619
1.04k
  case AArch64_LDPQpost:
1620
1.33k
  case AArch64_STPQpost:
1621
1.43k
  case AArch64_LDPQi:
1622
1.56k
  case AArch64_STPQi:
1623
1.67k
  case AArch64_LDPQpre:
1624
1.91k
  case AArch64_STPQpre:
1625
1.91k
    DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
1626
1.91k
    DecodeFPR128RegisterClass(Inst, Rt2, Addr, Decoder);
1627
1.91k
    break;
1628
810
  case AArch64_LDNPDi:
1629
1.35k
  case AArch64_STNPDi:
1630
1.59k
  case AArch64_LDPDpost:
1631
1.74k
  case AArch64_STPDpost:
1632
2.41k
  case AArch64_LDPDi:
1633
2.62k
  case AArch64_STPDi:
1634
2.81k
  case AArch64_LDPDpre:
1635
2.90k
  case AArch64_STPDpre:
1636
2.90k
    DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
1637
2.90k
    DecodeFPR64RegisterClass(Inst, Rt2, Addr, Decoder);
1638
2.90k
    break;
1639
467
  case AArch64_LDNPSi:
1640
1.44k
  case AArch64_STNPSi:
1641
1.66k
  case AArch64_LDPSpost:
1642
2.03k
  case AArch64_STPSpost:
1643
2.28k
  case AArch64_LDPSi:
1644
2.99k
  case AArch64_STPSi:
1645
3.20k
  case AArch64_LDPSpre:
1646
3.45k
  case AArch64_STPSpre:
1647
3.45k
    DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
1648
3.45k
    DecodeFPR32RegisterClass(Inst, Rt2, Addr, Decoder);
1649
3.45k
    break;
1650
14.3k
  }
1651
1652
14.3k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1653
14.3k
  MCOperand_CreateImm0(Inst, (offset));
1654
1655
  // You shouldn't load to the same register twice in an instruction...
1656
14.3k
  if (IsLoad && Rt == Rt2)
1657
172
    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
14.2k
  if (NeedsDisjointWritebackTransfer && Rn != 31 &&
1662
14.2k
      (Rt == Rn || Rt2 == Rn))
1663
481
    return SoftFail;
1664
1665
13.7k
  return Success;
1666
14.2k
}
1667
1668
static DecodeStatus DecodeAuthLoadInstruction(MCInst *Inst, uint32_t insn,
1669
                uint64_t Addr,
1670
                const void *Decoder)
1671
4.08k
{
1672
4.08k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1673
4.08k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1674
4.08k
  uint64_t offset = fieldFromInstruction_4(insn, 22, 1) << 9 |
1675
4.08k
        fieldFromInstruction_4(insn, 12, 9);
1676
4.08k
  unsigned writeback = fieldFromInstruction_4(insn, 11, 1);
1677
1678
4.08k
  switch (MCInst_getOpcode(Inst)) {
1679
0
  default:
1680
0
    return Fail;
1681
836
  case AArch64_LDRAAwriteback:
1682
2.42k
  case AArch64_LDRABwriteback:
1683
2.42k
    DecodeGPR64spRegisterClass(Inst, Rn /* writeback register */,
1684
2.42k
             Addr, Decoder);
1685
2.42k
    break;
1686
1.02k
  case AArch64_LDRAAindexed:
1687
1.65k
  case AArch64_LDRABindexed:
1688
1.65k
    break;
1689
4.08k
  }
1690
1691
4.08k
  DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1692
4.08k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1693
4.08k
  CONCAT(DecodeSImm, 10)(Inst, offset, Addr, Decoder);
1694
1695
4.08k
  if (writeback && Rt == Rn && Rn != 31) {
1696
349
    return SoftFail;
1697
349
  }
1698
1699
3.73k
  return Success;
1700
4.08k
}
1701
1702
static DecodeStatus DecodeAddSubERegInstruction(MCInst *Inst, uint32_t insn,
1703
            uint64_t Addr,
1704
            const void *Decoder)
1705
7.68k
{
1706
7.68k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1707
7.68k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1708
7.68k
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
1709
7.68k
  unsigned extend = fieldFromInstruction_4(insn, 10, 6);
1710
1711
7.68k
  unsigned shift = extend & 0x7;
1712
7.68k
  if (shift > 4)
1713
9
    return Fail;
1714
1715
7.67k
  switch (MCInst_getOpcode(Inst)) {
1716
0
  default:
1717
0
    return Fail;
1718
1.35k
  case AArch64_ADDWrx:
1719
1.68k
  case AArch64_SUBWrx:
1720
1.68k
    DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1721
1.68k
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1722
1.68k
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1723
1.68k
    break;
1724
873
  case AArch64_ADDSWrx:
1725
1.03k
  case AArch64_SUBSWrx:
1726
1.03k
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1727
1.03k
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1728
1.03k
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1729
1.03k
    break;
1730
999
  case AArch64_ADDXrx:
1731
1.54k
  case AArch64_SUBXrx:
1732
1.54k
    DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1733
1.54k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1734
1.54k
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1735
1.54k
    break;
1736
991
  case AArch64_ADDSXrx:
1737
1.15k
  case AArch64_SUBSXrx:
1738
1.15k
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1739
1.15k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1740
1.15k
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1741
1.15k
    break;
1742
1.33k
  case AArch64_ADDXrx64:
1743
1.49k
  case AArch64_SUBXrx64:
1744
1.49k
    DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1745
1.49k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1746
1.49k
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1747
1.49k
    break;
1748
302
  case AArch64_SUBSXrx64:
1749
759
  case AArch64_ADDSXrx64:
1750
759
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1751
759
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1752
759
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1753
759
    break;
1754
7.67k
  }
1755
1756
7.67k
  MCOperand_CreateImm0(Inst, (extend));
1757
7.67k
  return Success;
1758
7.67k
}
1759
1760
static DecodeStatus DecodeLogicalImmInstruction(MCInst *Inst, uint32_t insn,
1761
            uint64_t Addr,
1762
            const void *Decoder)
1763
9.47k
{
1764
9.47k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1765
9.47k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1766
9.47k
  unsigned Datasize = fieldFromInstruction_4(insn, 31, 1);
1767
9.47k
  unsigned imm;
1768
1769
9.47k
  if (Datasize) {
1770
5.33k
    if (MCInst_getOpcode(Inst) == AArch64_ANDSXri)
1771
1.48k
      DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1772
3.85k
    else
1773
3.85k
      DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1774
5.33k
    DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);
1775
5.33k
    imm = fieldFromInstruction_4(insn, 10, 13);
1776
5.33k
    if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 64))
1777
8
      return Fail;
1778
5.33k
  } else {
1779
4.14k
    if (MCInst_getOpcode(Inst) == AArch64_ANDSWri)
1780
838
      DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1781
3.30k
    else
1782
3.30k
      DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1783
4.14k
    DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);
1784
4.14k
    imm = fieldFromInstruction_4(insn, 10, 12);
1785
4.14k
    if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 32))
1786
5
      return Fail;
1787
4.14k
  }
1788
9.46k
  MCOperand_CreateImm0(Inst, (imm));
1789
9.46k
  return Success;
1790
9.47k
}
1791
1792
static DecodeStatus DecodeModImmInstruction(MCInst *Inst, uint32_t insn,
1793
              uint64_t Addr, const void *Decoder)
1794
6.11k
{
1795
6.11k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1796
6.11k
  unsigned cmode = fieldFromInstruction_4(insn, 12, 4);
1797
6.11k
  unsigned imm = fieldFromInstruction_4(insn, 16, 3) << 5;
1798
6.11k
  imm |= fieldFromInstruction_4(insn, 5, 5);
1799
1800
6.11k
  if (MCInst_getOpcode(Inst) == AArch64_MOVID)
1801
1.18k
    DecodeFPR64RegisterClass(Inst, Rd, Addr, Decoder);
1802
4.93k
  else
1803
4.93k
    DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1804
1805
6.11k
  MCOperand_CreateImm0(Inst, (imm));
1806
1807
6.11k
  switch (MCInst_getOpcode(Inst)) {
1808
2.85k
  default:
1809
2.85k
    break;
1810
2.85k
  case AArch64_MOVIv4i16:
1811
574
  case AArch64_MOVIv8i16:
1812
662
  case AArch64_MVNIv4i16:
1813
780
  case AArch64_MVNIv8i16:
1814
1.03k
  case AArch64_MOVIv2i32:
1815
1.79k
  case AArch64_MOVIv4i32:
1816
1.87k
  case AArch64_MVNIv2i32:
1817
2.24k
  case AArch64_MVNIv4i32:
1818
2.24k
    MCOperand_CreateImm0(Inst, ((cmode & 6) << 2));
1819
2.24k
    break;
1820
230
  case AArch64_MOVIv2s_msl:
1821
465
  case AArch64_MOVIv4s_msl:
1822
577
  case AArch64_MVNIv2s_msl:
1823
1.01k
  case AArch64_MVNIv4s_msl:
1824
1.01k
    MCOperand_CreateImm0(Inst, ((cmode & 1) ? 0x110 : 0x108));
1825
1.01k
    break;
1826
6.11k
  }
1827
1828
6.11k
  return Success;
1829
6.11k
}
1830
1831
static DecodeStatus DecodeModImmTiedInstruction(MCInst *Inst, uint32_t insn,
1832
            uint64_t Addr,
1833
            const void *Decoder)
1834
122
{
1835
122
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1836
122
  unsigned cmode = fieldFromInstruction_4(insn, 12, 4);
1837
122
  unsigned imm = fieldFromInstruction_4(insn, 16, 3) << 5;
1838
122
  imm |= fieldFromInstruction_4(insn, 5, 5);
1839
1840
  // Tied operands added twice.
1841
122
  DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1842
122
  DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1843
1844
122
  MCOperand_CreateImm0(Inst, (imm));
1845
122
  MCOperand_CreateImm0(Inst, ((cmode & 6) << 2));
1846
1847
122
  return Success;
1848
122
}
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
4.22k
    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.84k
{
1872
7.84k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1873
7.84k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1874
7.84k
  unsigned Imm = fieldFromInstruction_4(insn, 10, 14);
1875
7.84k
  unsigned S = fieldFromInstruction_4(insn, 29, 1);
1876
7.84k
  unsigned Datasize = fieldFromInstruction_4(insn, 31, 1);
1877
1878
7.84k
  unsigned ShifterVal = (Imm >> 12) & 3;
1879
7.84k
  unsigned ImmVal = Imm & 0xFFF;
1880
1881
7.84k
  if (ShifterVal != 0 && ShifterVal != 1)
1882
53
    return Fail;
1883
1884
7.79k
  if (Datasize) {
1885
2.74k
    if (Rd == 31 && !S)
1886
491
      DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1887
2.25k
    else
1888
2.25k
      DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1889
2.74k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1890
5.04k
  } else {
1891
5.04k
    if (Rd == 31 && !S)
1892
485
      DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1893
4.56k
    else
1894
4.56k
      DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1895
5.04k
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1896
5.04k
  }
1897
1898
  // No symbols supported in Capstone
1899
  // if (!Decoder->tryAddingSymbolicOperand(Inst, Imm, Addr, Fail, 0, 0, 4))
1900
7.79k
  MCOperand_CreateImm0(Inst, (ImmVal));
1901
7.79k
  MCOperand_CreateImm0(Inst, (12 * ShifterVal));
1902
7.79k
  return Success;
1903
7.84k
}
1904
1905
static DecodeStatus DecodeUnconditionalBranch(MCInst *Inst, uint32_t insn,
1906
                uint64_t Addr,
1907
                const void *Decoder)
1908
6.01k
{
1909
6.01k
  int64_t imm = fieldFromInstruction_4(insn, 0, 26);
1910
1911
  // Sign-extend the 26-bit immediate.
1912
6.01k
  if (imm & (1 << (26 - 1)))
1913
2.72k
    imm |= ~((1LL << 26) - 1);
1914
1915
  // No symbols supported in Capstone
1916
  // if (!Decoder->tryAddingSymbolicOperand(Inst, imm * 4, Addr, true, 0, 0, 4))
1917
6.01k
  MCOperand_CreateImm0(Inst, (imm));
1918
1919
6.01k
  return Success;
1920
6.01k
}
1921
1922
static bool isInvalidPState(uint64_t Op1, uint64_t Op2)
1923
3.92k
{
1924
3.92k
  return Op1 == 0 && (Op2 == 0 || // CFINV
1925
1.72k
              Op2 == 1 || // XAFlag
1926
1.72k
              Op2 == 2);  // AXFlag
1927
3.92k
}
1928
1929
static DecodeStatus DecodeSystemPStateImm0_15Instruction(MCInst *Inst,
1930
               uint32_t insn,
1931
               uint64_t Addr,
1932
               const void *Decoder)
1933
2.07k
{
1934
2.07k
  uint64_t op1 = fieldFromInstruction_4(insn, 16, 3);
1935
2.07k
  uint64_t op2 = fieldFromInstruction_4(insn, 5, 3);
1936
2.07k
  uint64_t imm = fieldFromInstruction_4(insn, 8, 4);
1937
2.07k
  uint64_t pstate_field = (op1 << 3) | op2;
1938
1939
2.07k
  if (isInvalidPState(op1, op2))
1940
71
    return Fail;
1941
1942
2.00k
  MCOperand_CreateImm0(Inst, (pstate_field));
1943
2.00k
  MCOperand_CreateImm0(Inst, (imm));
1944
1945
2.00k
  const AArch64PState_PStateImm0_15 *PState =
1946
2.00k
    AArch64PState_lookupPStateImm0_15ByEncoding(pstate_field);
1947
2.00k
  if (PState &&
1948
2.00k
      AArch64_testFeatureList(Inst->csh->mode, PState->FeaturesRequired))
1949
1.08k
    return Success;
1950
916
  return Fail;
1951
2.00k
}
1952
1953
static DecodeStatus DecodeSystemPStateImm0_1Instruction(MCInst *Inst,
1954
              uint32_t insn,
1955
              uint64_t Addr,
1956
              const void *Decoder)
1957
1.84k
{
1958
1.84k
  uint64_t op1 = fieldFromInstruction_4(insn, 16, 3);
1959
1.84k
  uint64_t op2 = fieldFromInstruction_4(insn, 5, 3);
1960
1.84k
  uint64_t crm_high = fieldFromInstruction_4(insn, 9, 3);
1961
1.84k
  uint64_t imm = fieldFromInstruction_4(insn, 8, 1);
1962
1.84k
  uint64_t pstate_field = (crm_high << 6) | (op1 << 3) | op2;
1963
1964
1.84k
  if (isInvalidPState(op1, op2))
1965
71
    return Fail;
1966
1967
1.77k
  MCOperand_CreateImm0(Inst, (pstate_field));
1968
1.77k
  MCOperand_CreateImm0(Inst, (imm));
1969
1970
1.77k
  const AArch64PState_PStateImm0_1 *PState =
1971
1.77k
    AArch64PState_lookupPStateImm0_1ByEncoding(pstate_field);
1972
1.77k
  if (PState &&
1973
1.77k
      AArch64_testFeatureList(Inst->csh->mode, PState->FeaturesRequired))
1974
80
    return Success;
1975
1.69k
  return Fail;
1976
1.77k
}
1977
1978
static DecodeStatus DecodeTestAndBranch(MCInst *Inst, uint32_t insn,
1979
          uint64_t Addr, const void *Decoder)
1980
5.76k
{
1981
5.76k
  uint64_t Rt = fieldFromInstruction_4(insn, 0, 5);
1982
5.76k
  uint64_t bit = fieldFromInstruction_4(insn, 31, 1) << 5;
1983
5.76k
  bit |= fieldFromInstruction_4(insn, 19, 5);
1984
5.76k
  int64_t dst = fieldFromInstruction_4(insn, 5, 14);
1985
1986
  // Sign-extend 14-bit immediate.
1987
5.76k
  if (dst & (1 << (14 - 1)))
1988
3.51k
    dst |= ~((1LL << 14) - 1);
1989
1990
5.76k
  if (fieldFromInstruction_4(insn, 31, 1) == 0)
1991
3.85k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1992
1.91k
  else
1993
1.91k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1994
5.76k
  MCOperand_CreateImm0(Inst, (bit));
1995
  // No symbols supported in Capstone
1996
  // if (!Decoder->tryAddingSymbolicOperand(Inst, dst * 4, Addr, true, 0, 0, 4))
1997
5.76k
  MCOperand_CreateImm0(Inst, (dst));
1998
1999
5.76k
  return Success;
2000
5.76k
}
2001
2002
static DecodeStatus DecodeGPRSeqPairsClassRegisterClass(MCInst *Inst,
2003
              unsigned RegClassID,
2004
              unsigned RegNo,
2005
              uint64_t Addr,
2006
              const void *Decoder)
2007
6.52k
{
2008
  // Register number must be even (see CASP instruction)
2009
6.52k
  if (RegNo & 0x1)
2010
15
    return Fail;
2011
2012
6.51k
  unsigned Reg =
2013
6.51k
    AArch64MCRegisterClasses[RegClassID].RegsBegin[RegNo / 2];
2014
6.51k
  MCOperand_CreateReg0(Inst, (Reg));
2015
6.51k
  return Success;
2016
6.52k
}
2017
2018
static DecodeStatus DecodeWSeqPairsClassRegisterClass(MCInst *Inst,
2019
                  unsigned RegNo,
2020
                  uint64_t Addr,
2021
                  const void *Decoder)
2022
1.07k
{
2023
1.07k
  return DecodeGPRSeqPairsClassRegisterClass(
2024
1.07k
    Inst, AArch64_WSeqPairsClassRegClassID, RegNo, Addr, Decoder);
2025
1.07k
}
2026
2027
static DecodeStatus DecodeXSeqPairsClassRegisterClass(MCInst *Inst,
2028
                  unsigned RegNo,
2029
                  uint64_t Addr,
2030
                  const void *Decoder)
2031
5.45k
{
2032
5.45k
  return DecodeGPRSeqPairsClassRegisterClass(
2033
5.45k
    Inst, AArch64_XSeqPairsClassRegClassID, RegNo, Addr, Decoder);
2034
5.45k
}
2035
2036
static DecodeStatus DecodeSyspXzrInstruction(MCInst *Inst, uint32_t insn,
2037
               uint64_t Addr, const void *Decoder)
2038
1.42k
{
2039
1.42k
  unsigned op1 = fieldFromInstruction_4(insn, 16, 3);
2040
1.42k
  unsigned CRn = fieldFromInstruction_4(insn, 12, 4);
2041
1.42k
  unsigned CRm = fieldFromInstruction_4(insn, 8, 4);
2042
1.42k
  unsigned op2 = fieldFromInstruction_4(insn, 5, 3);
2043
1.42k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
2044
1.42k
  if (Rt != 0x1f)
2045
0
    return Fail;
2046
2047
1.42k
  MCOperand_CreateImm0(Inst, (op1));
2048
1.42k
  MCOperand_CreateImm0(Inst, (CRn));
2049
1.42k
  MCOperand_CreateImm0(Inst, (CRm));
2050
1.42k
  MCOperand_CreateImm0(Inst, (op2));
2051
1.42k
  DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
2052
2053
1.42k
  return Success;
2054
1.42k
}
2055
2056
static DecodeStatus DecodeSVELogicalImmInstruction(MCInst *Inst, uint32_t insn,
2057
               uint64_t Addr,
2058
               const void *Decoder)
2059
14.4k
{
2060
14.4k
  unsigned Zdn = fieldFromInstruction_4(insn, 0, 5);
2061
14.4k
  unsigned imm = fieldFromInstruction_4(insn, 5, 13);
2062
14.4k
  if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 64))
2063
4
    return Fail;
2064
2065
  // The same (tied) operand is added twice to the instruction.
2066
14.4k
  DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);
2067
14.4k
  if (MCInst_getOpcode(Inst) != AArch64_DUPM_ZI)
2068
1.86k
    DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);
2069
14.4k
  MCOperand_CreateImm0(Inst, (imm));
2070
14.4k
  return Success;
2071
14.4k
}
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
9.96k
  { \
2079
9.96k
    if (Imm & ~((1LL << Bits) - 1)) \
2080
9.96k
      return Fail; \
2081
9.96k
\
2082
9.96k
    if (Imm & (1 << (Bits - 1))) \
2083
9.96k
      Imm |= ~((1LL << Bits) - 1); \
2084
9.96k
\
2085
9.96k
    MCOperand_CreateImm0(Inst, (Imm)); \
2086
9.96k
    return Success; \
2087
9.96k
  }
AArch64Disassembler.c:DecodeSImm_5
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_4
Line
Count
Source
2078
4.91k
  { \
2079
4.91k
    if (Imm & ~((1LL << Bits) - 1)) \
2080
4.91k
      return Fail; \
2081
4.91k
\
2082
4.91k
    if (Imm & (1 << (Bits - 1))) \
2083
4.91k
      Imm |= ~((1LL << Bits) - 1); \
2084
4.91k
\
2085
4.91k
    MCOperand_CreateImm0(Inst, (Imm)); \
2086
4.91k
    return Success; \
2087
4.91k
  }
AArch64Disassembler.c:DecodeSImm_6
Line
Count
Source
2078
302
  { \
2079
302
    if (Imm & ~((1LL << Bits) - 1)) \
2080
302
      return Fail; \
2081
302
\
2082
302
    if (Imm & (1 << (Bits - 1))) \
2083
302
      Imm |= ~((1LL << Bits) - 1); \
2084
302
\
2085
302
    MCOperand_CreateImm0(Inst, (Imm)); \
2086
302
    return Success; \
2087
302
  }
AArch64Disassembler.c:DecodeSImm_8
Line
Count
Source
2078
739
  { \
2079
739
    if (Imm & ~((1LL << Bits) - 1)) \
2080
739
      return Fail; \
2081
739
\
2082
739
    if (Imm & (1 << (Bits - 1))) \
2083
739
      Imm |= ~((1LL << Bits) - 1); \
2084
739
\
2085
739
    MCOperand_CreateImm0(Inst, (Imm)); \
2086
739
    return Success; \
2087
739
  }
AArch64Disassembler.c:DecodeSImm_9
Line
Count
Source
2078
1.22k
  { \
2079
1.22k
    if (Imm & ~((1LL << Bits) - 1)) \
2080
1.22k
      return Fail; \
2081
1.22k
\
2082
1.22k
    if (Imm & (1 << (Bits - 1))) \
2083
1.22k
      Imm |= ~((1LL << Bits) - 1); \
2084
1.22k
\
2085
1.22k
    MCOperand_CreateImm0(Inst, (Imm)); \
2086
1.22k
    return Success; \
2087
1.22k
  }
AArch64Disassembler.c:DecodeSImm_10
Line
Count
Source
2078
1.07k
  { \
2079
1.07k
    if (Imm & ~((1LL << Bits) - 1)) \
2080
1.07k
      return Fail; \
2081
1.07k
\
2082
1.07k
    if (Imm & (1 << (Bits - 1))) \
2083
1.07k
      Imm |= ~((1LL << Bits) - 1); \
2084
1.07k
\
2085
1.07k
    MCOperand_CreateImm0(Inst, (Imm)); \
2086
1.07k
    return Success; \
2087
1.07k
  }
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
4.07k
  { \
2101
4.07k
    unsigned Val = (uint8_t)Imm; \
2102
4.07k
    unsigned Shift = (Imm & 0x100) ? 8 : 0; \
2103
4.07k
    if (ElementWidth == 8 && Shift) \
2104
4.07k
      return Fail; \
2105
4.07k
    MCOperand_CreateImm0(Inst, (Val)); \
2106
4.06k
    MCOperand_CreateImm0(Inst, (Shift)); \
2107
4.06k
    return Success; \
2108
4.07k
  }
AArch64Disassembler.c:DecodeImm8OptLsl_8
Line
Count
Source
2100
238
  { \
2101
238
    unsigned Val = (uint8_t)Imm; \
2102
238
    unsigned Shift = (Imm & 0x100) ? 8 : 0; \
2103
238
    if (ElementWidth == 8 && Shift) \
2104
238
      return Fail; \
2105
238
    MCOperand_CreateImm0(Inst, (Val)); \
2106
232
    MCOperand_CreateImm0(Inst, (Shift)); \
2107
232
    return Success; \
2108
238
  }
AArch64Disassembler.c:DecodeImm8OptLsl_16
Line
Count
Source
2100
1.18k
  { \
2101
1.18k
    unsigned Val = (uint8_t)Imm; \
2102
1.18k
    unsigned Shift = (Imm & 0x100) ? 8 : 0; \
2103
1.18k
    if (ElementWidth == 8 && Shift) \
2104
1.18k
      return Fail; \
2105
1.18k
    MCOperand_CreateImm0(Inst, (Val)); \
2106
1.18k
    MCOperand_CreateImm0(Inst, (Shift)); \
2107
1.18k
    return Success; \
2108
1.18k
  }
AArch64Disassembler.c:DecodeImm8OptLsl_32
Line
Count
Source
2100
1.63k
  { \
2101
1.63k
    unsigned Val = (uint8_t)Imm; \
2102
1.63k
    unsigned Shift = (Imm & 0x100) ? 8 : 0; \
2103
1.63k
    if (ElementWidth == 8 && Shift) \
2104
1.63k
      return Fail; \
2105
1.63k
    MCOperand_CreateImm0(Inst, (Val)); \
2106
1.63k
    MCOperand_CreateImm0(Inst, (Shift)); \
2107
1.63k
    return Success; \
2108
1.63k
  }
AArch64Disassembler.c:DecodeImm8OptLsl_64
Line
Count
Source
2100
1.01k
  { \
2101
1.01k
    unsigned Val = (uint8_t)Imm; \
2102
1.01k
    unsigned Shift = (Imm & 0x100) ? 8 : 0; \
2103
1.01k
    if (ElementWidth == 8 && Shift) \
2104
1.01k
      return Fail; \
2105
1.01k
    MCOperand_CreateImm0(Inst, (Val)); \
2106
1.01k
    MCOperand_CreateImm0(Inst, (Shift)); \
2107
1.01k
    return Success; \
2108
1.01k
  }
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.1k
{
2118
10.1k
  MCOperand_CreateImm0(Inst, (Imm + 1));
2119
10.1k
  return Success;
2120
10.1k
}
2121
2122
static DecodeStatus DecodeSVCROp(MCInst *Inst, unsigned Imm, uint64_t Address,
2123
         const void *Decoder)
2124
1.19k
{
2125
1.19k
  if (AArch64SVCR_lookupSVCRByEncoding(Imm)) {
2126
258
    MCOperand_CreateImm0(Inst, (Imm));
2127
258
    return Success;
2128
258
  }
2129
935
  return Fail;
2130
1.19k
}
2131
2132
static DecodeStatus DecodeCPYMemOpInstruction(MCInst *Inst, uint32_t insn,
2133
                uint64_t Addr,
2134
                const void *Decoder)
2135
1.04k
{
2136
1.04k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
2137
1.04k
  unsigned Rs = fieldFromInstruction_4(insn, 16, 5);
2138
1.04k
  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
1.04k
  if (Rd == Rs || Rs == Rn || Rd == Rn)
2143
8
    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
1.03k
  if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2148
1.03k
      !DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||
2149
1.03k
      !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2150
1.03k
      !DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2151
1.03k
      !DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||
2152
1.03k
      !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder))
2153
6
    return MCDisassembler_Fail;
2154
2155
1.03k
  return MCDisassembler_Success;
2156
1.03k
}
2157
2158
static DecodeStatus DecodeSETMemOpInstruction(MCInst *Inst, uint32_t insn,
2159
                uint64_t Addr,
2160
                const void *Decoder)
2161
907
{
2162
907
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
2163
907
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
2164
907
  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
907
  if (Rd == Rm || Rm == Rn || Rd == Rn)
2169
5
    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
902
  if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2174
902
      !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2175
902
      !DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2176
902
      !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2177
902
      !DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder))
2178
2
    return MCDisassembler_Fail;
2179
2180
900
  return MCDisassembler_Success;
2181
902
}
2182
2183
static DecodeStatus DecodePRFMRegInstruction(MCInst *Inst, uint32_t insn,
2184
               uint64_t Addr, const void *Decoder)
2185
1.35k
{
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.35k
  unsigned Mask = 0x18;
2189
1.35k
  uint64_t Rt = fieldFromInstruction_4(insn, 0, 5);
2190
1.35k
  if ((Rt & Mask) == Mask)
2191
772
    return Fail;
2192
2193
581
  uint64_t Rn = fieldFromInstruction_4(insn, 5, 5);
2194
581
  uint64_t Shift = fieldFromInstruction_4(insn, 12, 1);
2195
581
  uint64_t Extend = fieldFromInstruction_4(insn, 15, 1);
2196
581
  uint64_t Rm = fieldFromInstruction_4(insn, 16, 5);
2197
2198
581
  MCOperand_CreateImm0(Inst, (Rt));
2199
581
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
2200
2201
581
  switch (MCInst_getOpcode(Inst)) {
2202
0
  default:
2203
0
    return Fail;
2204
96
  case AArch64_PRFMroW:
2205
96
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
2206
96
    break;
2207
485
  case AArch64_PRFMroX:
2208
485
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
2209
485
    break;
2210
581
  }
2211
2212
581
  DecodeMemExtend(Inst, (Extend << 1) | Shift, Addr, Decoder);
2213
2214
581
  return Success;
2215
581
}