Coverage Report

Created: 2026-09-03 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/Xtensa/XtensaDisassembler.c
Line
Count
Source
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
//===-- XtensaDisassembler.cpp - Disassembler for Xtensa ------------------===//
16
//
17
//                     The LLVM Compiler Infrastructure
18
//
19
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
20
// See https://llvm.org/LICENSE.txt for license information.
21
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22
//
23
//===----------------------------------------------------------------------===//
24
//
25
// This file implements the XtensaDisassembler class.
26
//
27
//===----------------------------------------------------------------------===//
28
29
#include <stdio.h>
30
#include <string.h>
31
#include <stdlib.h>
32
#include <capstone/platform.h>
33
34
#include "../../MathExtras.h"
35
#include "../../MCDisassembler.h"
36
#include "../../MCFixedLenDisassembler.h"
37
#include "../../SStream.h"
38
#include "../../cs_priv.h"
39
#include "../../utils.h"
40
41
#include "priv.h"
42
43
#define GET_INSTRINFO_MC_DESC
44
#include "XtensaGenInstrInfo.inc"
45
46
#define CONCAT(a, b) CONCAT_(a, b)
47
#define CONCAT_(a, b) a##_##b
48
49
#define DEBUG_TYPE "Xtensa-disassembler"
50
51
static const unsigned ARDecoderTable[] = {
52
  Xtensa_A0,  Xtensa_SP,  Xtensa_A2,  Xtensa_A3, Xtensa_A4,  Xtensa_A5,
53
  Xtensa_A6,  Xtensa_A7,  Xtensa_A8,  Xtensa_A9, Xtensa_A10, Xtensa_A11,
54
  Xtensa_A12, Xtensa_A13, Xtensa_A14, Xtensa_A15
55
};
56
57
static const unsigned AE_DRDecoderTable[] = {
58
  Xtensa_AED0,  Xtensa_AED1,  Xtensa_AED2,  Xtensa_AED3,
59
  Xtensa_AED4,  Xtensa_AED5,  Xtensa_AED6,  Xtensa_AED7,
60
  Xtensa_AED8,  Xtensa_AED9,  Xtensa_AED10, Xtensa_AED11,
61
  Xtensa_AED12, Xtensa_AED13, Xtensa_AED14, Xtensa_AED15
62
};
63
64
static const unsigned AE_VALIGNDecoderTable[] = { Xtensa_U0, Xtensa_U1,
65
              Xtensa_U2, Xtensa_U3 };
66
67
static DecodeStatus DecodeAE_DRRegisterClass(MCInst *Inst, uint64_t RegNo,
68
               uint64_t Address,
69
               const void *Decoder)
70
0
{
71
0
  if (RegNo >= ARR_SIZE(AE_DRDecoderTable))
72
0
    return MCDisassembler_Fail;
73
74
0
  unsigned Reg = AE_DRDecoderTable[RegNo];
75
0
  MCOperand_CreateReg0(Inst, (Reg));
76
0
  return MCDisassembler_Success;
77
0
}
78
79
static DecodeStatus DecodeAE_VALIGNRegisterClass(MCInst *Inst, uint64_t RegNo,
80
             uint64_t Address,
81
             const void *Decoder)
82
0
{
83
0
  if (RegNo >= ARR_SIZE(AE_VALIGNDecoderTable))
84
0
    return MCDisassembler_Fail;
85
86
0
  unsigned Reg = AE_VALIGNDecoderTable[RegNo];
87
0
  MCOperand_CreateReg0(Inst, (Reg));
88
0
  return MCDisassembler_Success;
89
0
}
90
91
static DecodeStatus DecodeARRegisterClass(MCInst *Inst, uint64_t RegNo,
92
            uint64_t Address, const void *Decoder)
93
95.7k
{
94
95.7k
  if (RegNo >= ARR_SIZE(ARDecoderTable))
95
0
    return MCDisassembler_Fail;
96
97
95.7k
  unsigned Reg = ARDecoderTable[RegNo];
98
95.7k
  MCOperand_CreateReg0(Inst, (Reg));
99
95.7k
  return MCDisassembler_Success;
100
95.7k
}
101
102
static const unsigned QRDecoderTable[] = { Xtensa_Q0, Xtensa_Q1, Xtensa_Q2,
103
             Xtensa_Q3, Xtensa_Q4, Xtensa_Q5,
104
             Xtensa_Q6, Xtensa_Q7 };
105
106
static DecodeStatus DecodeQRRegisterClass(MCInst *Inst, uint64_t RegNo,
107
            uint64_t Address, const void *Decoder)
108
0
{
109
0
  if (RegNo >= ARR_SIZE(QRDecoderTable))
110
0
    return MCDisassembler_Fail;
111
112
0
  unsigned Reg = QRDecoderTable[RegNo];
113
0
  MCOperand_CreateReg0(Inst, (Reg));
114
0
  return MCDisassembler_Success;
115
0
}
116
117
static const unsigned FPRDecoderTable[] = {
118
  Xtensa_F0,  Xtensa_F1,  Xtensa_F2,  Xtensa_F3, Xtensa_F4,  Xtensa_F5,
119
  Xtensa_F6,  Xtensa_F7,  Xtensa_F8,  Xtensa_F9, Xtensa_F10, Xtensa_F11,
120
  Xtensa_F12, Xtensa_F13, Xtensa_F14, Xtensa_F15
121
};
122
123
static DecodeStatus DecodeFPRRegisterClass(MCInst *Inst, uint64_t RegNo,
124
             uint64_t Address,
125
             const void *Decoder)
126
7.87k
{
127
7.87k
  if (RegNo >= ARR_SIZE(FPRDecoderTable))
128
0
    return MCDisassembler_Fail;
129
130
7.87k
  unsigned Reg = FPRDecoderTable[RegNo];
131
7.87k
  MCOperand_CreateReg0(Inst, (Reg));
132
7.87k
  return MCDisassembler_Success;
133
7.87k
}
134
135
static const unsigned BRDecoderTable[] = {
136
  Xtensa_B0,  Xtensa_B1,  Xtensa_B2,  Xtensa_B3, Xtensa_B4,  Xtensa_B5,
137
  Xtensa_B6,  Xtensa_B7,  Xtensa_B8,  Xtensa_B9, Xtensa_B10, Xtensa_B11,
138
  Xtensa_B12, Xtensa_B13, Xtensa_B14, Xtensa_B15
139
};
140
141
static const unsigned BR2DecoderTable[] = { Xtensa_B0_B1,   Xtensa_B2_B3,
142
              Xtensa_B4_B5,   Xtensa_B6_B7,
143
              Xtensa_B8_B9,   Xtensa_B10_B11,
144
              Xtensa_B12_B13, Xtensa_B14_B15 };
