Coverage Report

Created: 2026-08-31 06:58

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
98.3k
{
94
98.3k
  if (RegNo >= ARR_SIZE(ARDecoderTable))
95
0
    return MCDisassembler_Fail;
96
97
98.3k
  unsigned Reg = ARDecoderTable[RegNo];
98
98.3k
  MCOperand_CreateReg0(Inst, (Reg));
99
98.3k
  return MCDisassembler_Success;
100
98.3k
}
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
9.53k
{
127
9.53k
  if (RegNo >= ARR_SIZE(FPRDecoderTable))
128
0
    return MCDisassembler_Fail;
129
130
9.53k
  unsigned Reg = FPRDecoderTable[RegNo];
131
9.53k
  MCOperand_CreateReg0(Inst, (Reg));
132
9.53k
  return MCDisassembler_Success;
133
9.53k
}
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.27k
{
186
2.27k
  if (RegNo >= ARR_SIZE(BRDecoderTable))
187
0
    return MCDisassembler_Fail;
188
189
2.27k
  unsigned Reg = BRDecoderTable[RegNo];
190
2.27k
  MCOperand_CreateReg0(Inst, (Reg));
191
2.27k
  return MCDisassembler_Success;
192
2.27k
}
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
905
{
200
905
  if (RegNo >= ARR_SIZE(MRDecoderTable))
201
0
    return MCDisassembler_Fail;
202
203
905
  unsigned Reg = MRDecoderTable[RegNo];
204
905
  MCOperand_CreateReg0(Inst, (Reg));
205
905
  return MCDisassembler_Success;
206
905
}
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
1.21k
{
214
1.21k
  if (RegNo >= ARR_SIZE(MR01DecoderTable))
215
0
    return MCDisassembler_Fail;
216
217
1.21k
  unsigned Reg = MR01DecoderTable[RegNo];
218
1.21k
  MCOperand_CreateReg0(Inst, (Reg));
219
1.21k
  return MCDisassembler_Success;
220
1.21k
}
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
1.23k
{
228
1.23k
  if (RegNo >= ARR_SIZE(MR23DecoderTable))
229
0
    return MCDisassembler_Fail;
230
231
1.23k
  unsigned Reg = MR23DecoderTable[RegNo];
232
1.23k
  MCOperand_CreateReg0(Inst, (Reg));
233
1.23k
  return MCDisassembler_Success;
234
1.23k
}
235
236
bool Xtensa_getFeatureBits(unsigned int mode, unsigned int feature)
237
111k
{
238
111k
  switch (feature) {
239
408
  case Xtensa_FeatureESP32S3Ops:
240
    // SIMD/AI "ee.*" ops only exist on the ESP32-S3.
241
408
    return (mode & CS_MODE_XTENSA_ESP32S3) != 0;
242
407
  case Xtensa_FeatureHIFI3:
243
    // HiFi3 DSP ops are gated behind the ESP32-S3 in this tree.
244
407
    return (mode & CS_MODE_XTENSA_ESP32S3) != 0;
245
86.5k
  case Xtensa_FeatureDensity:
246
    // Code Density is a base Tensilica default option.
247
86.5k
    return true;
248
24.1k
  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
24.1k
    return true;
254
111k
  }
255
111k
}
256
257
// Verify SR and UR
258
bool CheckRegister(MCInst *Inst, unsigned RegNo)
259
6.50k
{
260
6.50k
  unsigned NumIntLevels = 0;
261
6.50k
  unsigned NumTimers = 0;
262
6.50k
  unsigned NumMiscSR = 0;
263
6.50k
  bool IsESP32 = false;
264
6.50k
  bool IsESP32S2 = false;
265
6.50k
  bool Res = true;
266
267
  // Assume that CPU is esp32 by default
268
6.50k
  if ((Inst->csh->mode & CS_MODE_XTENSA_ESP32)) {
269
3.89k
    NumIntLevels = 6;
270
3.89k
    NumTimers = 3;
271
3.89k
    NumMiscSR = 4;
272
3.89k
    IsESP32 = true;
273
3.89k
  } else if (Inst->csh->mode & CS_MODE_XTENSA_ESP32S2) {
274
2.13k
    NumIntLevels = 6;
275
2.13k
    NumTimers = 3;
276
2.13k
    NumMiscSR = 4;
277
2.13k
    IsESP32S2 = true;
278
2.13k
  } else if (Inst->csh->mode & CS_MODE_XTENSA_ESP8266) {
279
480
    NumIntLevels = 2;
280
480
    NumTimers = 1;
281
480
  }
282
283
6.50k
  switch (RegNo) {
284
14
  case Xtensa_LBEG:
285
20
  case Xtensa_LEND:
286
21
  case Xtensa_LCOUNT:
287
21
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
288
21
              Xtensa_FeatureLoop);
289
21
    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
1
  case Xtensa_M0:
305
1
  case Xtensa_M1:
306
7
  case Xtensa_M2:
307
7
  case Xtensa_M3:
308
7
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
309
7
              Xtensa_FeatureMAC16);
