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
1.07k
#define CONCAT(a, b) CONCAT_(a, b)
49
1.07k
#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
839k
#define Success MCDisassembler_Success
331
8.20k
#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
56.8k
{
433
56.8k
  if (RegNo > 31)
434
0
    return Fail;
435
436
56.8k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR128RegClassID]
437
56.8k
            .RegsBegin[RegNo];
438
56.8k
  MCOperand_CreateReg0(Inst, (Register));
439
56.8k
  return Success;
440
56.8k
}
441
442
static DecodeStatus DecodeFPR128_loRegisterClass(MCInst *Inst, unsigned RegNo,
443
             uint64_t Addr,
444
             const void *Decoder)
445
1.95k
{
446
1.95k
  if (RegNo > 15)
447
0
    return Fail;
448
1.95k
  return DecodeFPR128RegisterClass(Inst, RegNo, Addr, Decoder);
449
1.95k
}
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
33.2k
{
463
33.2k
  if (RegNo > 31)
464
0
    return Fail;
465
466
33.2k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR64RegClassID]
467
33.2k
            .RegsBegin[RegNo];
468
33.2k
  MCOperand_CreateReg0(Inst, (Register));
469
33.2k
  return Success;
470
33.2k
}
471
472
static DecodeStatus DecodeFPR32RegisterClass(MCInst *Inst, unsigned RegNo,
473
               uint64_t Addr, const void *Decoder)
474
17.3k
{
475
17.3k
  if (RegNo > 31)
476
0
    return Fail;
477
478
17.3k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR32RegClassID]
479
17.3k
            .RegsBegin[RegNo];
480
17.3k
  MCOperand_CreateReg0(Inst, (Register));
481
17.3k
  return Success;
482
17.3k
}
483
484
static DecodeStatus DecodeFPR16RegisterClass(MCInst *Inst, unsigned RegNo,
485
               uint64_t Addr, const void *Decoder)
486
14.1k
{
487
14.1k
  if (RegNo > 31)
488
0
    return Fail;
489
490
14.1k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR16RegClassID]
491
14.1k
            .RegsBegin[RegNo];
492
14.1k
  MCOperand_CreateReg0(Inst, (Register));
493
14.1k
  return Success;
494
14.1k
}
495
496
static DecodeStatus DecodeFPR8RegisterClass(MCInst *Inst, unsigned RegNo,
497
              uint64_t Addr, const void *Decoder)
498
5.63k
{
499
5.63k
  if (RegNo > 31)
500
0
    return Fail;
501
502
5.63k
  unsigned Register = AArch64MCRegisterClasses[AArch64_FPR8RegClassID]
503
5.63k
            .RegsBegin[RegNo];
504
5.63k
  MCOperand_CreateReg0(Inst, (Register));
505
5.63k
  return Success;
506
5.63k
}
507
508
static DecodeStatus DecodeGPR64commonRegisterClass(MCInst *Inst, unsigned RegNo,
509
               uint64_t Addr,
510
               const void *Decoder)
511
6.09k
{
512
6.09k
  if (RegNo > 30)
513
11
    return Fail;
514
515
6.08k
  unsigned Register =
516
6.08k
    AArch64MCRegisterClasses[AArch64_GPR64commonRegClassID]
517
6.08k
      .RegsBegin[RegNo];
518
6.08k
  MCOperand_CreateReg0(Inst, (Register));
519
6.08k
  return Success;
520
6.09k
}
521
522
static DecodeStatus DecodeGPR64RegisterClass(MCInst *Inst, unsigned RegNo,
523
               uint64_t Addr, const void *Decoder)
524
110k
{
525
110k
  if (RegNo > 31)
526
0
    return Fail;
527
528
110k
  unsigned Register = AArch64MCRegisterClasses[AArch64_GPR64RegClassID]
529
110k
            .RegsBegin[RegNo];
530
110k
  MCOperand_CreateReg0(Inst, (Register));
531
110k
  return Success;
532
110k
}
533
534
static DecodeStatus DecodeGPR64x8ClassRegisterClass(MCInst *Inst,
535
                unsigned RegNo,
536
                uint64_t Address,
537
                const void *Decoder)
538
64
{
539
64
  if (RegNo > 22)
540
3
    return Fail;
541
61
  if (RegNo & 1)
542
2
    return Fail;
543
544
59
  unsigned Register =
545
59
    AArch64MCRegisterClasses[AArch64_GPR64x8ClassRegClassID]
546
59
      .RegsBegin[RegNo >> 1];
547
59
  MCOperand_CreateReg0(Inst, (Register));
548
59
  return Success;
549
61
}
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
6.13k
{
581
6.13k
  if (RegNo > 3)
582
0
    return Fail;
583
584
6.13k
  unsigned Register =
585
6.13k
    AArch64MCRegisterClasses[AArch64_MatrixIndexGPR32_12_15RegClassID]
586
6.13k
      .RegsBegin[RegNo];
587
6.13k
  MCOperand_CreateReg0(Inst, (Register));
588
6.13k
  return Success;
589
6.13k
}
590
591
static DecodeStatus DecodeGPR32RegisterClass(MCInst *Inst, unsigned RegNo,
592
               uint64_t Addr, const void *Decoder)
593
51.0k
{
594
51.0k
  if (RegNo > 31)
595
0
    return Fail;
596
597
51.0k
  unsigned Register = AArch64MCRegisterClasses[AArch64_GPR32RegClassID]
598
51.0k
            .RegsBegin[RegNo];
599
51.0k
  MCOperand_CreateReg0(Inst, (Register));
600
51.0k
  return Success;
601
51.0k
}
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
136k
{
620
136k
  if (RegNo > 31)
621
0
    return Fail;
622
623
136k
  unsigned Register =
624
136k
    AArch64MCRegisterClasses[AArch64_ZPRRegClassID].RegsBegin[RegNo];
625
136k
  MCOperand_CreateReg0(Inst, (Register));
626
136k
  return Success;
627
136k
}
628
629
static DecodeStatus DecodeZPR_4bRegisterClass(MCInst *Inst, unsigned RegNo,
630
                uint64_t Address,
631
                const void *Decoder)