145
146
static const unsigned BR4DecoderTable[] = { Xtensa_B0_B1_B2_B3,
147
              Xtensa_B4_B5_B6_B7,
148
              Xtensa_B8_B9_B10_B11,
149
              Xtensa_B12_B13_B14_B15 };
150
151
static DecodeStatus DecodeXtensaRegisterClass(MCInst *Inst, uint64_t RegNo,
152
                uint64_t Address,
153
                const void *Decoder,
154
                const unsigned *DecoderTable,
155
                size_t DecoderTableLen)
156
0
{
157
0
  if (RegNo >= DecoderTableLen)
158
0
    return MCDisassembler_Fail;
159
160
0
  unsigned Reg = DecoderTable[RegNo];
161
0
  MCOperand_CreateReg0(Inst, (Reg));
162
0
  return MCDisassembler_Success;
163
0
}
164
165
static DecodeStatus DecodeBR2RegisterClass(MCInst *Inst, uint64_t RegNo,
166
             uint64_t Address,
167
             const void *Decoder)
168
0
{
169
0
  return DecodeXtensaRegisterClass(Inst, RegNo, Address, Decoder,
170
0
           BR2DecoderTable,
171
0
           ARR_SIZE(BR2DecoderTable));
172
0
}
173
174
static DecodeStatus DecodeBR4RegisterClass(MCInst *Inst, uint64_t RegNo,
175
             uint64_t Address,
176
             const void *Decoder)
177
0
{
178
0
  return DecodeXtensaRegisterClass(Inst, RegNo, Address, Decoder,
179
0
           BR4DecoderTable,
180
0
           ARR_SIZE(BR4DecoderTable));
181
0
}
182
183
static DecodeStatus DecodeBRRegisterClass(MCInst *Inst, uint64_t RegNo,
184
            uint64_t Address, const void *Decoder)
185
2.44k
{
186
2.44k
  if (RegNo >= ARR_SIZE(BRDecoderTable))
187
0
    return MCDisassembler_Fail;
188
189
2.44k
  unsigned Reg = BRDecoderTable[RegNo];
190
2.44k
  MCOperand_CreateReg0(Inst, (Reg));
191
2.44k
  return MCDisassembler_Success;
192
2.44k
}
193
194
static const unsigned MRDecoderTable[] = { Xtensa_M0, Xtensa_M1, Xtensa_M2,
195
             Xtensa_M3 };
196
197
static DecodeStatus DecodeMRRegisterClass(MCInst *Inst, uint64_t RegNo,
198
            uint64_t Address, const void *Decoder)
199
309
{
200
309
  if (RegNo >= ARR_SIZE(MRDecoderTable))
201
0
    return MCDisassembler_Fail;
202
203
309
  unsigned Reg = MRDecoderTable[RegNo];
204
309
  MCOperand_CreateReg0(Inst, (Reg));
205
309
  return MCDisassembler_Success;
206
309
}
207
208
static const unsigned MR01DecoderTable[] = { Xtensa_M0, Xtensa_M1 };
209
210
static DecodeStatus DecodeMR01RegisterClass(MCInst *Inst, uint64_t RegNo,
211
              uint64_t Address,
212
              const void *Decoder)
213
454
{
214
454
  if (RegNo >= ARR_SIZE(MR01DecoderTable))
215
0
    return MCDisassembler_Fail;
216
217
454
  unsigned Reg = MR01DecoderTable[RegNo];
218
454
  MCOperand_CreateReg0(Inst, (Reg));
219
454
  return MCDisassembler_Success;
220
454
}
221
222
static const unsigned MR23DecoderTable[] = { Xtensa_M2, Xtensa_M3 };
223
224
static DecodeStatus DecodeMR23RegisterClass(MCInst *Inst, uint64_t RegNo,
225
              uint64_t Address,
226
              const void *Decoder)
227
530
{
228
530
  if (RegNo >= ARR_SIZE(MR23DecoderTable))
229
0
    return MCDisassembler_Fail;
230
231
530
  unsigned Reg = MR23DecoderTable[RegNo];
232
530
  MCOperand_CreateReg0(Inst, (Reg));
233
530
  return MCDisassembler_Success;
234
530
}
235
236
bool Xtensa_getFeatureBits(unsigned int mode, unsigned int feature)
237
105k
{
238
105k
  switch (feature) {
239
387
  case Xtensa_FeatureESP32S3Ops:
240
    // SIMD/AI "ee.*" ops only exist on the ESP32-S3.
241
387
    return (mode & CS_MODE_XTENSA_ESP32S3) != 0;
242
386
  case Xtensa_FeatureHIFI3:
243
    // HiFi3 DSP ops are gated behind the ESP32-S3 in this tree.
244
386
    return (mode & CS_MODE_XTENSA_ESP32S3) != 0;
245
83.8k
  case Xtensa_FeatureDensity:
246
    // Code Density is a base Tensilica default option.
247
83.8k
    return true;
248
20.4k
  default:
249
    // Default case is the "allow all features", which is normal
250
    // Capstone behavior until
251
    // https://github.com/capstone-engine/capstone/issues/1992
252
    // is implemented.
253
20.4k
    return true;
254
105k
  }
255
105k
}
256
257
// Verify SR and UR
258
bool CheckRegister(MCInst *Inst, unsigned RegNo)
259
4.73k
{
260
4.73k
  unsigned NumIntLevels = 0;
261
4.73k
  unsigned NumTimers = 0;
262
4.73k
  unsigned NumMiscSR = 0;
263
4.73k
  bool IsESP32 = false;
264
4.73k
  bool IsESP32S2 = false;
265
4.73k
  bool Res = true;
266
267
  // Assume that CPU is esp32 by default
268
4.73k
  if ((Inst->csh->mode & CS_MODE_XTENSA_ESP32)) {
269
3.03k
    NumIntLevels = 6;
270
3.03k
    NumTimers = 3;
271
3.03k
    NumMiscSR = 4;
272
3.03k
    IsESP32 = true;
273
3.03k
  } else if (Inst->csh->mode & CS_MODE_XTENSA_ESP32S2) {
274
1.18k
    NumIntLevels = 6;
275
1.18k
    NumTimers = 3;
276
1.18k
    NumMiscSR = 4;
277
1.18k
    IsESP32S2 = true;
278
1.18k
  } else if (Inst->csh->mode & CS_MODE_XTENSA_ESP8266) {
279
517
    NumIntLevels = 2;
280
517
    NumTimers = 1;
281
517
  }
282
283
4.73k
  switch (RegNo) {
284
14
  case Xtensa_LBEG:
285
20
  case Xtensa_LEND:
286
24
  case Xtensa_LCOUNT:
287
24
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
288
24
              Xtensa_FeatureLoop);
289
24
    break;
290
0
  case Xtensa_BREG:
291
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
292
0
              Xtensa_FeatureBoolean);
