Coverage Report

Created: 2024-09-08 06:22

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