310
7
    break;
311
0
  case Xtensa_WINDOWBASE:
312
0
  case Xtensa_WINDOWSTART:
313
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
314
0
              Xtensa_FeatureWindowed);
315
0
    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
2
  case Xtensa_ICOUNTLEVEL:
326
2
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
327
2
              Xtensa_FeatureDebug);
328
2
    break;
329
0
  case Xtensa_ATOMCTL:
330
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
331
0
              Xtensa_FeatureATOMCTL);
332
0
    break;
333
2
  case Xtensa_MEMCTL:
334
2
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
335
2
              Xtensa_FeatureMEMCTL);
336
2
    break;
337
0
  case Xtensa_EPC1:
338
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
339
0
              Xtensa_FeatureException);
340
0
    break;
341
84
  case Xtensa_EPC2:
342
159
  case Xtensa_EPC3:
343
389
  case Xtensa_EPC4:
344
797
  case Xtensa_EPC5:
345
819
  case Xtensa_EPC6:
346
905
  case Xtensa_EPC7:
347
905
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
348
905
              Xtensa_FeatureHighPriInterrupts);
349
905
    Res = Res & (NumIntLevels >= (RegNo - Xtensa_EPC1));
350
905
    break;
351
116
  case Xtensa_EPS2:
352
532
  case Xtensa_EPS3:
353
600
  case Xtensa_EPS4:
354
674
  case Xtensa_EPS5:
355
698
  case Xtensa_EPS6:
356
1.17k
  case Xtensa_EPS7:
357
1.17k
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
358
1.17k
              Xtensa_FeatureHighPriInterrupts);
359
1.17k
    Res = Res & (NumIntLevels > (RegNo - Xtensa_EPS2));
360
1.17k
    break;
361
4
  case Xtensa_EXCSAVE1:
362
4
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
363
4
              Xtensa_FeatureException);
364
4
    break;
365
93
  case Xtensa_EXCSAVE2:
366
489
  case Xtensa_EXCSAVE3:
367
1.21k
  case Xtensa_EXCSAVE4:
368
1.27k
  case Xtensa_EXCSAVE5:
369
1.56k
  case Xtensa_EXCSAVE6:
370
2.12k
  case Xtensa_EXCSAVE7:
371
2.12k
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
372
2.12k
              Xtensa_FeatureHighPriInterrupts);
373
2.12k
    Res = Res & (NumIntLevels >= (RegNo - Xtensa_EXCSAVE1));
374
2.12k
    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
37
  case Xtensa_CCOUNT:
390
37
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
391
37
              Xtensa_FeatureTimerInt);
392
37
    Res &= (NumTimers > 0);
393
37
    break;
394
74
  case Xtensa_CCOMPARE0:
395
545
  case Xtensa_CCOMPARE1:
396
646
  case Xtensa_CCOMPARE2:
397
646
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
398
646
              Xtensa_FeatureTimerInt);
399
646
    Res &= (NumTimers > (RegNo - Xtensa_CCOMPARE0));
400
646
    break;
401
0
  case Xtensa_PRID:
402
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
403
0
              Xtensa_FeaturePRID);
404
0
    break;
405
131
  case Xtensa_INTERRUPT:
406
131
  case Xtensa_INTCLEAR:
407
131
  case Xtensa_INTENABLE:
408
131
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
409
131
              Xtensa_FeatureInterrupt);
410
131
    break;
411
101
  case Xtensa_MISC0:
412
253
  case Xtensa_MISC1:
413
571
  case Xtensa_MISC2:
414
873
  case Xtensa_MISC3:
415
873
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
416
873
              Xtensa_FeatureMiscSR);
417
873
    Res &= (NumMiscSR > (RegNo - Xtensa_MISC0));
418
873
    break;
419
1
  case Xtensa_THREADPTR:
420
1
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
421
1
              Xtensa_FeatureTHREADPTR);
422
1
    break;
423
21
  case Xtensa_GPIO_OUT:
424
21
    Res = IsESP32S2;
425
21
    break;
426
75
  case Xtensa_EXPSTATE:
427
75
    Res = IsESP32;
428
75
    break;
429
55
  case Xtensa_FCR:
430
82
  case Xtensa_FSR:
431
82
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
432
82
              Xtensa_FeatureSingleFloat);
433
82
    break;
434
171
  case Xtensa_F64R_LO:
435
386
  case Xtensa_F64R_HI:
436
405
  case Xtensa_F64S:
437
405
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
438
405
              Xtensa_FeatureDFPAccel);
439
405
    break;
440
6.50k
  }
441
442
6.50k
  return Res;
443
6.50k
}
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
5.93k
{
485
  //  const llvm_MCSubtargetInfo STI =
486
  //    ((const MCDisassembler *)Decoder)->getSubtargetInfo();
487
488
5.93k
  if (RegNo > 255)
489
0
    return MCDisassembler_Fail;
490
491
277k
  for (unsigned i = 0; i < ARR_SIZE(SRDecoderTable); i += 2) {
492
277k
    if (SRDecoderTable[i + 1] == RegNo) {
493
5.92k
      unsigned Reg = SRDecoderTable[i];
494
495
5.92k
      if (!CheckRegister(Inst, Reg))
496
5
        return MCDisassembler_Fail;
497
498
5.92k
      MCOperand_CreateReg0(Inst, (Reg));
499
5.92k
      return MCDisassembler_Success;
500
5.92k
    }
501
277k
  }
502
503
5
  return MCDisassembler_Fail;
504
5.93k
}
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
595
{
515
595
  if (RegNo > 255)
516
0
    return MCDisassembler_Fail;
517
518
3.31k
  for (unsigned i = 0; i < ARR_SIZE(URDecoderTable); i += 2) {
519
3.30k
    if (URDecoderTable[i + 1] == RegNo) {
520
584
      unsigned Reg = URDecoderTable[i];
521
522
584
      if (!CheckRegister(Inst, Reg))
523
4
        return MCDisassembler_Fail;
524
525
580
      MCOperand_CreateReg0(Inst, (Reg));
526
580
      return MCDisassembler_Success;
527
584
    }
528
3.30k
  }
529
530
11
  return MCDisassembler_Fail;
531
595
}
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.5k
{
538
  //  return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
539
  //               Offset, /*OpSize=*/0, InstSize);
540
11.5k
  return false;
541
11.5k
}
542
543
static DecodeStatus decodeCallOperand(MCInst *Inst, uint64_t Imm,
544
              int64_t Address, const void *Decoder)
545
2.51k
{
546
2.51k
  CS_ASSERT_RET_VAL(isUIntN(18, Imm) && "Invalid immediate",
547
2.51k
        MCDisassembler_Fail);
548
2.51k
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 2), 20)));
549
2.51k
  return MCDisassembler_Success;
550
2.51k
}
551
552
static DecodeStatus decodeJumpOperand(MCInst *Inst, uint64_t Imm,
553
              int64_t Address, const void *Decoder)
554
995
{
555
995
  CS_ASSERT_RET_VAL(isUIntN(18, Imm) && "Invalid immediate",
556
995
        MCDisassembler_Fail);
557
995
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 18)));
558
995
  return MCDisassembler_Success;
559
995
}
560
561
static DecodeStatus decodeBranchOperand(MCInst *Inst, uint64_t Imm,
562
          int64_t Address, const void *Decoder)
563
11.2k
{
564
11.2k
  switch (MCInst_getOpcode(Inst)) {
565
572
  case Xtensa_BEQZ:
566
831
  case Xtensa_BGEZ:
567
1.17k
  case Xtensa_BLTZ:
568
1.51k
  case Xtensa_BNEZ:
569
1.51k
    CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
570
1.51k
          MCDisassembler_Fail);
571
1.51k
    if (!tryAddingSymbolicOperand(
572
1.51k
          SignExtend64((Imm), 12) + 4 + Address, true,
573
1.51k
          Address, 0, 3, Inst, Decoder))
574
1.51k
      MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 12)));
575
1.51k
    break;
576
9.70k
  default:
577
9.70k
    CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
578
9.70k
          MCDisassembler_Fail);
579
9.70k
    if (!tryAddingSymbolicOperand(
580
9.70k
          SignExtend64((Imm), 8) + 4 + Address, true, Address,
581
9.70k
          0, 3, Inst, Decoder))
582
9.70k
      MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 8)));
583
11.2k
  }