293
0
    break;
294
1
  case Xtensa_LITBASE:
295
1
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
296
1
              Xtensa_FeatureExtendedL32R);
297
1
    break;
298
0
  case Xtensa_SCOMPARE1:
299
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
300
0
              Xtensa_FeatureS32C1I);
301
0
    break;
302
0
  case Xtensa_ACCLO:
303
0
  case Xtensa_ACCHI:
304
2
  case Xtensa_M0:
305
2
  case Xtensa_M1:
306
3
  case Xtensa_M2:
307
5
  case Xtensa_M3:
308
5
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
309
5
              Xtensa_FeatureMAC16);
310
5
    break;
311
0
  case Xtensa_WINDOWBASE:
312
2
  case Xtensa_WINDOWSTART:
313
2
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
314
2
              Xtensa_FeatureWindowed);
315
2
    break;
316
0
  case Xtensa_IBREAKENABLE:
317
0
  case Xtensa_IBREAKA0:
318
0
  case Xtensa_IBREAKA1:
319
0
  case Xtensa_DBREAKA0:
320
0
  case Xtensa_DBREAKA1:
321
2
  case Xtensa_DBREAKC0:
322
2
  case Xtensa_DBREAKC1:
323
2
  case Xtensa_DEBUGCAUSE:
324
2
  case Xtensa_ICOUNT:
325
4
  case Xtensa_ICOUNTLEVEL:
326
4
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
327
4
              Xtensa_FeatureDebug);
328
4
    break;
329
0
  case Xtensa_ATOMCTL:
330
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
331
0
              Xtensa_FeatureATOMCTL);
332
0
    break;
333
0
  case Xtensa_MEMCTL:
334
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
335
0
              Xtensa_FeatureMEMCTL);
336
0
    break;
337
0
  case Xtensa_EPC1:
338
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
339
0
              Xtensa_FeatureException);
340
0
    break;
341
27
  case Xtensa_EPC2:
342
100
  case Xtensa_EPC3:
343
182
  case Xtensa_EPC4:
344
374
  case Xtensa_EPC5:
345
387
  case Xtensa_EPC6:
346
455
  case Xtensa_EPC7:
347
455
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
348
455
              Xtensa_FeatureHighPriInterrupts);
349
455
    Res = Res & (NumIntLevels >= (RegNo - Xtensa_EPC1));
350
455
    break;
351
79
  case Xtensa_EPS2:
352
443
  case Xtensa_EPS3:
353
508
  case Xtensa_EPS4:
354
579
  case Xtensa_EPS5:
355
650
  case Xtensa_EPS6:
356
1.06k
  case Xtensa_EPS7:
357
1.06k
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
358
1.06k
              Xtensa_FeatureHighPriInterrupts);
359
1.06k
    Res = Res & (NumIntLevels > (RegNo - Xtensa_EPS2));
360
1.06k
    break;
361
4
  case Xtensa_EXCSAVE1:
362
4
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
363
4
              Xtensa_FeatureException);
364
4
    break;
365
80
  case Xtensa_EXCSAVE2:
366
197
  case Xtensa_EXCSAVE3:
367
481
  case Xtensa_EXCSAVE4:
368
544
  case Xtensa_EXCSAVE5:
369
844
  case Xtensa_EXCSAVE6:
370
974
  case Xtensa_EXCSAVE7:
371
974
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
372
974
              Xtensa_FeatureHighPriInterrupts);
373
974
    Res = Res & (NumIntLevels >= (RegNo - Xtensa_EXCSAVE1));
374
974
    break;
375
0
  case Xtensa_DEPC:
376
0
  case Xtensa_EXCCAUSE:
377
0
  case Xtensa_EXCVADDR:
378
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
379
0
              Xtensa_FeatureException);
380
0
    break;
381
0
  case Xtensa_CPENABLE:
382
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
383
0
              Xtensa_FeatureCoprocessor);
384
0
    break;
385
0
  case Xtensa_VECBASE:
386
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
387
0
              Xtensa_FeatureRelocatableVector);
388
0
    break;
389
30
  case Xtensa_CCOUNT:
390
30
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
391
30
              Xtensa_FeatureTimerInt);
392
30
    Res &= (NumTimers > 0);
393
30
    break;
394
81
  case Xtensa_CCOMPARE0:
395
596
  case Xtensa_CCOMPARE1:
396
621
  case Xtensa_CCOMPARE2:
397
621
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
398
621
              Xtensa_FeatureTimerInt);
399
621
    Res &= (NumTimers > (RegNo - Xtensa_CCOMPARE0));
400
621
    break;
401
0
  case Xtensa_PRID:
402
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
403
0
              Xtensa_FeaturePRID);
404
0
    break;
405
118
  case Xtensa_INTERRUPT:
406
118
  case Xtensa_INTCLEAR:
407
118
  case Xtensa_INTENABLE:
408
118
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
409
118
              Xtensa_FeatureInterrupt);
410
118
    break;
411
64
  case Xtensa_MISC0:
412
170
  case Xtensa_MISC1:
413
433
  case Xtensa_MISC2:
414
743
  case Xtensa_MISC3:
415
743
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
416
743
              Xtensa_FeatureMiscSR);
417
743
    Res &= (NumMiscSR > (RegNo - Xtensa_MISC0));
418
743
    break;
419
7
  case Xtensa_THREADPTR:
420
7
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
421
7
              Xtensa_FeatureTHREADPTR);
422
7
    break;
423
20
  case Xtensa_GPIO_OUT:
424
20
    Res = IsESP32S2;
425
20
    break;
426
74
  case Xtensa_EXPSTATE:
427
74
    Res = IsESP32;
428
74
    break;
429
85
  case Xtensa_FCR:
430
138
  case Xtensa_FSR:
431
138
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
432
138
              Xtensa_FeatureSingleFloat);
433
138
    break;
434
188
  case Xtensa_F64R_LO:
435
415
  case Xtensa_F64R_HI:
436
437
  case Xtensa_F64S:
437
437
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
438
437
              Xtensa_FeatureDFPAccel);
439
437
    break;
440
4.73k
  }
441
442
4.73k
  return Res;