632
9.81k
{
633
9.81k
  if (RegNo > 15)
634
0
    return Fail;
635
9.81k
  return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);
636
9.81k
}
637
638
static DecodeStatus DecodeZPR_3bRegisterClass(MCInst *Inst, unsigned RegNo,
639
                uint64_t Address,
640
                const void *Decoder)
641
1.58k
{
642
1.58k
  if (RegNo > 7)
643
0
    return Fail;
644
1.58k
  return DecodeZPRRegisterClass(Inst, RegNo, Address, Decoder);
645
1.58k
}
646
647
static DecodeStatus DecodeZPR2RegisterClass(MCInst *Inst, unsigned RegNo,
648
              uint64_t Address,
649
              const void *Decoder)
650
1.63k
{
651
1.63k
  if (RegNo > 31)
652
0
    return Fail;
653
1.63k
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR2RegClassID]
654
1.63k
            .RegsBegin[RegNo];
655
1.63k
  MCOperand_CreateReg0(Inst, (Register));
656
1.63k
  return Success;
657
1.63k
}
658
659
static DecodeStatus DecodeZPR3RegisterClass(MCInst *Inst, unsigned RegNo,
660
              uint64_t Address,
661
              const void *Decoder)
662
231
{
663
231
  if (RegNo > 31)
664
0
    return Fail;
665
231
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR3RegClassID]
666
231
            .RegsBegin[RegNo];
667
231
  MCOperand_CreateReg0(Inst, (Register));
668
231
  return Success;
669
231
}
670
671
static DecodeStatus DecodeZPR4RegisterClass(MCInst *Inst, unsigned RegNo,
672
              uint64_t Address,
673
              const void *Decoder)
674
1.83k
{
675
1.83k
  if (RegNo > 31)
676
0
    return Fail;
677
1.83k
  unsigned Register = AArch64MCRegisterClasses[AArch64_ZPR4RegClassID]
678
1.83k
            .RegsBegin[RegNo];
679
1.83k
  MCOperand_CreateReg0(Inst, (Register));
680
1.83k
  return Success;
681
1.83k
}
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.06k
{
738
2.06k
  if (RegMask > 0xFF)
739
0
    return Fail;
740
2.06k
  MCOperand_CreateImm0(Inst, (RegMask));
741
2.06k
  return Success;
742
2.06k
}
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
46.1k
{
777
46.1k
  if (RegNo > 15)
778
0
    return Fail;
779
780
46.1k
  unsigned Register =
781
46.1k
    AArch64MCRegisterClasses[AArch64_PPRRegClassID].RegsBegin[RegNo];
782
46.1k
  MCOperand_CreateReg0(Inst, (Register));
783
46.1k
  return Success;
784
46.1k
}
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
36.1k
{
802
36.1k
  if (RegNo > 7)
803
0
    return Fail;
804
805
  // Just reuse the PPR decode table
806
36.1k
  return DecodePPRRegisterClass(Inst, RegNo, Addr, Decoder);
807
36.1k
}
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
9.05k
{
848
9.05k
  if (RegNo > 31)
849
0
    return Fail;
850
9.05k
  unsigned Register =
851
9.05k
    AArch64MCRegisterClasses[AArch64_QQRegClassID].RegsBegin[RegNo];
852
9.05k
  MCOperand_CreateReg0(Inst, (Register));
853
9.05k
  return Success;
854
9.05k
}
855
856
static DecodeStatus DecodeQQQRegisterClass(MCInst *Inst, unsigned RegNo,
857
             uint64_t Addr, const void *Decoder)
858
7.79k
{
859
7.79k
  if (RegNo > 31)
860
0
    return Fail;
861
7.79k
  unsigned Register =
862
7.79k
    AArch64MCRegisterClasses[AArch64_QQQRegClassID].RegsBegin[RegNo];
863
7.79k
  MCOperand_CreateReg0(Inst, (Register));
864
7.79k
  return Success;
865
7.79k
}
866
867
static DecodeStatus DecodeQQQQRegisterClass(MCInst *Inst, unsigned RegNo,
868
              uint64_t Addr, const void *Decoder)
869
8.30k
{
870
8.30k
  if (RegNo > 31)
871
0
    return Fail;
872
8.30k
  unsigned Register = AArch64MCRegisterClasses[AArch64_QQQQRegClassID]
873
8.30k
            .RegsBegin[RegNo];
874
8.30k
  MCOperand_CreateReg0(Inst, (Register));
875
8.30k
  return Success;
876
8.30k
}
877
878
static DecodeStatus DecodeDDRegisterClass(MCInst *Inst, unsigned RegNo,
879
            uint64_t Addr, const void *Decoder)
880
751
{
881
751
  if (RegNo > 31)
882
0
    return Fail;
883
751
  unsigned Register =
884
751
    AArch64MCRegisterClasses[AArch64_DDRegClassID].RegsBegin[RegNo];
885
751
  MCOperand_CreateReg0(Inst, (Register));
886
751
  return Success;
887
751
}
888
889
static DecodeStatus DecodeDDDRegisterClass(MCInst *Inst, unsigned RegNo,
890
             uint64_t Addr, const void *Decoder)
891
2.13k
{
892
2.13k
  if (RegNo > 31)
893
0
    return Fail;
894
2.13k
  unsigned Register =
895
2.13k
    AArch64MCRegisterClasses[AArch64_DDDRegClassID].RegsBegin[RegNo];
896
2.13k
  MCOperand_CreateReg0(Inst, (Register));
897
2.13k
  return Success;
898
2.13k
}
899
900
static DecodeStatus DecodeDDDDRegisterClass(MCInst *Inst, unsigned RegNo,
901
              uint64_t Addr, const void *Decoder)
