Coverage Report

Created: 2025-08-29 06:29

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