443
4.73k
}
444
445
static const unsigned SRDecoderTable[] = {
446
  Xtensa_LBEG,      0,   Xtensa_LEND,       1,
447
  Xtensa_LCOUNT,      2,   Xtensa_SAR,        3,
448
  Xtensa_BREG,      4,   Xtensa_LITBASE,      5,
449
  Xtensa_SCOMPARE1,   12,  Xtensa_ACCLO,        16,
450
  Xtensa_ACCHI,     17,  Xtensa_M0,       32,
451
  Xtensa_M1,      33,  Xtensa_M2,       34,
452
  Xtensa_M3,      35,  Xtensa_WINDOWBASE,   72,
453
  Xtensa_WINDOWSTART, 73,  Xtensa_IBREAKENABLE, 96,
454
  Xtensa_MEMCTL,      97,  Xtensa_ATOMCTL,      99,
455
  Xtensa_DDR,     104, Xtensa_IBREAKA0,     128,
456
  Xtensa_IBREAKA1,    129, Xtensa_DBREAKA0,     144,
457
  Xtensa_DBREAKA1,    145, Xtensa_DBREAKC0,     160,
458
  Xtensa_DBREAKC1,    161, Xtensa_CONFIGID0,    176,
459
  Xtensa_EPC1,      177, Xtensa_EPC2,       178,
460
  Xtensa_EPC3,      179, Xtensa_EPC4,       180,
461
  Xtensa_EPC5,      181, Xtensa_EPC6,       182,
462
  Xtensa_EPC7,      183, Xtensa_DEPC,       192,
463
  Xtensa_EPS2,      194, Xtensa_EPS3,       195,
464
  Xtensa_EPS4,      196, Xtensa_EPS5,       197,
465
  Xtensa_EPS6,      198, Xtensa_EPS7,       199,
466
  Xtensa_CONFIGID1,   208, Xtensa_EXCSAVE1,     209,
467
  Xtensa_EXCSAVE2,    210, Xtensa_EXCSAVE3,     211,
468
  Xtensa_EXCSAVE4,    212, Xtensa_EXCSAVE5,     213,
469
  Xtensa_EXCSAVE6,    214, Xtensa_EXCSAVE7,     215,
470
  Xtensa_CPENABLE,    224, Xtensa_INTERRUPT,    226,
471
  Xtensa_INTCLEAR,    227, Xtensa_INTENABLE,    228,
472
  Xtensa_PS,      230, Xtensa_VECBASE,      231,
473
  Xtensa_EXCCAUSE,    232, Xtensa_DEBUGCAUSE,   233,
474
  Xtensa_CCOUNT,      234, Xtensa_PRID,       235,
475
  Xtensa_ICOUNT,      236, Xtensa_ICOUNTLEVEL,  237,
476
  Xtensa_EXCVADDR,    238, Xtensa_CCOMPARE0,    240,
477
  Xtensa_CCOMPARE1,   241, Xtensa_CCOMPARE2,    242,
478
  Xtensa_MISC0,     244, Xtensa_MISC1,        245,
479
  Xtensa_MISC2,     246, Xtensa_MISC3,        247
480
};
481
482
static DecodeStatus DecodeSRRegisterClass(MCInst *Inst, uint64_t RegNo,
483
            uint64_t Address, const void *Decoder)
484
4.06k
{
485
  //  const llvm_MCSubtargetInfo STI =
486
  //    ((const MCDisassembler *)Decoder)->getSubtargetInfo();
487
488
4.06k
  if (RegNo > 255)
489
0
    return MCDisassembler_Fail;
490
491
196k
  for (unsigned i = 0; i < ARR_SIZE(SRDecoderTable); i += 2) {
492
196k
    if (SRDecoderTable[i + 1] == RegNo) {
493
4.06k
      unsigned Reg = SRDecoderTable[i];
494
495
4.06k
      if (!CheckRegister(Inst, Reg))
496
6
        return MCDisassembler_Fail;
497
498
4.05k
      MCOperand_CreateReg0(Inst, (Reg));
499
4.05k
      return MCDisassembler_Success;
500
4.06k
    }
501
196k
  }
502
503
4
  return MCDisassembler_Fail;
504
4.06k
}
505
506
static const unsigned URDecoderTable[] = {
507
  Xtensa_GPIO_OUT, 0,   Xtensa_EXPSTATE, 230, Xtensa_THREADPTR, 231,
508
  Xtensa_FCR,  232, Xtensa_FSR,      233, Xtensa_F64R_LO,   234,
509
  Xtensa_F64R_HI,  235, Xtensa_F64S,     236
510
};
511
512
static DecodeStatus DecodeURRegisterClass(MCInst *Inst, uint64_t RegNo,
513
            uint64_t Address, const void *Decoder)
514
684
{
515
684
  if (RegNo > 255)
516
0
    return MCDisassembler_Fail;
517
518
3.75k
  for (unsigned i = 0; i < ARR_SIZE(URDecoderTable); i += 2) {
519
3.75k
    if (URDecoderTable[i + 1] == RegNo) {
520
676
      unsigned Reg = URDecoderTable[i];
521
522
676
      if (!CheckRegister(Inst, Reg))
523
2
        return MCDisassembler_Fail;
524
525
674
      MCOperand_CreateReg0(Inst, (Reg));
526
674
      return MCDisassembler_Success;
527
676
    }
528
3.75k
  }
529
530
8
  return MCDisassembler_Fail;
531
684
}
532
533
static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
534
             uint64_t Address, uint64_t Offset,
535
             uint64_t InstSize, MCInst *MI,
536
             const void *Decoder)
537
11.7k
{
538
  //  return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
539
  //               Offset, /*OpSize=*/0, InstSize);
540
11.7k
  return false;
541
11.7k
}
542
543
static DecodeStatus decodeCallOperand(MCInst *Inst, uint64_t Imm,
544
              int64_t Address, const void *Decoder)
545
2.55k
{
546
2.55k
  CS_ASSERT_RET_VAL(isUIntN(18, Imm) && "Invalid immediate",
547
2.55k
        MCDisassembler_Fail);
548
2.55k
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 2), 20)));
549
2.55k
  return MCDisassembler_Success;
550
2.55k
}
551
552
static DecodeStatus decodeJumpOperand(MCInst *Inst, uint64_t Imm,
553
              int64_t Address, const void *Decoder)
554
1.21k
{
555
1.21k
  CS_ASSERT_RET_VAL(isUIntN(18, Imm) && "Invalid immediate",
556
1.21k
        MCDisassembler_Fail);
557
1.21k
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 18)));
558
1.21k
  return MCDisassembler_Success;
559
1.21k
}
560
561
static DecodeStatus decodeBranchOperand(MCInst *Inst, uint64_t Imm,
562
          int64_t Address, const void *Decoder)