584
11.2k
  return MCDisassembler_Success;
585
11.2k
}
586
587
static DecodeStatus decodeLoopOperand(MCInst *Inst, uint64_t Imm,
588
              int64_t Address, const void *Decoder)
589
342
{
590
342
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
591
342
        MCDisassembler_Fail);
592
342
  if (!tryAddingSymbolicOperand(Imm + 4 + Address, true, Address, 0, 3,
593
342
              Inst, Decoder))
594
342
    MCOperand_CreateImm0(Inst, (Imm));
595
342
  return MCDisassembler_Success;
596
342
}
597
598
static DecodeStatus decodeL32ROperand(MCInst *Inst, uint64_t Imm,
599
              int64_t Address, const void *Decoder)
600
3.71k
{
601
3.71k
  CS_ASSERT_RET_VAL(isUIntN(16, Imm) && "Invalid immediate",
602
3.71k
        MCDisassembler_Fail);
603
3.71k
  MCOperand_CreateImm0(Inst, OneExtend64(Imm << 2, 18));
604
3.71k
  return MCDisassembler_Success;
605
3.71k
}
606
607
static DecodeStatus decodeImm8Operand(MCInst *Inst, uint64_t Imm,
608
              int64_t Address, const void *Decoder)
609
339
{
610
339
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
611
339
        MCDisassembler_Fail);
612
339
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 8)));
613
339
  return MCDisassembler_Success;
614
339
}
615
616
static DecodeStatus decodeImm8_sh8Operand(MCInst *Inst, uint64_t Imm,
617
            int64_t Address, const void *Decoder)
618
265
{
619
265
  CS_ASSERT_RET_VAL(isUIntN(16, Imm) && ((Imm & 0xff) == 0) &&
620
265
          "Invalid immediate",
621
265
        MCDisassembler_Fail);
622
265
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 16)));
623
265
  return MCDisassembler_Success;
624
265
}
625
626
static DecodeStatus decodeImm12Operand(MCInst *Inst, uint64_t Imm,
627
               int64_t Address, const void *Decoder)
628
378
{
629
378
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
630
378
        MCDisassembler_Fail);
631
378
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 12)));
632
378
  return MCDisassembler_Success;
633
378
}
634
635
static DecodeStatus decodeUimm4Operand(MCInst *Inst, uint64_t Imm,
636
               int64_t Address, const void *Decoder)
637
3.07k
{
638
3.07k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
639
3.07k
        MCDisassembler_Fail);
640
3.07k
  MCOperand_CreateImm0(Inst, (Imm));
641
3.07k
  return MCDisassembler_Success;
642
3.07k
}
643
644
static DecodeStatus decodeUimm5Operand(MCInst *Inst, uint64_t Imm,
645
               int64_t Address, const void *Decoder)
646
2.51k
{
647
2.51k
  CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate",
648
2.51k
        MCDisassembler_Fail);
649
2.51k
  MCOperand_CreateImm0(Inst, (Imm));
650
2.51k
  return MCDisassembler_Success;
651
2.51k
}
652
653
static DecodeStatus decodeImm1_16Operand(MCInst *Inst, uint64_t Imm,
654
           int64_t Address, const void *Decoder)
655
777
{
656
777
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
657
777
        MCDisassembler_Fail);
658
777
  MCOperand_CreateImm0(Inst, (Imm + 1));
659
777
  return MCDisassembler_Success;
660
777
}
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
272
    MCOperand_CreateImm0(Inst, (-1));
669
4.28k
  else
670
4.28k
    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.87k
{
677
1.87k
  CS_ASSERT_RET_VAL(isUIntN(7, Imm) && "Invalid immediate",
678
1.87k
        MCDisassembler_Fail);
679
1.87k
  if ((Imm & 0x60) == 0x60)
680
968
    MCOperand_CreateImm0(Inst, ((~0x1f) | Imm));
681
908
  else
682
908
    MCOperand_CreateImm0(Inst, (Imm));
683
1.87k
  return MCDisassembler_Success;
684
1.87k
}
685
686
static DecodeStatus decodeImm8n_7Operand(MCInst *Inst, uint64_t Imm,
687
           int64_t Address, const void *Decoder)
688
126
{
689
126
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
690
126
        MCDisassembler_Fail);
691
126
  if (Imm > 7)
692
59
    MCOperand_CreateImm0(Inst, (Imm - 16));
693
67
  else
694
67
    MCOperand_CreateImm0(Inst, (Imm));
695
126
  return MCDisassembler_Success;