902
760
{
903
760
  if (RegNo > 31)
904
0
    return Fail;
905
760
  unsigned Register = AArch64MCRegisterClasses[AArch64_DDDDRegClassID]
906
760
            .RegsBegin[RegNo];
907
760
  MCOperand_CreateReg0(Inst, (Register));
908
760
  return Success;
909
760
}
910
911
static DecodeStatus DecodeFixedPointScaleImm32(MCInst *Inst, unsigned Imm,
912
                 uint64_t Addr,
913
                 const void *Decoder)
914
166
{
915
  // scale{5} is asserted as 1 in tblgen.
916
166
  Imm |= 0x20;
917
166
  MCOperand_CreateImm0(Inst, (64 - Imm));
918
166
  return Success;
919
166
}
920
921
static DecodeStatus DecodeFixedPointScaleImm64(MCInst *Inst, unsigned Imm,
922
                 uint64_t Addr,
923
                 const void *Decoder)
924
331
{
925
331
  MCOperand_CreateImm0(Inst, (64 - Imm));
926
331
  return Success;
927
331
}
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
7.75k
{
944
7.75k
  int64_t ImmVal = Imm;
945
946
  // Sign-extend 19-bit immediate.
947
7.75k
  if (ImmVal & (1 << (19 - 1)))
948
2.89k
    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
7.75k
  MCOperand_CreateImm0(Inst, (ImmVal));
955
7.75k
  return Success;
956
7.75k
}
957
958
static DecodeStatus DecodeMemExtend(MCInst *Inst, unsigned Imm,
959
            uint64_t Address, const void *Decoder)
960
4.37k
{
961
4.37k
  MCOperand_CreateImm0(Inst, ((Imm >> 1) & 1));
962
4.37k
  MCOperand_CreateImm0(Inst, (Imm & 1));
963
4.37k
  return Success;
964
4.37k
}
965
966
static DecodeStatus DecodeMRSSystemRegister(MCInst *Inst, unsigned Imm,
967
              uint64_t Address,
968
              const void *Decoder)
969
1.45k
{
970
1.45k
  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
1.45k
  return Success;
976
1.45k
}
977
978
static DecodeStatus DecodeMSRSystemRegister(MCInst *Inst, unsigned Imm,
979
              uint64_t Address,
980
              const void *Decoder)
981
3.98k
{
982
3.98k
  MCOperand_CreateImm0(Inst, (Imm));
983
984
3.98k
  return Success;
985
3.98k
}
986
987
static DecodeStatus DecodeFMOVLaneInstruction(MCInst *Inst, unsigned Insn,
988
                uint64_t Address,
989
                const void *Decoder)
990
53
{
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
53
  unsigned Rd = fieldFromInstruction_4(Insn, 0, 5);
994
53
  unsigned Rn = fieldFromInstruction_4(Insn, 5, 5);
995
53
  unsigned IsToVec = fieldFromInstruction_4(Insn, 16, 1);
996
997
53
  if (IsToVec) {
998
19
    DecodeFPR128RegisterClass(Inst, Rd, Address, Decoder);
999
19
    DecodeGPR64RegisterClass(Inst, Rn, Address, Decoder);
1000
34
  } else {
1001
34
    DecodeGPR64RegisterClass(Inst, Rd, Address, Decoder);
1002
34
    DecodeFPR128RegisterClass(Inst, Rn, Address, Decoder);
1003
34
  }
1004
1005
  // Add the lane
1006
53
  MCOperand_CreateImm0(Inst, (1));
1007
1008
53
  return Success;
1009
53
}
1010
1011
static DecodeStatus DecodeVecShiftRImm(MCInst *Inst, unsigned Imm, unsigned Add)
1012
4.27k
{
1013
4.27k
  MCOperand_CreateImm0(Inst, (Add - Imm));
1014
4.27k
  return Success;
1015
4.27k
}
1016
1017
static DecodeStatus DecodeVecShiftLImm(MCInst *Inst, unsigned Imm, unsigned Add)
1018
4.09k
{
1019
4.09k
  MCOperand_CreateImm0(Inst, ((Imm + Add) & (Add - 1)));
1020
4.09k
  return Success;
1021
4.09k
}
1022
1023
static DecodeStatus DecodeVecShiftR64Imm(MCInst *Inst, unsigned Imm,
1024
           uint64_t Addr, const void *Decoder)
1025
645
{
1026
645
  return DecodeVecShiftRImm(Inst, Imm, 64);
1027
645
}
1028
1029
static DecodeStatus DecodeVecShiftR64ImmNarrow(MCInst *Inst, unsigned Imm,
1030
                 uint64_t Addr,
1031
                 const void *Decoder)
1032
623
{
1033
623
  return DecodeVecShiftRImm(Inst, Imm | 0x20, 64);
1034
623
}
1035
1036
static DecodeStatus DecodeVecShiftR32Imm(MCInst *Inst, unsigned Imm,
1037
           uint64_t Addr, const void *Decoder)
1038
565
{
1039
565
  return DecodeVecShiftRImm(Inst, Imm, 32);
1040
565
}
1041
1042
static DecodeStatus DecodeVecShiftR32ImmNarrow(MCInst *Inst, unsigned Imm,
1043
                 uint64_t Addr,
1044
                 const void *Decoder)
1045
36
{
1046
36
  return DecodeVecShiftRImm(Inst, Imm | 0x10, 32);
1047
36
}
1048
1049
static DecodeStatus DecodeVecShiftR16Imm(MCInst *Inst, unsigned Imm,
1050
           uint64_t Addr, const void *Decoder)
1051
933
{
1052
933
  return DecodeVecShiftRImm(Inst, Imm, 16);
1053
933
}
1054
1055
static DecodeStatus DecodeVecShiftR16ImmNarrow(MCInst *Inst, unsigned Imm,
1056
                 uint64_t Addr,
1057
                 const void *Decoder)
1058
111
{
1059
111
  return DecodeVecShiftRImm(Inst, Imm | 0x8, 16);
1060
111
}
1061
1062
static DecodeStatus DecodeVecShiftR8Imm(MCInst *Inst, unsigned Imm,
1063
          uint64_t Addr, const void *Decoder)