563
11.4k
{
564
11.4k
  switch (MCInst_getOpcode(Inst)) {
565
541
  case Xtensa_BEQZ:
566
753
  case Xtensa_BGEZ:
567
1.09k
  case Xtensa_BLTZ:
568
1.93k
  case Xtensa_BNEZ:
569
1.93k
    CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
570
1.93k
          MCDisassembler_Fail);
571
1.93k
    if (!tryAddingSymbolicOperand(
572
1.93k
          SignExtend64((Imm), 12) + 4 + Address, true,
573
1.93k
          Address, 0, 3, Inst, Decoder))
574
1.93k
      MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 12)));
575
1.93k
    break;
576
9.55k
  default:
577
9.55k
    CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
578
9.55k
          MCDisassembler_Fail);
579
9.55k
    if (!tryAddingSymbolicOperand(
580
9.55k
          SignExtend64((Imm), 8) + 4 + Address, true, Address,
581
9.55k
          0, 3, Inst, Decoder))
582
9.55k
      MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 8)));
583
11.4k
  }
584
11.4k
  return MCDisassembler_Success;
585
11.4k
}
586
587
static DecodeStatus decodeLoopOperand(MCInst *Inst, uint64_t Imm,
588
              int64_t Address, const void *Decoder)
589
232
{
590
232
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
591
232
        MCDisassembler_Fail);
592
232
  if (!tryAddingSymbolicOperand(Imm + 4 + Address, true, Address, 0, 3,
593
232
              Inst, Decoder))
594
232
    MCOperand_CreateImm0(Inst, (Imm));
595
232
  return MCDisassembler_Success;
596
232
}
597
598
static DecodeStatus decodeL32ROperand(MCInst *Inst, uint64_t Imm,
599
              int64_t Address, const void *Decoder)
600
4.02k
{
601
4.02k
  CS_ASSERT_RET_VAL(isUIntN(16, Imm) && "Invalid immediate",
602
4.02k
        MCDisassembler_Fail);
603
4.02k
  MCOperand_CreateImm0(Inst, OneExtend64(Imm << 2, 18));
604
4.02k
  return MCDisassembler_Success;
605
4.02k
}
606
607
static DecodeStatus decodeImm8Operand(MCInst *Inst, uint64_t Imm,
608
              int64_t Address, const void *Decoder)
609
447
{
610
447
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
611
447
        MCDisassembler_Fail);
612
447
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 8)));
613
447
  return MCDisassembler_Success;
614
447
}
615
616
static DecodeStatus decodeImm8_sh8Operand(MCInst *Inst, uint64_t Imm,
617
            int64_t Address, const void *Decoder)
618
375
{
619
375
  CS_ASSERT_RET_VAL(isUIntN(16, Imm) && ((Imm & 0xff) == 0) &&
620
375
          "Invalid immediate",
621
375
        MCDisassembler_Fail);
622
375
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 16)));
623
375
  return MCDisassembler_Success;
624
375
}
625
626
static DecodeStatus decodeImm12Operand(MCInst *Inst, uint64_t Imm,
627
               int64_t Address, const void *Decoder)
628
381
{
629
381
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
630
381
        MCDisassembler_Fail);
631
381
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 12)));
632
381
  return MCDisassembler_Success;
633
381
}
634
635
static DecodeStatus decodeUimm4Operand(MCInst *Inst, uint64_t Imm,
636
               int64_t Address, const void *Decoder)
637
2.27k
{
638
2.27k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
639
2.27k
        MCDisassembler_Fail);
640
2.27k
  MCOperand_CreateImm0(Inst, (Imm));
641
2.27k
  return MCDisassembler_Success;
642
2.27k
}
643
644
static DecodeStatus decodeUimm5Operand(MCInst *Inst, uint64_t Imm,
645
               int64_t Address, const void *Decoder)
646
2.66k
{
647
2.66k
  CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate",
648
2.66k
        MCDisassembler_Fail);
649
2.66k
  MCOperand_CreateImm0(Inst, (Imm));
650
2.66k
  return MCDisassembler_Success;
651
2.66k
}
652
653
static DecodeStatus decodeImm1_16Operand(MCInst *Inst, uint64_t Imm,
654
           int64_t Address, const void *Decoder)
655
933
{
656
933
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
657
933
        MCDisassembler_Fail);
658
933
  MCOperand_CreateImm0(Inst, (Imm + 1));
659
933
  return MCDisassembler_Success;
660
933
}
661
662
static DecodeStatus decodeImm1n_15Operand(MCInst *Inst, uint64_t Imm,
663
            int64_t Address, const void *Decoder)
664
4.55k
{
665
4.55k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
666
4.55k
        MCDisassembler_Fail);
667
4.55k
  if (!Imm)
668
232
    MCOperand_CreateImm0(Inst, (-1));
669
4.31k
  else
670
4.31k
    MCOperand_CreateImm0(Inst, (Imm));
671
4.55k
  return MCDisassembler_Success;
672
4.55k
}
673
674
static DecodeStatus decodeImm32n_95Operand(MCInst *Inst, uint64_t Imm,
675
             int64_t Address, const void *Decoder)
676
1.56k
{
677
1.56k
  CS_ASSERT_RET_VAL(isUIntN(7, Imm) && "Invalid immediate",
678
1.56k
        MCDisassembler_Fail);
679
1.56k
  if ((Imm & 0x60) == 0x60)
680
639
    MCOperand_CreateImm0(Inst, ((~0x1f) | Imm));
681
925
  else
682
925
    MCOperand_CreateImm0(Inst, (Imm));
683
1.56k
  return MCDisassembler_Success;
684
1.56k
}
685
686
static DecodeStatus decodeImm8n_7Operand(MCInst *Inst, uint64_t Imm,
687
           int64_t Address, const void *Decoder)
688
139
{
689
139
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
690
139
        MCDisassembler_Fail);
691
139
  if (Imm > 7)
692
65
    MCOperand_CreateImm0(Inst, (Imm - 16));
693
74
  else
694
74
    MCOperand_CreateImm0(Inst, (Imm));
695
139
  return MCDisassembler_Success;
696
139
}
697
698
static DecodeStatus decodeImm64n_4nOperand(MCInst *Inst, uint64_t Imm,
699
             int64_t Address, const void *Decoder)
700
298
{
701
298
  CS_ASSERT_RET_VAL(isUIntN(6, Imm) && ((Imm & 0x3) == 0) &&
702
298
          "Invalid immediate",
703
298
        MCDisassembler_Fail);
704
298
  MCOperand_CreateImm0(Inst, ((~0x3f) | (Imm)));
705
298
  return MCDisassembler_Success;
706
298
}
707
708
static DecodeStatus decodeOffset8m32Operand(MCInst *Inst, uint64_t Imm,
709
              int64_t Address,
710
              const void *Decoder)