696
126
}
697
698
static DecodeStatus decodeImm64n_4nOperand(MCInst *Inst, uint64_t Imm,
699
             int64_t Address, const void *Decoder)
700
104
{
701
104
  CS_ASSERT_RET_VAL(isUIntN(6, Imm) && ((Imm & 0x3) == 0) &&
702
104
          "Invalid immediate",
703
104
        MCDisassembler_Fail);
704
104
  MCOperand_CreateImm0(Inst, ((~0x3f) | (Imm)));
705
104
  return MCDisassembler_Success;
706
104
}
707
708
static DecodeStatus decodeOffset8m32Operand(MCInst *Inst, uint64_t Imm,
709
              int64_t Address,
710
              const void *Decoder)
711
764
{
712
764
  CS_ASSERT_RET_VAL(isUIntN(10, Imm) && ((Imm & 0x3) == 0) &&
713
764
          "Invalid immediate",
714
764
        MCDisassembler_Fail);
715
764
  MCOperand_CreateImm0(Inst, (Imm));
716
764
  return MCDisassembler_Success;
717
764
}
718
719
static DecodeStatus decodeEntry_Imm12OpValue(MCInst *Inst, uint64_t Imm,
720
               int64_t Address,
721
               const void *Decoder)
722
248
{
723
248
  CS_ASSERT_RET_VAL(isUIntN(15, Imm) && ((Imm & 0x7) == 0) &&
724
248
          "Invalid immediate",
725
248
        MCDisassembler_Fail);
726
248
  MCOperand_CreateImm0(Inst, (Imm));
727
248
  return MCDisassembler_Success;
728
248
}
729
730
static DecodeStatus decodeShimm1_31Operand(MCInst *Inst, uint64_t Imm,
731
             int64_t Address, const void *Decoder)
732
645
{
733
645
  CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate",
734
645
        MCDisassembler_Fail);
735
645
  MCOperand_CreateImm0(Inst, (32 - Imm));
736
645
  return MCDisassembler_Success;
737
645
}
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
163
{
750
163
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
751
163
        MCDisassembler_Fail);
752
163
  MCOperand_CreateImm0(Inst, (Imm + 7));
753
163
  return MCDisassembler_Success;
754
163
}
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
88
{
796
88
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
797
88
        MCDisassembler_Fail);
798
88
  MCOperand_CreateImm0(Inst, (Imm));
799
88
  return MCDisassembler_Success;
800
88
}
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.45k
{
877
3.45k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
878
3.45k
        MCDisassembler_Fail);
879
880
3.45k
  MCOperand_CreateImm0(Inst, (TableB4const[Imm]));
881
3.45k
  return MCDisassembler_Success;
882
3.45k
}
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.73k
{
889
3.73k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
890
3.73k
        MCDisassembler_Fail);
891
892
3.73k
  MCOperand_CreateImm0(Inst, (TableB4constu[Imm]));
893
3.73k
  return MCDisassembler_Success;
894
3.73k
}
895
896
static DecodeStatus decodeMem8Operand(MCInst *Inst, uint64_t Imm,
897
              int64_t Address, const void *Decoder)
898
1.52k
{
899
1.52k
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
900
1.52k
        MCDisassembler_Fail);
901
1.52k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
902
1.52k
  MCOperand_CreateImm0(Inst, ((Imm >> 4) & 0xff));
903
1.52k
  return MCDisassembler_Success;
904
1.52k
}
905
906
static DecodeStatus decodeMem16Operand(MCInst *Inst, uint64_t Imm,
907
               int64_t Address, const void *Decoder)
908
377
{
909
377
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
910
377
        MCDisassembler_Fail);
911
377
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
912
377
  MCOperand_CreateImm0(Inst, ((Imm >> 3) & 0x1fe));
913
377
  return MCDisassembler_Success;
914
377
}
915
916
static DecodeStatus decodeMem32Operand(MCInst *Inst, uint64_t Imm,
917
               int64_t Address, const void *Decoder)
918
1.93k
{
919
1.93k
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
920
1.93k
        MCDisassembler_Fail);
921
1.93k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
922
1.93k
  MCOperand_CreateImm0(Inst, ((Imm >> 2) & 0x3fc));
923
1.93k
  return MCDisassembler_Success;
924
1.93k
}
925
926
static DecodeStatus decodeMem32nOperand(MCInst *Inst, uint64_t Imm,
927
          int64_t Address, const void *Decoder)
928
5.48k
{
929
5.48k
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
930
5.48k
        MCDisassembler_Fail);