1064
1.36k
{
1065
1.36k
  return DecodeVecShiftRImm(Inst, Imm, 8);
1066
1.36k
}
1067
1068
static DecodeStatus DecodeVecShiftL64Imm(MCInst *Inst, unsigned Imm,
1069
           uint64_t Addr, const void *Decoder)
1070
390
{
1071
390
  return DecodeVecShiftLImm(Inst, Imm, 64);
1072
390
}
1073
1074
static DecodeStatus DecodeVecShiftL32Imm(MCInst *Inst, unsigned Imm,
1075
           uint64_t Addr, const void *Decoder)
1076
516
{
1077
516
  return DecodeVecShiftLImm(Inst, Imm, 32);
1078
516
}
1079
1080
static DecodeStatus DecodeVecShiftL16Imm(MCInst *Inst, unsigned Imm,
1081
           uint64_t Addr, const void *Decoder)
1082
1.64k
{
1083
1.64k
  return DecodeVecShiftLImm(Inst, Imm, 16);
1084
1.64k
}
1085
1086
static DecodeStatus DecodeVecShiftL8Imm(MCInst *Inst, unsigned Imm,
1087
          uint64_t Addr, const void *Decoder)
1088
1.54k
{
1089
1.54k
  return DecodeVecShiftLImm(Inst, Imm, 8);
1090
1.54k
}
1091
1092
static DecodeStatus DecodeThreeAddrSRegInstruction(MCInst *Inst, uint32_t insn,
1093
               uint64_t Addr,
1094
               const void *Decoder)
1095
6.05k
{
1096
6.05k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1097
6.05k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1098
6.05k
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
1099
6.05k
  unsigned shiftHi = fieldFromInstruction_4(insn, 22, 2);
1100
6.05k
  unsigned shiftLo = fieldFromInstruction_4(insn, 10, 6);
1101
6.05k
  unsigned shift = (shiftHi << 6) | shiftLo;
1102
6.05k
  switch (MCInst_getOpcode(Inst)) {
1103
0
  default:
1104
0
    return Fail;
1105
214
  case AArch64_ADDWrs:
1106
339
  case AArch64_ADDSWrs:
1107
604
  case AArch64_SUBWrs:
1108
696
  case AArch64_SUBSWrs:
1109
    // if shift == '11' then ReservedValue()
1110
696
    if (shiftHi == 0x3)
1111
4
      return Fail;
1112
    // fall through
1113
1.10k
  case AArch64_ANDWrs:
1114
1.18k
  case AArch64_ANDSWrs:
1115
1.32k
  case AArch64_BICWrs:
1116
1.59k
  case AArch64_BICSWrs:
1117
1.91k
  case AArch64_ORRWrs:
1118
2.30k
  case AArch64_ORNWrs:
1119
2.62k
  case AArch64_EORWrs:
1120
2.72k
  case AArch64_EONWrs: {
1121
    // if sf == '0' and imm6<5> == '1' then ReservedValue()
1122
2.72k
    if (shiftLo >> 5 == 1)
1123
27
      return Fail;
1124
2.69k
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1125
2.69k
    DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);
1126
2.69k
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1127
2.69k
    break;
1128
2.72k
  }
1129
286
  case AArch64_ADDXrs:
1130
429
  case AArch64_ADDSXrs:
1131
511
  case AArch64_SUBXrs:
1132
738
  case AArch64_SUBSXrs:
1133
    // if shift == '11' then ReservedValue()
1134
738
    if (shiftHi == 0x3)
1135
7
      return Fail;
1136
    // fall through
1137
1.35k
  case AArch64_ANDXrs:
1138
1.59k
  case AArch64_ANDSXrs:
1139
1.89k
  case AArch64_BICXrs:
1140
2.14k
  case AArch64_BICSXrs:
1141
2.27k
  case AArch64_ORRXrs:
1142
2.53k
  case AArch64_ORNXrs:
1143
2.97k
  case AArch64_EORXrs:
1144
3.31k
  case AArch64_EONXrs:
1145
3.31k
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1146
3.31k
    DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);
1147
3.31k
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1148
3.31k
    break;
1149
6.05k
  }
1150
1151
6.01k
  MCOperand_CreateImm0(Inst, (shift));
1152
6.01k
  return Success;
1153
6.05k
}
1154
1155
static DecodeStatus DecodeMoveImmInstruction(MCInst *Inst, uint32_t insn,
1156
               uint64_t Addr, const void *Decoder)
1157
2.46k
{
1158
2.46k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1159
2.46k
  unsigned imm = fieldFromInstruction_4(insn, 5, 16);
1160
2.46k
  unsigned shift = fieldFromInstruction_4(insn, 21, 2);
1161
2.46k
  shift <<= 4;
1162
2.46k
  switch (MCInst_getOpcode(Inst)) {
1163
0
  default:
1164
0
    return Fail;
1165
83
  case AArch64_MOVZWi:
1166
248
  case AArch64_MOVNWi:
1167
308
  case AArch64_MOVKWi:
1168
308
    if (shift & (1U << 5))
1169
3
      return Fail;
1170
305
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1171
305
    break;
1172
569
  case AArch64_MOVZXi:
1173
1.17k
  case AArch64_MOVNXi:
1174
2.16k
  case AArch64_MOVKXi:
1175
2.16k
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1176
2.16k
    break;
1177
2.46k
  }
1178
1179
2.46k
  if (MCInst_getOpcode(Inst) == AArch64_MOVKWi ||
1180
2.46k
      MCInst_getOpcode(Inst) == AArch64_MOVKXi)
1181
1.04k
    MCInst_addOperand2(Inst, (MCInst_getOperand(Inst, (0))));
1182
1183
2.46k
  MCOperand_CreateImm0(Inst, (imm));
1184
2.46k
  MCOperand_CreateImm0(Inst, (shift));
1185
2.46k
  return Success;
1186
2.46k
}
1187
1188
static DecodeStatus DecodeUnsignedLdStInstruction(MCInst *Inst, uint32_t insn,
1189
              uint64_t Addr,
1190
              const void *Decoder)