711
972
{
712
972
  CS_ASSERT_RET_VAL(isUIntN(10, Imm) && ((Imm & 0x3) == 0) &&
713
972
          "Invalid immediate",
714
972
        MCDisassembler_Fail);
715
972
  MCOperand_CreateImm0(Inst, (Imm));
716
972
  return MCDisassembler_Success;
717
972
}
718
719
static DecodeStatus decodeEntry_Imm12OpValue(MCInst *Inst, uint64_t Imm,
720
               int64_t Address,
721
               const void *Decoder)
722
267
{
723
267
  CS_ASSERT_RET_VAL(isUIntN(15, Imm) && ((Imm & 0x7) == 0) &&
724
267
          "Invalid immediate",
725
267
        MCDisassembler_Fail);
726
267
  MCOperand_CreateImm0(Inst, (Imm));
727
267
  return MCDisassembler_Success;
728
267
}
729
730
static DecodeStatus decodeShimm1_31Operand(MCInst *Inst, uint64_t Imm,
731
             int64_t Address, const void *Decoder)
732
458
{
733
458
  CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate",
734
458
        MCDisassembler_Fail);
735
458
  MCOperand_CreateImm0(Inst, (32 - Imm));
736
458
  return MCDisassembler_Success;
737
458
}
738
739
//static DecodeStatus decodeShimm0_31Operand(MCInst *Inst, uint64_t Imm,
740
//             int64_t Address, const void *Decoder)
741
//{
742
//  CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate", MCDisassembler_Fail);
743
//  MCOperand_CreateImm0(Inst, (32 - Imm));
744
//  return MCDisassembler_Success;
745
//}
746
747
static DecodeStatus decodeImm7_22Operand(MCInst *Inst, uint64_t Imm,
748
           int64_t Address, const void *Decoder)
749
93
{
750
93
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
751
93
        MCDisassembler_Fail);
752
93
  MCOperand_CreateImm0(Inst, (Imm + 7));
753
93
  return MCDisassembler_Success;
754
93
}
755
756
static DecodeStatus decodeSelect_2Operand(MCInst *Inst, uint64_t Imm,
757
            int64_t Address, const void *Decoder)
758
0
{
759
0
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
760
0
        MCDisassembler_Fail);
761
0
  MCOperand_CreateImm0(Inst, (Imm));
762
0
  return MCDisassembler_Success;
763
0
}
764
765
static DecodeStatus decodeSelect_4Operand(MCInst *Inst, uint64_t Imm,
766
            int64_t Address, const void *Decoder)
767
0
{
768
0
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
769
0
        MCDisassembler_Fail);
770
0
  MCOperand_CreateImm0(Inst, (Imm));
771
0
  return MCDisassembler_Success;
772
0
}
773
774
static DecodeStatus decodeSelect_8Operand(MCInst *Inst, uint64_t Imm,
775
            int64_t Address, const void *Decoder)
776
0
{
777
0
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
778
0
        MCDisassembler_Fail);
779
0
  MCOperand_CreateImm0(Inst, (Imm));
780
0
  return MCDisassembler_Success;
781
0
}
782
783
static DecodeStatus decodeSelect_16Operand(MCInst *Inst, uint64_t Imm,
784
             int64_t Address, const void *Decoder)
785
0
{
786
0
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
787
0
        MCDisassembler_Fail);
788
0
  MCOperand_CreateImm0(Inst, (Imm));
789
0
  return MCDisassembler_Success;
790
0
}
791
792
static DecodeStatus decodeSelect_256Operand(MCInst *Inst, uint64_t Imm,
793
              int64_t Address,
794
              const void *Decoder)
795
235
{
796
235
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
797
235
        MCDisassembler_Fail);
798
235
  MCOperand_CreateImm0(Inst, (Imm));
799
235
  return MCDisassembler_Success;
800
235
}
801
802
static DecodeStatus decodeOffset_16_16Operand(MCInst *Inst, uint64_t Imm,
803
                int64_t Address,
804
                const void *Decoder)
805
0
{
806
0
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
807
0
        MCDisassembler_Fail);
808
0
  MCOperand_CreateImm0(Inst, SignExtend64(Imm << 4, 8));
809
0
  return MCDisassembler_Success;
810
0
}
811
812
static DecodeStatus decodeOffset_256_8Operand(MCInst *Inst, uint64_t Imm,
813
                int64_t Address,
814
                const void *Decoder)
815
0
{
816
0
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
817
0
        MCDisassembler_Fail);
818
0
  MCOperand_CreateImm0(Inst, SignExtend64(Imm << 3, 11));
819
0
  return MCDisassembler_Success;
820
0
}
821
822
static DecodeStatus decodeOffset_256_16Operand(MCInst *Inst, uint64_t Imm,
823
                 int64_t Address,
824
                 const void *Decoder)
825
0
{
826
0
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
827
0
        MCDisassembler_Fail);
828
0
  MCOperand_CreateImm0(Inst, SignExtend64(Imm << 4, 12));
829
0
  return MCDisassembler_Success;
830
0
}
831
832
static DecodeStatus decodeOffset_256_4Operand(MCInst *Inst, uint64_t Imm,
833
                int64_t Address,
834
                const void *Decoder)
835
0
{
836
0
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
837
0
        MCDisassembler_Fail);
838
0
  MCOperand_CreateImm0(Inst, SignExtend64(Imm << 2, 10));
839
0
  return MCDisassembler_Success;
840
0
}
841
842
static DecodeStatus decodeOffset_128_2Operand(MCInst *Inst, uint64_t Imm,
843
                int64_t Address,
844
                const void *Decoder)
845
0
{
846
0
  CS_ASSERT_RET_VAL(isUIntN(7, Imm) && "Invalid immediate",
847
0
        MCDisassembler_Fail);
848
0
  MCOperand_CreateImm0(Inst, (Imm << 1));
849
0
  return MCDisassembler_Success;
850
0
}
851
852
static DecodeStatus decodeOffset_128_1Operand(MCInst *Inst, uint64_t Imm,
853
                int64_t Address,
854
                const void *Decoder)
855
0
{
856
0
  CS_ASSERT_RET_VAL(isUIntN(7, Imm) && "Invalid immediate",
857
0
        MCDisassembler_Fail);
858
0
  MCOperand_CreateImm0(Inst, (Imm));
859
0
  return MCDisassembler_Success;
860
0
}
861
862
static DecodeStatus decodeOffset_64_16Operand(MCInst *Inst, uint64_t Imm,
863
                int64_t Address,
864
                const void *Decoder)