931
5.48k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
932
5.48k
  MCOperand_CreateImm0(Inst, ((Imm >> 2) & 0x3c));
933
5.48k
  return MCDisassembler_Success;
934
5.48k
}
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
65.2k
{
943
  // We want to read exactly 2 Bytes of data.
944
65.2k
  if (BytesLen < 2) {
945
270
    *Size = 0;
946
270
    return MCDisassembler_Fail;
947
270
  }
948
949
65.0k
  *Insn = readBytes16(MI, Bytes);
950
65.0k
  *Size = 2;
951
952
65.0k
  return MCDisassembler_Success;
953
65.2k
}
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
45.6k
{
961
  // We want to read exactly 3 Bytes of data.
962
45.6k
  if (BytesLen < 3) {
963
159
    *Size = 0;
964
159
    return MCDisassembler_Fail;
965
159
  }
966
967
45.4k
  if (CheckTIE && (Bytes[0] & 0x8) != 0)
968
0
    return MCDisassembler_Fail;
969
45.4k
  *Insn = readBytes24(MI, Bytes);
970
45.4k
  *Size = 3;
971
972
45.4k
  return MCDisassembler_Success;
973
45.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.3k
DecodeToMCInst(decodeToMCInst_2, fieldFromInstruction_2, uint64_t);
1023
65.0k
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
65.2k
{
1038
65.2k
  return Xtensa_getFeatureBits(MI->csh->mode, Xtensa_FeatureDensity);
1039
65.2k
}
1040
static bool hasESP32S3Ops(MCInst *MI)
1041
407
{
1042
407
  return Xtensa_getFeatureBits(MI->csh->mode, Xtensa_FeatureESP32S3Ops);
1043
407
}
1044
static bool hasHIFI3(MCInst *MI)
1045
407
{
1046
407
  return Xtensa_getFeatureBits(MI->csh->mode, Xtensa_FeatureHIFI3);
1047
407
}
1048
1049
static DecodeStatus getInstruction(MCInst *MI, uint64_t *Size,
1050
           const uint8_t *Bytes, size_t BytesLen,
1051
           uint64_t Address)
1052
65.2k
{
1053
65.2k
  uint64_t Insn;
1054
65.2k
  DecodeStatus Result;
1055
65.2k
  bool IsLittleEndian = MI->csh->mode & CS_MODE_LITTLE_ENDIAN;
1056
1057
  // Parse 16-bit instructions
1058
65.2k
  if (hasDensity(MI)) {
1059
65.2k
    Result = readInstruction16(MI, Bytes, BytesLen, Address, Size,
1060
65.2k
             &Insn, IsLittleEndian);
1061
65.2k
    if (Result == MCDisassembler_Fail)
1062
270
      return MCDisassembler_Fail;
1063
1064
65.0k
    Result = decodeInstruction_2(DecoderTable16, MI, Insn, Address,
1065
65.0k
               NULL);
1066
65.0k
    if (Result != MCDisassembler_Fail) {
1067
19.3k
      *Size = 2;
1068
19.3k
      return Result;
1069
19.3k
    }
1070
65.0k
  }
1071
1072
  // Parse Core 24-bit instructions
1073
45.6k
  Result = readInstruction24(MI, Bytes, BytesLen, Address, Size, &Insn,
1074
45.6k
           IsLittleEndian, false);
1075
45.6k
  if (Result == MCDisassembler_Fail)
1076
159
    return MCDisassembler_Fail;
1077
1078
45.4k
  Result = decodeInstruction_3(DecoderTable24, MI, Insn, Address, NULL);
1079
45.4k
  if (Result != MCDisassembler_Fail) {
1080
45.0k
    *Size = 3;
1081
45.0k
    return Result;
1082
45.0k
  }
1083
1084
407
  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
407
  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
407
  return Result;
1128
407
}
1129
1130
DecodeStatus Xtensa_LLVM_getInstruction(MCInst *MI, uint16_t *size16,
1131
          const uint8_t *Bytes,
1132
          unsigned BytesSize, uint64_t Address)
1133
65.2k
{
1134
65.2k
  uint64_t size64;
1135
65.2k
  DecodeStatus status =
1136
65.2k
    getInstruction(MI, &size64, Bytes, BytesSize, Address);
1137
65.2k
  CS_ASSERT_RET_VAL(size64 < 0xffff, MCDisassembler_Fail);
1138
65.2k
  *size16 = size64;
1139
65.2k
  return status;
1140
65.2k
}