1191
5.55k
{
1192
5.55k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1193
5.55k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1194
5.55k
  unsigned offset = fieldFromInstruction_4(insn, 10, 12);
1195
1196
5.55k
  switch (MCInst_getOpcode(Inst)) {
1197
0
  default:
1198
0
    return Fail;
1199
103
  case AArch64_PRFMui:
1200
    // Rt is an immediate in prefetch.
1201
103
    MCOperand_CreateImm0(Inst, (Rt));
1202
103
    break;
1203
822
  case AArch64_STRBBui:
1204
1.03k
  case AArch64_LDRBBui:
1205
1.38k
  case AArch64_LDRSBWui:
1206
1.69k
  case AArch64_STRHHui:
1207
1.96k
  case AArch64_LDRHHui:
1208
2.00k
  case AArch64_LDRSHWui:
1209
2.08k
  case AArch64_STRWui:
1210
2.17k
  case AArch64_LDRWui:
1211
2.17k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1212
2.17k
    break;
1213
43
  case AArch64_LDRSBXui:
1214
125
  case AArch64_LDRSHXui:
1215
343
  case AArch64_LDRSWui:
1216
707
  case AArch64_STRXui:
1217
934
  case AArch64_LDRXui:
1218
934
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1219
934
    break;
1220
233
  case AArch64_LDRQui:
1221
324
  case AArch64_STRQui:
1222
324
    DecodeFPR128RegisterClass(Inst, Rt, Addr, Decoder);
1223
324
    break;
1224
91
  case AArch64_LDRDui:
1225
217
  case AArch64_STRDui:
1226
217
    DecodeFPR64RegisterClass(Inst, Rt, Addr, Decoder);
1227
217
    break;
1228
229
  case AArch64_LDRSui:
1229
326
  case AArch64_STRSui:
1230
326
    DecodeFPR32RegisterClass(Inst, Rt, Addr, Decoder);
1231
326
    break;
1232
598
  case AArch64_LDRHui:
1233
982
  case AArch64_STRHui:
1234
982
    DecodeFPR16RegisterClass(Inst, Rt, Addr, Decoder);
1235
982
    break;
1236
228
  case AArch64_LDRBui:
1237
494
  case AArch64_STRBui:
1238
494
    DecodeFPR8RegisterClass(Inst, Rt, Addr, Decoder);
1239
494
    break;
1240
5.55k
  }
1241
1242
5.55k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1243
  // No symbols supported in Capstone
1244
  // if (!Decoder->tryAddingSymbolicOperand(Inst, offset, Addr, Fail, 0, 0, 4))
1245
5.55k
  MCOperand_CreateImm0(Inst, (offset));
1246
5.55k
  return Success;
1247
5.55k
}
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
7.53k
{
1452
7.53k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1453
7.53k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1454
7.53k
  unsigned Rt2 = fieldFromInstruction_4(insn, 10, 5);
1455
7.53k
  unsigned Rs = fieldFromInstruction_4(insn, 16, 5);
1456
1457
7.53k
  unsigned Opcode = MCInst_getOpcode(Inst);
1458
7.53k
  switch (Opcode) {
1459
0
  default:
1460
0
    return Fail;
1461
80
  case AArch64_STLXRW:
1462
540
  case AArch64_STLXRB:
1463
586
  case AArch64_STLXRH:
1464
678
  case AArch64_STXRW:
1465
1.46k
  case AArch64_STXRB:
1466
1.55k
  case AArch64_STXRH:
1467
1.55k
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1468
    // fall through
1469
1.70k
  case AArch64_LDARW:
1470
1.75k
  case AArch64_LDARB:
1471
1.83k
  case AArch64_LDARH:
1472
1.91k
  case AArch64_LDAXRW:
1473
1.95k
  case AArch64_LDAXRB:
1474
2.09k
  case AArch64_LDAXRH:
1475
2.30k
  case AArch64_LDXRW:
1476
2.38k
  case AArch64_LDXRB:
1477
2.59k
  case AArch64_LDXRH:
1478
2.85k
  case AArch64_STLRW:
1479
3.07k
  case AArch64_STLRB:
1480
3.14k
  case AArch64_STLRH:
1481
3.37k
  case AArch64_STLLRW:
1482
3.47k
  case AArch64_STLLRB:
1483
3.54k
  case AArch64_STLLRH:
1484
3.72k
  case AArch64_LDLARW:
1485
3.80k
  case AArch64_LDLARB:
1486
3.87k
  case AArch64_LDLARH:
1487
3.87k
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1488
3.87k
    break;
1489
76
  case AArch64_STLXRX:
1490
197
  case AArch64_STXRX:
1491
197
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1492
    // fall through
1493
859
  case AArch64_LDARX:
1494
938
  case AArch64_LDAXRX:
1495
1.12k
  case AArch64_LDXRX:
1496
2.21k
  case AArch64_STLRX:
1497
2.33k
  case AArch64_LDLARX:
1498
2.41k
  case AArch64_STLLRX:
1499
2.41k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1500
2.41k
    break;
1501
81
  case AArch64_STLXPW:
1502
429
  case AArch64_STXPW:
1503
429
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1504
    // fall through
1505
723
  case AArch64_LDAXPW:
1506
803
  case AArch64_LDXPW:
1507
803
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1508
803
    DecodeGPR32RegisterClass(Inst, Rt2, Addr, Decoder);
1509
803
    break;
1510
110
  case AArch64_STLXPX:
1511
248
  case AArch64_STXPX:
1512
248
    DecodeGPR32RegisterClass(Inst, Rs, Addr, Decoder);
1513
    // fall through
1514
351
  case AArch64_LDAXPX:
1515
439
  case AArch64_LDXPX:
1516
439
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1517
439
    DecodeGPR64RegisterClass(Inst, Rt2, Addr, Decoder);
1518
439
    break;
1519
7.53k
  }
1520
1521
7.53k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1522
1523
  // You shouldn't load to the same register twice in an instruction...
1524
7.53k
  if ((Opcode == AArch64_LDAXPW || Opcode == AArch64_LDXPW ||
1525
7.53k
       Opcode == AArch64_LDAXPX || Opcode == AArch64_LDXPX) &&
1526
7.53k
      Rt == Rt2)
1527
40
    return SoftFail;
1528
1529
7.49k
  return Success;
1530
7.53k
}
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
1.07k
{
1672
1.07k
  unsigned Rt = fieldFromInstruction_4(insn, 0, 5);
1673
1.07k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1674
1.07k
  uint64_t offset = fieldFromInstruction_4(insn, 22, 1) << 9 |
1675
1.07k
        fieldFromInstruction_4(insn, 12, 9);
1676
1.07k
  unsigned writeback = fieldFromInstruction_4(insn, 11, 1);
1677
1678
1.07k
  switch (MCInst_getOpcode(Inst)) {
1679
0
  default:
1680
0
    return Fail;
1681
491
  case AArch64_LDRAAwriteback:
1682
958
  case AArch64_LDRABwriteback:
1683
958
    DecodeGPR64spRegisterClass(Inst, Rn /* writeback register */,
1684
958
             Addr, Decoder);
1685
958
    break;
1686
32
  case AArch64_LDRAAindexed:
1687
113
  case AArch64_LDRABindexed:
1688
113
    break;
1689
1.07k
  }
1690
1691
1.07k
  DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1692
1.07k
  DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1693
1.07k
  CONCAT(DecodeSImm, 10)(Inst, offset, Addr, Decoder);
1694
1695
1.07k
  if (writeback && Rt == Rn && Rn != 31) {
1696
347
    return SoftFail;
1697
347
  }
1698
1699
724
  return Success;
1700
1.07k
}
1701
1702
static DecodeStatus DecodeAddSubERegInstruction(MCInst *Inst, uint32_t insn,
1703
            uint64_t Addr,
1704
            const void *Decoder)