865
0
{
866
0
  CS_ASSERT_RET_VAL(isUIntN(6, Imm) && "Invalid immediate",
867
0
        MCDisassembler_Fail);
868
0
  MCOperand_CreateImm0(Inst, SignExtend64(Imm << 4, 10));
869
0
  return MCDisassembler_Success;
870
0
}
871
872
static int64_t TableB4const[16] = { -1, 1,  2,  3,  4,  5,  6,   7,
873
            8,  10, 12, 16, 32, 64, 128, 256 };
874
static DecodeStatus decodeB4constOperand(MCInst *Inst, uint64_t Imm,
875
           int64_t Address, const void *Decoder)
876
3.66k
{
877
3.66k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
878
3.66k
        MCDisassembler_Fail);
879
880
3.66k
  MCOperand_CreateImm0(Inst, (TableB4const[Imm]));
881
3.66k
  return MCDisassembler_Success;
882
3.66k
}
883
884
static int64_t TableB4constu[16] = { 32768, 65536, 2,  3,  4,  5,  6, 7,
885
             8,     10,    12, 16, 32, 64, 128, 256 };
886
static DecodeStatus decodeB4constuOperand(MCInst *Inst, uint64_t Imm,
887
            int64_t Address, const void *Decoder)
888
3.13k
{
889
3.13k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
890
3.13k
        MCDisassembler_Fail);
891
892
3.13k
  MCOperand_CreateImm0(Inst, (TableB4constu[Imm]));
893
3.13k
  return MCDisassembler_Success;
894
3.13k
}
895
896
static DecodeStatus decodeMem8Operand(MCInst *Inst, uint64_t Imm,
897
              int64_t Address, const void *Decoder)
898
1.65k
{
899
1.65k
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
900
1.65k
        MCDisassembler_Fail);
901
1.65k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
902
1.65k
  MCOperand_CreateImm0(Inst, ((Imm >> 4) & 0xff));
903
1.65k
  return MCDisassembler_Success;
904
1.65k
}
905
906
static DecodeStatus decodeMem16Operand(MCInst *Inst, uint64_t Imm,
907
               int64_t Address, const void *Decoder)
908
383
{
909
383
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
910
383
        MCDisassembler_Fail);
911
383
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
912
383
  MCOperand_CreateImm0(Inst, ((Imm >> 3) & 0x1fe));
913
383
  return MCDisassembler_Success;
914
383
}
915
916
static DecodeStatus decodeMem32Operand(MCInst *Inst, uint64_t Imm,
917
               int64_t Address, const void *Decoder)
918
1.72k
{
919
1.72k
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
920
1.72k
        MCDisassembler_Fail);
921
1.72k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
922
1.72k
  MCOperand_CreateImm0(Inst, ((Imm >> 2) & 0x3fc));
923
1.72k
  return MCDisassembler_Success;
924
1.72k
}
925
926
static DecodeStatus decodeMem32nOperand(MCInst *Inst, uint64_t Imm,
927
          int64_t Address, const void *Decoder)
928
6.09k
{
929
6.09k
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
930
6.09k
        MCDisassembler_Fail);
931
6.09k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
932
6.09k
  MCOperand_CreateImm0(Inst, ((Imm >> 2) & 0x3c));
933
6.09k
  return MCDisassembler_Success;
934
6.09k
}
935
936
/// Read two bytes from the ArrayRef and return 16 bit data sorted
937
/// according to the given endianness.
938
static DecodeStatus readInstruction16(MCInst *MI, const uint8_t *Bytes,
939
              size_t BytesLen, uint64_t Address,
940
              uint64_t *Size, uint64_t *Insn,
941
              bool IsLittleEndian)
942
63.0k
{
943
  // We want to read exactly 2 Bytes of data.
944
63.0k
  if (BytesLen < 2) {
945
238
    *Size = 0;
946
238
    return MCDisassembler_Fail;
947
238
  }
948
949
62.8k
  *Insn = readBytes16(MI, Bytes);
950
62.8k
  *Size = 2;
951
952
62.8k
  return MCDisassembler_Success;
953
63.0k
}
954
955
/// Read three bytes from the ArrayRef and return 24 bit data
956
static DecodeStatus readInstruction24(MCInst *MI, const uint8_t *Bytes,
957
              size_t BytesLen, uint64_t Address,
958
              uint64_t *Size, uint64_t *Insn,
959
              bool IsLittleEndian, bool CheckTIE)
960
43.6k
{
961
  // We want to read exactly 3 Bytes of data.
962
43.6k
  if (BytesLen < 3) {
963
174
    *Size = 0;
964
174
    return MCDisassembler_Fail;
965
174
  }
966
967
43.4k
  if (CheckTIE && (Bytes[0] & 0x8) != 0)
968
0
    return MCDisassembler_Fail;
969
43.4k
  *Insn = readBytes24(MI, Bytes);
970
43.4k
  *Size = 3;
971
972
43.4k
  return MCDisassembler_Success;
973
43.4k
}
974
975
/// Read three bytes from the ArrayRef and return 32 bit data
976
static DecodeStatus readInstruction32(MCInst *MI, const uint8_t *Bytes,
977
              size_t BytesLen, uint64_t Address,
978
              uint64_t *Size, uint64_t *Insn,
979
              bool IsLittleEndian)
980
0
{
981
  // We want to read exactly 4 Bytes of data.
982
0
  if (BytesLen < 4) {
983
0
    *Size = 0;
984
0
    return MCDisassembler_Fail;
985
0
  }
986
987
0
  if ((Bytes[0] & 0x8) == 0)
988
0
    return MCDisassembler_Fail;
989
0
  *Insn = readBytes32(MI, Bytes);
990
0
  *Size = 4;
991
992
0
  return MCDisassembler_Success;
993
0
}
994
995
/// Read InstSize bytes from the ArrayRef and return 24 bit data
996
/// InstSize cannot be larger than 8.
997
static DecodeStatus readInstructionN(const uint8_t *Bytes, size_t BytesLen,
998
             uint64_t Address, unsigned InstSize,
999
             uint64_t *Size, uint64_t *Insn,
1000
             bool IsLittleEndian)