1705
2.66k
{
1706
2.66k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1707
2.66k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1708
2.66k
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
1709
2.66k
  unsigned extend = fieldFromInstruction_4(insn, 10, 6);
1710
1711
2.66k
  unsigned shift = extend & 0x7;
1712
2.66k
  if (shift > 4)
1713
4
    return Fail;
1714
1715
2.65k
  switch (MCInst_getOpcode(Inst)) {
1716
0
  default:
1717
0
    return Fail;
1718
293
  case AArch64_ADDWrx:
1719
407
  case AArch64_SUBWrx:
1720
407
    DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1721
407
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1722
407
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1723
407
    break;
1724
259
  case AArch64_ADDSWrx:
1725
347
  case AArch64_SUBSWrx:
1726
347
    DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1727
347
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1728
347
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1729
347
    break;
1730
96
  case AArch64_ADDXrx:
1731
235
  case AArch64_SUBXrx:
1732
235
    DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1733
235
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1734
235
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1735
235
    break;
1736
880
  case AArch64_ADDSXrx:
1737
921
  case AArch64_SUBSXrx:
1738
921
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1739
921
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1740
921
    DecodeGPR32RegisterClass(Inst, Rm, Addr, Decoder);
1741
921
    break;
1742
19
  case AArch64_ADDXrx64:
1743
130
  case AArch64_SUBXrx64:
1744
130
    DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1745
130
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1746
130
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1747
130
    break;
1748
218
  case AArch64_SUBSXrx64:
1749
617
  case AArch64_ADDSXrx64:
1750
617
    DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1751
617
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1752
617
    DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder);
1753
617
    break;
1754
2.65k
  }
1755
1756
2.65k
  MCOperand_CreateImm0(Inst, (extend));
1757
2.65k
  return Success;
1758
2.65k
}
1759
1760
static DecodeStatus DecodeLogicalImmInstruction(MCInst *Inst, uint32_t insn,
1761
            uint64_t Addr,
1762
            const void *Decoder)
1763
5.15k
{
1764
5.15k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1765
5.15k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1766
5.15k
  unsigned Datasize = fieldFromInstruction_4(insn, 31, 1);
1767
5.15k
  unsigned imm;
1768
1769
5.15k
  if (Datasize) {
1770
2.70k
    if (MCInst_getOpcode(Inst) == AArch64_ANDSXri)
1771
413
      DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1772
2.29k
    else
1773
2.29k
      DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1774
2.70k
    DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder);
1775
2.70k
    imm = fieldFromInstruction_4(insn, 10, 13);
1776
2.70k
    if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 64))
1777
6
      return Fail;
1778
2.70k
  } else {
1779
2.44k
    if (MCInst_getOpcode(Inst) == AArch64_ANDSWri)
1780
323
      DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1781
2.12k
    else
1782
2.12k
      DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1783
2.44k
    DecodeGPR32RegisterClass(Inst, Rn, Addr, Decoder);
1784
2.44k
    imm = fieldFromInstruction_4(insn, 10, 12);
1785
2.44k
    if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 32))
1786
3
      return Fail;
1787
2.44k
  }
1788
5.14k
  MCOperand_CreateImm0(Inst, (imm));
1789
5.14k
  return Success;
1790
5.15k
}
1791
1792
static DecodeStatus DecodeModImmInstruction(MCInst *Inst, uint32_t insn,
1793
              uint64_t Addr, const void *Decoder)
1794
2.19k
{
1795
2.19k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1796
2.19k
  unsigned cmode = fieldFromInstruction_4(insn, 12, 4);
1797
2.19k
  unsigned imm = fieldFromInstruction_4(insn, 16, 3) << 5;
1798
2.19k
  imm |= fieldFromInstruction_4(insn, 5, 5);
1799
1800
2.19k
  if (MCInst_getOpcode(Inst) == AArch64_MOVID)
1801
439
    DecodeFPR64RegisterClass(Inst, Rd, Addr, Decoder);
1802
1.75k
  else
1803
1.75k
    DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1804
1805
2.19k
  MCOperand_CreateImm0(Inst, (imm));
1806
1807
2.19k
  switch (MCInst_getOpcode(Inst)) {
1808
1.34k
  default:
1809
1.34k
    break;
1810
1.34k
  case AArch64_MOVIv4i16:
1811
75
  case AArch64_MOVIv8i16:
1812
95
  case AArch64_MVNIv4i16:
1813
173
  case AArch64_MVNIv8i16:
1814
231
  case AArch64_MOVIv2i32:
1815
264
  case AArch64_MOVIv4i32:
1816
298
  case AArch64_MVNIv2i32:
1817
323
  case AArch64_MVNIv4i32:
1818
323
    MCOperand_CreateImm0(Inst, ((cmode & 6) << 2));
1819
323
    break;
1820
196
  case AArch64_MOVIv2s_msl:
1821
236
  case AArch64_MOVIv4s_msl:
1822
314
  case AArch64_MVNIv2s_msl:
1823
534
  case AArch64_MVNIv4s_msl:
1824
534
    MCOperand_CreateImm0(Inst, ((cmode & 1) ? 0x110 : 0x108));
1825
534
    break;
1826
2.19k
  }
1827
1828
2.19k
  return Success;
1829
2.19k
}
1830
1831
static DecodeStatus DecodeModImmTiedInstruction(MCInst *Inst, uint32_t insn,
1832
            uint64_t Addr,
1833
            const void *Decoder)
1834
44
{
1835
44
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1836
44
  unsigned cmode = fieldFromInstruction_4(insn, 12, 4);
1837
44
  unsigned imm = fieldFromInstruction_4(insn, 16, 3) << 5;
1838
44
  imm |= fieldFromInstruction_4(insn, 5, 5);
1839
1840
  // Tied operands added twice.
1841
44
  DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1842
44
  DecodeFPR128RegisterClass(Inst, Rd, Addr, Decoder);
1843
1844
44
  MCOperand_CreateImm0(Inst, (imm));
1845
44
  MCOperand_CreateImm0(Inst, ((cmode & 6) << 2));
1846
1847
44
  return Success;
1848
44
}
1849
1850
static DecodeStatus DecodeAdrInstruction(MCInst *Inst, uint32_t insn,
1851
           uint64_t Addr, const void *Decoder)
1852
5.04k
{
1853
5.04k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1854
5.04k
  int64_t imm = fieldFromInstruction_4(insn, 5, 19) << 2;
1855
5.04k
  imm |= fieldFromInstruction_4(insn, 29, 2);
1856
1857
  // Sign-extend the 21-bit immediate.
1858
5.04k
  if (imm & (1 << (21 - 1)))
1859
2.07k
    imm |= ~((1LL << 21) - 1);
1860
1861
5.04k
  DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1862
  // No symbols supported in Capstone
1863
  // if (!Decoder->tryAddingSymbolicOperand(Inst, imm, Addr, Fail, 0, 0, 4))
1864
5.04k
  MCOperand_CreateImm0(Inst, (imm));
1865
1866
5.04k
  return Success;
1867
5.04k
}
1868
1869
static DecodeStatus DecodeAddSubImmShift(MCInst *Inst, uint32_t insn,
1870
           uint64_t Addr, const void *Decoder)
1871
3.64k
{
1872
3.64k
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
1873
3.64k
  unsigned Rn = fieldFromInstruction_4(insn, 5, 5);
1874
3.64k
  unsigned Imm = fieldFromInstruction_4(insn, 10, 14);
1875
3.64k
  unsigned S = fieldFromInstruction_4(insn, 29, 1);
1876
3.64k
  unsigned Datasize = fieldFromInstruction_4(insn, 31, 1);
1877
1878
3.64k
  unsigned ShifterVal = (Imm >> 12) & 3;
1879
3.64k
  unsigned ImmVal = Imm & 0xFFF;
1880
1881
3.64k
  if (ShifterVal != 0 && ShifterVal != 1)
1882
34
    return Fail;
1883
1884
3.60k
  if (Datasize) {
1885
1.28k
    if (Rd == 31 && !S)
1886
45
      DecodeGPR64spRegisterClass(Inst, Rd, Addr, Decoder);
1887
1.24k
    else
1888
1.24k
      DecodeGPR64RegisterClass(Inst, Rd, Addr, Decoder);
1889
1.28k
    DecodeGPR64spRegisterClass(Inst, Rn, Addr, Decoder);
1890
2.32k
  } else {
1891
2.32k
    if (Rd == 31 && !S)
1892
253
      DecodeGPR32spRegisterClass(Inst, Rd, Addr, Decoder);
1893
2.06k
    else
1894
2.06k
      DecodeGPR32RegisterClass(Inst, Rd, Addr, Decoder);
1895
2.32k
    DecodeGPR32spRegisterClass(Inst, Rn, Addr, Decoder);
1896
2.32k
  }
1897
1898
  // No symbols supported in Capstone
1899
  // if (!Decoder->tryAddingSymbolicOperand(Inst, Imm, Addr, Fail, 0, 0, 4))
1900
3.60k
  MCOperand_CreateImm0(Inst, (ImmVal));
1901
3.60k
  MCOperand_CreateImm0(Inst, (12 * ShifterVal));
1902
3.60k
  return Success;
1903
3.64k
}
1904
1905
static DecodeStatus DecodeUnconditionalBranch(MCInst *Inst, uint32_t insn,
1906
                uint64_t Addr,
1907
                const void *Decoder)