1001
0
{
1002
  // We want to read exactly 3 Bytes of data.
1003
0
  if (BytesLen < InstSize) {
1004
0
    *Size = 0;
1005
0
    return MCDisassembler_Fail;
1006
0
  }
1007
0
  if (InstSize > 8) {
1008
0
    InstSize = 8;
1009
0
  }
1010
1011
0
  *Insn = 0;
1012
0
  for (unsigned i = 0; i < InstSize; i++)
1013
0
    *Insn |= (uint64_t)(Bytes[i]) << (8 * i);
1014
1015
0
  *Size = InstSize;
1016
0
  return MCDisassembler_Success;
1017
0
}
1018
1019
#include "XtensaGenDisassemblerTables.inc"
1020
1021
FieldFromInstruction(fieldFromInstruction_2, uint64_t);
1022
19.2k
DecodeToMCInst(decodeToMCInst_2, fieldFromInstruction_2, uint64_t);
1023
62.8k
DecodeInstruction(decodeInstruction_2, fieldFromInstruction_2, decodeToMCInst_2,
1024
      uint64_t);
1025
1026
FieldFromInstruction(fieldFromInstruction_4, uint64_t);
1027
0
DecodeToMCInst(decodeToMCInst_4, fieldFromInstruction_4, uint64_t);
1028
0
DecodeInstruction(decodeInstruction_4, fieldFromInstruction_4, decodeToMCInst_4,
1029
      uint64_t);
1030
1031
FieldFromInstruction(fieldFromInstruction_6, uint64_t);
1032
0
DecodeToMCInst(decodeToMCInst_6, fieldFromInstruction_6, uint64_t);
1033
0
DecodeInstruction(decodeInstruction_6, fieldFromInstruction_6, decodeToMCInst_6,
1034
      uint64_t);
1035
1036
static bool hasDensity(MCInst *MI)
1037
63.0k
{
1038
63.0k
  return Xtensa_getFeatureBits(MI->csh->mode, Xtensa_FeatureDensity);
1039
63.0k
}
1040
static bool hasESP32S3Ops(MCInst *MI)
1041
386
{
1042
386
  return Xtensa_getFeatureBits(MI->csh->mode, Xtensa_FeatureESP32S3Ops);
1043
386
}
1044
static bool hasHIFI3(MCInst *MI)
1045
386
{
1046
386
  return Xtensa_getFeatureBits(MI->csh->mode, Xtensa_FeatureHIFI3);
1047
386
}
1048
1049
static DecodeStatus getInstruction(MCInst *MI, uint64_t *Size,
1050
           const uint8_t *Bytes, size_t BytesLen,
1051
           uint64_t Address)
1052
63.0k
{
1053
63.0k
  uint64_t Insn;
1054
63.0k
  DecodeStatus Result;
1055
63.0k
  bool IsLittleEndian = MI->csh->mode & CS_MODE_LITTLE_ENDIAN;
1056
1057
  // Parse 16-bit instructions
1058
63.0k
  if (hasDensity(MI)) {
1059
63.0k
    Result = readInstruction16(MI, Bytes, BytesLen, Address, Size,
1060
63.0k
             &Insn, IsLittleEndian);
1061
63.0k
    if (Result == MCDisassembler_Fail)
1062
238
      return MCDisassembler_Fail;
1063
1064
62.8k
    Result = decodeInstruction_2(DecoderTable16, MI, Insn, Address,
1065
62.8k
               NULL);
1066
62.8k
    if (Result != MCDisassembler_Fail) {
1067
19.2k
      *Size = 2;
1068
19.2k
      return Result;
1069
19.2k
    }
1070
62.8k
  }
1071
1072
  // Parse Core 24-bit instructions
1073
43.6k
  Result = readInstruction24(MI, Bytes, BytesLen, Address, Size, &Insn,
1074
43.6k
           IsLittleEndian, false);
1075
43.6k
  if (Result == MCDisassembler_Fail)
1076
174
    return MCDisassembler_Fail;
1077
1078
43.4k
  Result = decodeInstruction_3(DecoderTable24, MI, Insn, Address, NULL);
1079
43.4k
  if (Result != MCDisassembler_Fail) {
1080
43.0k
    *Size = 3;
1081
43.0k
    return Result;
1082
43.0k
  }
1083
1084
386
  if (hasESP32S3Ops(MI)) {
1085
    // Parse ESP32S3 24-bit instructions
1086
0
    Result = readInstruction24(MI, Bytes, BytesLen, Address, Size,
1087
0
             &Insn, IsLittleEndian, true);
1088
0
    if (Result != MCDisassembler_Fail) {
1089
0
      Result = decodeInstruction_3(DecoderTableESP32S324, MI,
1090
0
                 Insn, Address, NULL);
1091
0
      if (Result != MCDisassembler_Fail) {
1092
0
        *Size = 3;
1093
0
        return Result;
1094
0
      }
1095
0
    }
1096
1097
    // Parse ESP32S3 32-bit instructions
1098
0
    Result = readInstruction32(MI, Bytes, BytesLen, Address, Size,
1099
0
             &Insn, IsLittleEndian);
1100
0
    if (Result == MCDisassembler_Fail)
1101
0
      return MCDisassembler_Fail;
1102
1103
0
    Result = decodeInstruction_4(DecoderTableESP32S332, MI, Insn,
1104
0
               Address, NULL);
1105
0
    if (Result != MCDisassembler_Fail) {
1106
0
      *Size = 4;
1107
0
      return Result;
1108
0
    }
1109
0
  }
1110
1111
386
  if (hasHIFI3(MI)) {
1112
0
    Result = decodeInstruction_3(DecoderTableHIFI324, MI, Insn,
1113
0
               Address, NULL);
1114
0
    if (Result != MCDisassembler_Fail)
1115
0
      return Result;
1116
1117
0
    Result = readInstructionN(Bytes, BytesLen, Address, 6, Size,
1118
0
            &Insn, IsLittleEndian);
1119
0
    if (Result == MCDisassembler_Fail)
1120
0
      return MCDisassembler_Fail;
1121
1122
0
    Result = decodeInstruction_6(DecoderTableHIFI348, MI, Insn,
1123
0
               Address, NULL);
1124
0
    if (Result != MCDisassembler_Fail)
1125
0
      return Result;
1126
0
  }
1127
386
  return Result;
1128
386
}
1129
1130
DecodeStatus Xtensa_LLVM_getInstruction(MCInst *MI, uint16_t *size16,
1131
          const uint8_t *Bytes,
1132
          unsigned BytesSize, uint64_t Address)
1133
63.0k
{
1134
63.0k
  uint64_t size64;
1135
63.0k
  DecodeStatus status =
1136
63.0k
    getInstruction(MI, &size64, Bytes, BytesSize, Address);
1137
63.0k
  CS_ASSERT_RET_VAL(size64 < 0xffff, MCDisassembler_Fail);
1138
63.0k
  *size16 = size64;
1139
63.0k
  return status;
1140
63.0k
}