1908
2.33k
{
1909
2.33k
  int64_t imm = fieldFromInstruction_4(insn, 0, 26);
1910
1911
  // Sign-extend the 26-bit immediate.
1912
2.33k
  if (imm & (1 << (26 - 1)))
1913
931
    imm |= ~((1LL << 26) - 1);
1914
1915
  // No symbols supported in Capstone
1916
  // if (!Decoder->tryAddingSymbolicOperand(Inst, imm * 4, Addr, true, 0, 0, 4))
1917
2.33k
  MCOperand_CreateImm0(Inst, (imm));
1918
1919
2.33k
  return Success;
1920
2.33k
}
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
1.79k
{
1981
1.79k
  uint64_t Rt = fieldFromInstruction_4(insn, 0, 5);
1982
1.79k
  uint64_t bit = fieldFromInstruction_4(insn, 31, 1) << 5;
1983
1.79k
  bit |= fieldFromInstruction_4(insn, 19, 5);
1984
1.79k
  int64_t dst = fieldFromInstruction_4(insn, 5, 14);
1985
1986
  // Sign-extend 14-bit immediate.
1987
1.79k
  if (dst & (1 << (14 - 1)))
1988
896
    dst |= ~((1LL << 14) - 1);
1989
1990
1.79k
  if (fieldFromInstruction_4(insn, 31, 1) == 0)
1991
577
    DecodeGPR32RegisterClass(Inst, Rt, Addr, Decoder);
1992
1.21k
  else
1993
1.21k
    DecodeGPR64RegisterClass(Inst, Rt, Addr, Decoder);
1994
1.79k
  MCOperand_CreateImm0(Inst, (bit));
1995
  // No symbols supported in Capstone
1996
  // if (!Decoder->tryAddingSymbolicOperand(Inst, dst * 4, Addr, true, 0, 0, 4))
1997
1.79k
  MCOperand_CreateImm0(Inst, (dst));
1998
1999
1.79k
  return Success;
2000
1.79k
}
2001
2002
static DecodeStatus DecodeGPRSeqPairsClassRegisterClass(MCInst *Inst,
2003
              unsigned RegClassID,
2004
              unsigned RegNo,
2005
              uint64_t Addr,
2006
              const void *Decoder)
2007
1.70k
{
2008
  // Register number must be even (see CASP instruction)
2009
1.70k
  if (RegNo & 0x1)
2010
10
    return Fail;
2011
2012
1.69k
  unsigned Reg =
2013
1.69k
    AArch64MCRegisterClasses[RegClassID].RegsBegin[RegNo / 2];
2014
1.69k
  MCOperand_CreateReg0(Inst, (Reg));
2015
1.69k
  return Success;
2016
1.70k
}
2017
2018
static DecodeStatus DecodeWSeqPairsClassRegisterClass(MCInst *Inst,
2019
                  unsigned RegNo,
2020
                  uint64_t Addr,
2021
                  const void *Decoder)
2022
106
{
2023
106
  return DecodeGPRSeqPairsClassRegisterClass(
2024
106
    Inst, AArch64_WSeqPairsClassRegClassID, RegNo, Addr, Decoder);
2025
106
}
2026
2027
static DecodeStatus DecodeXSeqPairsClassRegisterClass(MCInst *Inst,
2028
                  unsigned RegNo,
2029
                  uint64_t Addr,
2030
                  const void *Decoder)
2031
1.59k
{
2032
1.59k
  return DecodeGPRSeqPairsClassRegisterClass(
2033
1.59k
    Inst, AArch64_XSeqPairsClassRegClassID, RegNo, Addr, Decoder);
2034
1.59k
}
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
8.34k
{
2060
8.34k
  unsigned Zdn = fieldFromInstruction_4(insn, 0, 5);
2061
8.34k
  unsigned imm = fieldFromInstruction_4(insn, 5, 13);
2062
8.34k
  if (!AArch64_AM_isValidDecodeLogicalImmediate(imm, 64))
2063
2
    return Fail;
2064
2065
  // The same (tied) operand is added twice to the instruction.
2066
8.34k
  DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);
2067
8.34k
  if (MCInst_getOpcode(Inst) != AArch64_DUPM_ZI)
2068
1.08k
    DecodeZPRRegisterClass(Inst, Zdn, Addr, Decoder);
2069
8.34k
  MCOperand_CreateImm0(Inst, (imm));
2070
8.34k
  return Success;
2071
8.34k
}
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
6.97k
{
2118
6.97k
  MCOperand_CreateImm0(Inst, (Imm + 1));
2119
6.97k
  return Success;
2120
6.97k
}
2121
2122
static DecodeStatus DecodeSVCROp(MCInst *Inst, unsigned Imm, uint64_t Address,
2123
         const void *Decoder)
2124
1.08k
{
2125
1.08k
  if (AArch64SVCR_lookupSVCRByEncoding(Imm)) {
2126
221
    MCOperand_CreateImm0(Inst, (Imm));
2127
221
    return Success;
2128
221
  }
2129
860
  return Fail;
2130
1.08k
}
2131
2132
static DecodeStatus DecodeCPYMemOpInstruction(MCInst *Inst, uint32_t insn,
2133
                uint64_t Addr,
2134
                const void *Decoder)
2135
637
{
2136
637
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
2137
637
  unsigned Rs = fieldFromInstruction_4(insn, 16, 5);
2138
637
  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
637
  if (Rd == Rs || Rs == Rn || Rd == Rn)
2143
5
    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
632
  if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2148
632
      !DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||
2149
632
      !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2150
632
      !DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2151
632
      !DecodeGPR64commonRegisterClass(Inst, Rs, Addr, Decoder) ||
2152
632
      !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder))
2153
2
    return MCDisassembler_Fail;
2154
2155
630
  return MCDisassembler_Success;
2156
632
}
2157
2158
static DecodeStatus DecodeSETMemOpInstruction(MCInst *Inst, uint32_t insn,
2159
                uint64_t Addr,
2160
                const void *Decoder)
2161
582
{
2162
582
  unsigned Rd = fieldFromInstruction_4(insn, 0, 5);
2163
582
  unsigned Rm = fieldFromInstruction_4(insn, 16, 5);
2164
582
  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
582
  if (Rd == Rm || Rm == Rn || Rd == Rn)
2169
2
    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
580
  if (!DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2174
580
      !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2175
580
      !DecodeGPR64commonRegisterClass(Inst, Rd, Addr, Decoder) ||
2176
580
      !DecodeGPR64RegisterClass(Inst, Rn, Addr, Decoder) ||
2177
580
      !DecodeGPR64RegisterClass(Inst, Rm, Addr, Decoder))
2178
1
    return MCDisassembler_Fail;
2179
2180
579
  return MCDisassembler_Success;
2181
580
}
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
}