Coverage Report

Created: 2026-07-16 06:55

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
179k
{
94
179k
  if (RegNo >= ARR_SIZE(ARDecoderTable))
95
0
    return MCDisassembler_Fail;
96
97
179k
  unsigned Reg = ARDecoderTable[RegNo];
98
179k
  MCOperand_CreateReg0(Inst, (Reg));
99
179k
  return MCDisassembler_Success;
100
179k
}
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
63.6k
{
109
63.6k
  if (RegNo >= ARR_SIZE(QRDecoderTable))
110
0
    return MCDisassembler_Fail;
111
112
63.6k
  unsigned Reg = QRDecoderTable[RegNo];
113
63.6k
  MCOperand_CreateReg0(Inst, (Reg));
114
63.6k
  return MCDisassembler_Success;
115
63.6k
}
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
17.4k
{
127
17.4k
  if (RegNo >= ARR_SIZE(FPRDecoderTable))
128
0
    return MCDisassembler_Fail;
129
130
17.4k
  unsigned Reg = FPRDecoderTable[RegNo];
131
17.4k
  MCOperand_CreateReg0(Inst, (Reg));
132
17.4k
  return MCDisassembler_Success;
133
17.4k
}
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.62k
{
186
2.62k
  if (RegNo >= ARR_SIZE(BRDecoderTable))
187
0
    return MCDisassembler_Fail;
188
189
2.62k
  unsigned Reg = BRDecoderTable[RegNo];
190
2.62k
  MCOperand_CreateReg0(Inst, (Reg));
191
2.62k
  return MCDisassembler_Success;
192
2.62k
}
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
1.35k
{
200
1.35k
  if (RegNo >= ARR_SIZE(MRDecoderTable))
201
0
    return MCDisassembler_Fail;
202
203
1.35k
  unsigned Reg = MRDecoderTable[RegNo];
204
1.35k
  MCOperand_CreateReg0(Inst, (Reg));
205
1.35k
  return MCDisassembler_Success;
206
1.35k
}
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
897
{
214
897
  if (RegNo >= ARR_SIZE(MR01DecoderTable))
215
0
    return MCDisassembler_Fail;
216
217
897
  unsigned Reg = MR01DecoderTable[RegNo];
218
897
  MCOperand_CreateReg0(Inst, (Reg));
219
897
  return MCDisassembler_Success;
220
897
}
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
587
{
228
587
  if (RegNo >= ARR_SIZE(MR23DecoderTable))
229
0
    return MCDisassembler_Fail;
230
231
587
  unsigned Reg = MR23DecoderTable[RegNo];
232
587
  MCOperand_CreateReg0(Inst, (Reg));
233
587
  return MCDisassembler_Success;
234
587
}
235
236
bool Xtensa_getFeatureBits(unsigned int mode, unsigned int feature)
237
80.6k
{
238
  // we support everything
239
80.6k
  return true;
240
80.6k
}
241
242
// Verify SR and UR
243
bool CheckRegister(MCInst *Inst, unsigned RegNo)
244
8.88k
{
245
8.88k
  unsigned NumIntLevels = 0;
246
8.88k
  unsigned NumTimers = 0;
247
8.88k
  unsigned NumMiscSR = 0;
248
8.88k
  bool IsESP32 = false;
249
8.88k
  bool IsESP32S2 = false;
250
8.88k
  bool Res = true;
251
252
  // Assume that CPU is esp32 by default
253
8.88k
  if ((Inst->csh->mode & CS_MODE_XTENSA_ESP32)) {
254
6.46k
    NumIntLevels = 6;
255
6.46k
    NumTimers = 3;
256
6.46k
    NumMiscSR = 4;
257
6.46k
    IsESP32 = true;
258
6.46k
  } else if (Inst->csh->mode & CS_MODE_XTENSA_ESP32S2) {
259
1.07k
    NumIntLevels = 6;
260
1.07k
    NumTimers = 3;
261
1.07k
    NumMiscSR = 4;
262
1.07k
    IsESP32S2 = true;
263
1.34k
  } else if (Inst->csh->mode & CS_MODE_XTENSA_ESP8266) {
264
1.34k
    NumIntLevels = 2;
265
1.34k
    NumTimers = 1;
266
1.34k
  }
267
268
8.88k
  switch (RegNo) {
269
67
  case Xtensa_LBEG:
270
72
  case Xtensa_LEND:
271
72
  case Xtensa_LCOUNT:
272
72
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
273
72
              Xtensa_FeatureLoop);
274
72
    break;
275
0
  case Xtensa_BREG:
276
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
277
0
              Xtensa_FeatureBoolean);
278
0
    break;
279
39
  case Xtensa_LITBASE:
280
39
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
281
39
              Xtensa_FeatureExtendedL32R);
282
39
    break;
283
0
  case Xtensa_SCOMPARE1:
284
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
285
0
              Xtensa_FeatureS32C1I);
286
0
    break;
287
3
  case Xtensa_ACCLO:
288
3
  case Xtensa_ACCHI:
289
5
  case Xtensa_M0:
290
5
  case Xtensa_M1:
291
5
  case Xtensa_M2:
292
33
  case Xtensa_M3:
293
33
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
294
33
              Xtensa_FeatureMAC16);
295
33
    break;
296
0
  case Xtensa_WINDOWBASE:
297
0
  case Xtensa_WINDOWSTART:
298
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
299
0
              Xtensa_FeatureWindowed);
300
0
    break;
301
0
  case Xtensa_IBREAKENABLE:
302
0
  case Xtensa_IBREAKA0:
303
0
  case Xtensa_IBREAKA1:
304
0
  case Xtensa_DBREAKA0:
305
0
  case Xtensa_DBREAKA1:
306
94
  case Xtensa_DBREAKC0:
307
94
  case Xtensa_DBREAKC1:
308
94
  case Xtensa_DEBUGCAUSE:
309
94
  case Xtensa_ICOUNT:
310
94
  case Xtensa_ICOUNTLEVEL:
311
94
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
312
94
              Xtensa_FeatureDebug);
313
94
    break;
314
0
  case Xtensa_ATOMCTL:
315
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
316
0
              Xtensa_FeatureATOMCTL);
317
0
    break;
318
10
  case Xtensa_MEMCTL:
319
10
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
320
10
              Xtensa_FeatureMEMCTL);
321
10
    break;
322
6
  case Xtensa_EPC1:
323
6
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
324
6
              Xtensa_FeatureException);
325
6
    break;
326
208
  case Xtensa_EPC2:
327
281
  case Xtensa_EPC3:
328
696
  case Xtensa_EPC4:
329
959
  case Xtensa_EPC5:
330
1.19k
  case Xtensa_EPC6:
331
1.96k
  case Xtensa_EPC7:
332
1.96k
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
333
1.96k
              Xtensa_FeatureHighPriInterrupts);
334
1.96k
    Res = Res & (NumIntLevels >= (RegNo - Xtensa_EPC1));
335
1.96k
    break;
336
542
  case Xtensa_EPS2:
337
781
  case Xtensa_EPS3:
338
859
  case Xtensa_EPS4:
339
948
  case Xtensa_EPS5:
340
1.06k
  case Xtensa_EPS6:
341
1.26k
  case Xtensa_EPS7:
342
1.26k
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
343
1.26k
              Xtensa_FeatureHighPriInterrupts);
344
1.26k
    Res = Res & (NumIntLevels > (RegNo - Xtensa_EPS2));
345
1.26k
    break;
346
4
  case Xtensa_EXCSAVE1:
347
4
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
348
4
              Xtensa_FeatureException);
349
4
    break;
350
103
  case Xtensa_EXCSAVE2:
351
261
  case Xtensa_EXCSAVE3:
352
631
  case Xtensa_EXCSAVE4:
353
881
  case Xtensa_EXCSAVE5:
354
956
  case Xtensa_EXCSAVE6:
355
1.09k
  case Xtensa_EXCSAVE7:
356
1.09k
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
357
1.09k
              Xtensa_FeatureHighPriInterrupts);
358
1.09k
    Res = Res & (NumIntLevels >= (RegNo - Xtensa_EXCSAVE1));
359
1.09k
    break;
360
0
  case Xtensa_DEPC:
361
0
  case Xtensa_EXCCAUSE:
362
0
  case Xtensa_EXCVADDR:
363
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
364
0
              Xtensa_FeatureException);
365
0
    break;
366
0
  case Xtensa_CPENABLE:
367
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
368
0
              Xtensa_FeatureCoprocessor);
369
0
    break;
370
0
  case Xtensa_VECBASE:
371
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
372
0
              Xtensa_FeatureRelocatableVector);
373
0
    break;
374
100
  case Xtensa_CCOUNT:
375
100
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
376
100
              Xtensa_FeatureTimerInt);
377
100
    Res &= (NumTimers > 0);
378
100
    break;
379
920
  case Xtensa_CCOMPARE0:
380
1.50k
  case Xtensa_CCOMPARE1:
381
1.91k
  case Xtensa_CCOMPARE2:
382
1.91k
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
383
1.91k
              Xtensa_FeatureTimerInt);
384
1.91k
    Res &= (NumTimers > (RegNo - Xtensa_CCOMPARE0));
385
1.91k
    break;
386
0
  case Xtensa_PRID:
387
0
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
388
0
              Xtensa_FeaturePRID);
389
0
    break;
390
233
  case Xtensa_INTERRUPT:
391
235
  case Xtensa_INTCLEAR:
392
235
  case Xtensa_INTENABLE:
393
235
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
394
235
              Xtensa_FeatureInterrupt);
395
235
    break;
396
59
  case Xtensa_MISC0:
397
79
  case Xtensa_MISC1:
398
365
  case Xtensa_MISC2:
399
533
  case Xtensa_MISC3:
400
533
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
401
533
              Xtensa_FeatureMiscSR);
402
533
    Res &= (NumMiscSR > (RegNo - Xtensa_MISC0));
403
533
    break;
404
1
  case Xtensa_THREADPTR:
405
1
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
406
1
              Xtensa_FeatureTHREADPTR);
407
1
    break;
408
534
  case Xtensa_GPIO_OUT:
409
534
    Res = IsESP32S2;
410
534
    break;
411
98
  case Xtensa_EXPSTATE:
412
98
    Res = IsESP32;
413
98
    break;
414
4
  case Xtensa_FCR:
415
60
  case Xtensa_FSR:
416
60
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
417
60
              Xtensa_FeatureSingleFloat);
418
60
    break;
419
523
  case Xtensa_F64R_LO:
420
722
  case Xtensa_F64R_HI:
421
812
  case Xtensa_F64S:
422
812
    Res = Xtensa_getFeatureBits(Inst->csh->mode,
423
812
              Xtensa_FeatureDFPAccel);
424
812
    break;
425
8.88k
  }
426
427
8.88k
  return Res;
428
8.88k
}
429
430
static const unsigned SRDecoderTable[] = {
431
  Xtensa_LBEG,      0,   Xtensa_LEND,       1,
432
  Xtensa_LCOUNT,      2,   Xtensa_SAR,        3,
433
  Xtensa_BREG,      4,   Xtensa_LITBASE,      5,
434
  Xtensa_SCOMPARE1,   12,  Xtensa_ACCLO,        16,
435
  Xtensa_ACCHI,     17,  Xtensa_M0,       32,
436
  Xtensa_M1,      33,  Xtensa_M2,       34,
437
  Xtensa_M3,      35,  Xtensa_WINDOWBASE,   72,
438
  Xtensa_WINDOWSTART, 73,  Xtensa_IBREAKENABLE, 96,
439
  Xtensa_MEMCTL,      97,  Xtensa_ATOMCTL,      99,
440
  Xtensa_DDR,     104, Xtensa_IBREAKA0,     128,
441
  Xtensa_IBREAKA1,    129, Xtensa_DBREAKA0,     144,
442
  Xtensa_DBREAKA1,    145, Xtensa_DBREAKC0,     160,
443
  Xtensa_DBREAKC1,    161, Xtensa_CONFIGID0,    176,
444
  Xtensa_EPC1,      177, Xtensa_EPC2,       178,
445
  Xtensa_EPC3,      179, Xtensa_EPC4,       180,
446
  Xtensa_EPC5,      181, Xtensa_EPC6,       182,
447
  Xtensa_EPC7,      183, Xtensa_DEPC,       192,
448
  Xtensa_EPS2,      194, Xtensa_EPS3,       195,
449
  Xtensa_EPS4,      196, Xtensa_EPS5,       197,
450
  Xtensa_EPS6,      198, Xtensa_EPS7,       199,
451
  Xtensa_CONFIGID1,   208, Xtensa_EXCSAVE1,     209,
452
  Xtensa_EXCSAVE2,    210, Xtensa_EXCSAVE3,     211,
453
  Xtensa_EXCSAVE4,    212, Xtensa_EXCSAVE5,     213,
454
  Xtensa_EXCSAVE6,    214, Xtensa_EXCSAVE7,     215,
455
  Xtensa_CPENABLE,    224, Xtensa_INTERRUPT,    226,
456
  Xtensa_INTCLEAR,    227, Xtensa_INTENABLE,    228,
457
  Xtensa_PS,      230, Xtensa_VECBASE,      231,
458
  Xtensa_EXCCAUSE,    232, Xtensa_DEBUGCAUSE,   233,
459
  Xtensa_CCOUNT,      234, Xtensa_PRID,       235,
460
  Xtensa_ICOUNT,      236, Xtensa_ICOUNTLEVEL,  237,
461
  Xtensa_EXCVADDR,    238, Xtensa_CCOMPARE0,    240,
462
  Xtensa_CCOMPARE1,   241, Xtensa_CCOMPARE2,    242,
463
  Xtensa_MISC0,     244, Xtensa_MISC1,        245,
464
  Xtensa_MISC2,     246, Xtensa_MISC3,        247
465
};
466
467
static DecodeStatus DecodeSRRegisterClass(MCInst *Inst, uint64_t RegNo,
468
            uint64_t Address, const void *Decoder)
469
7.39k
{
470
  //  const llvm_MCSubtargetInfo STI =
471
  //    ((const MCDisassembler *)Decoder)->getSubtargetInfo();
472
473
7.39k
  if (RegNo > 255)
474
0
    return MCDisassembler_Fail;
475
476
335k
  for (unsigned i = 0; i < ARR_SIZE(SRDecoderTable); i += 2) {
477
335k
    if (SRDecoderTable[i + 1] == RegNo) {
478
7.37k
      unsigned Reg = SRDecoderTable[i];
479
480
7.37k
      if (!CheckRegister(Inst, Reg))
481
12
        return MCDisassembler_Fail;
482
483
7.36k
      MCOperand_CreateReg0(Inst, (Reg));
484
7.36k
      return MCDisassembler_Success;
485
7.37k
    }
486
335k
  }
487
488
14
  return MCDisassembler_Fail;
489
7.39k
}
490
491
static const unsigned URDecoderTable[] = {
492
  Xtensa_GPIO_OUT, 0,   Xtensa_EXPSTATE, 230, Xtensa_THREADPTR, 231,
493
  Xtensa_FCR,  232, Xtensa_FSR,      233, Xtensa_F64R_LO,   234,
494
  Xtensa_F64R_HI,  235, Xtensa_F64S,     236
495
};
496
497
static DecodeStatus DecodeURRegisterClass(MCInst *Inst, uint64_t RegNo,
498
            uint64_t Address, const void *Decoder)
499
1.56k
{
500
1.56k
  if (RegNo > 255)
501
0
    return MCDisassembler_Fail;
502
503
6.82k
  for (unsigned i = 0; i < ARR_SIZE(URDecoderTable); i += 2) {
504
6.76k
    if (URDecoderTable[i + 1] == RegNo) {
505
1.50k
      unsigned Reg = URDecoderTable[i];
506
507
1.50k
      if (!CheckRegister(Inst, Reg))
508
533
        return MCDisassembler_Fail;
509
510
972
      MCOperand_CreateReg0(Inst, (Reg));
511
972
      return MCDisassembler_Success;
512
1.50k
    }
513
6.76k
  }
514
515
61
  return MCDisassembler_Fail;
516
1.56k
}
517
518
static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
519
             uint64_t Address, uint64_t Offset,
520
             uint64_t InstSize, MCInst *MI,
521
             const void *Decoder)
522
21.6k
{
523
  //  return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
524
  //               Offset, /*OpSize=*/0, InstSize);
525
21.6k
  return false;
526
21.6k
}
527
528
static DecodeStatus decodeCallOperand(MCInst *Inst, uint64_t Imm,
529
              int64_t Address, const void *Decoder)
530
3.90k
{
531
3.90k
  CS_ASSERT_RET_VAL(isUIntN(18, Imm) && "Invalid immediate",
532
3.90k
        MCDisassembler_Fail);
533
3.90k
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm << 2), 20)));
534
3.90k
  return MCDisassembler_Success;
535
3.90k
}
536
537
static DecodeStatus decodeJumpOperand(MCInst *Inst, uint64_t Imm,
538
              int64_t Address, const void *Decoder)
539
1.34k
{
540
1.34k
  CS_ASSERT_RET_VAL(isUIntN(18, Imm) && "Invalid immediate",
541
1.34k
        MCDisassembler_Fail);
542
1.34k
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 18)));
543
1.34k
  return MCDisassembler_Success;
544
1.34k
}
545
546
static DecodeStatus decodeBranchOperand(MCInst *Inst, uint64_t Imm,
547
          int64_t Address, const void *Decoder)
548
21.5k
{
549
21.5k
  switch (MCInst_getOpcode(Inst)) {
550
442
  case Xtensa_BEQZ:
551
775
  case Xtensa_BGEZ:
552
1.04k
  case Xtensa_BLTZ:
553
1.96k
  case Xtensa_BNEZ:
554
1.96k
    CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
555
1.96k
          MCDisassembler_Fail);
556
1.96k
    if (!tryAddingSymbolicOperand(
557
1.96k
          SignExtend64((Imm), 12) + 4 + Address, true,
558
1.96k
          Address, 0, 3, Inst, Decoder))
559
1.96k
      MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 12)));
560
1.96k
    break;
561
19.5k
  default:
562
19.5k
    CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
563
19.5k
          MCDisassembler_Fail);
564
19.5k
    if (!tryAddingSymbolicOperand(
565
19.5k
          SignExtend64((Imm), 8) + 4 + Address, true, Address,
566
19.5k
          0, 3, Inst, Decoder))
567
19.5k
      MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 8)));
568
21.5k
  }
569
21.5k
  return MCDisassembler_Success;
570
21.5k
}
571
572
static DecodeStatus decodeLoopOperand(MCInst *Inst, uint64_t Imm,
573
              int64_t Address, const void *Decoder)
574
128
{
575
128
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
576
128
        MCDisassembler_Fail);
577
128
  if (!tryAddingSymbolicOperand(Imm + 4 + Address, true, Address, 0, 3,
578
128
              Inst, Decoder))
579
128
    MCOperand_CreateImm0(Inst, (Imm));
580
128
  return MCDisassembler_Success;
581
128
}
582
583
static DecodeStatus decodeL32ROperand(MCInst *Inst, uint64_t Imm,
584
              int64_t Address, const void *Decoder)
585
7.06k
{
586
7.06k
  CS_ASSERT_RET_VAL(isUIntN(16, Imm) && "Invalid immediate",
587
7.06k
        MCDisassembler_Fail);
588
7.06k
  MCOperand_CreateImm0(Inst, OneExtend64(Imm << 2, 18));
589
7.06k
  return MCDisassembler_Success;
590
7.06k
}
591
592
static DecodeStatus decodeImm8Operand(MCInst *Inst, uint64_t Imm,
593
              int64_t Address, const void *Decoder)
594
286
{
595
286
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
596
286
        MCDisassembler_Fail);
597
286
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 8)));
598
286
  return MCDisassembler_Success;
599
286
}
600
601
static DecodeStatus decodeImm8_sh8Operand(MCInst *Inst, uint64_t Imm,
602
            int64_t Address, const void *Decoder)
603
322
{
604
322
  CS_ASSERT_RET_VAL(isUIntN(16, Imm) && ((Imm & 0xff) == 0) &&
605
322
          "Invalid immediate",
606
322
        MCDisassembler_Fail);
607
322
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 16)));
608
322
  return MCDisassembler_Success;
609
322
}
610
611
static DecodeStatus decodeImm12Operand(MCInst *Inst, uint64_t Imm,
612
               int64_t Address, const void *Decoder)
613
318
{
614
318
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
615
318
        MCDisassembler_Fail);
616
318
  MCOperand_CreateImm0(Inst, (SignExtend64((Imm), 12)));
617
318
  return MCDisassembler_Success;
618
318
}
619
620
static DecodeStatus decodeUimm4Operand(MCInst *Inst, uint64_t Imm,
621
               int64_t Address, const void *Decoder)
622
3.49k
{
623
3.49k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
624
3.49k
        MCDisassembler_Fail);
625
3.49k
  MCOperand_CreateImm0(Inst, (Imm));
626
3.49k
  return MCDisassembler_Success;
627
3.49k
}
628
629
static DecodeStatus decodeUimm5Operand(MCInst *Inst, uint64_t Imm,
630
               int64_t Address, const void *Decoder)
631
3.90k
{
632
3.90k
  CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate",
633
3.90k
        MCDisassembler_Fail);
634
3.90k
  MCOperand_CreateImm0(Inst, (Imm));
635
3.90k
  return MCDisassembler_Success;
636
3.90k
}
637
638
static DecodeStatus decodeImm1_16Operand(MCInst *Inst, uint64_t Imm,
639
           int64_t Address, const void *Decoder)
640
1.57k
{
641
1.57k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
642
1.57k
        MCDisassembler_Fail);
643
1.57k
  MCOperand_CreateImm0(Inst, (Imm + 1));
644
1.57k
  return MCDisassembler_Success;
645
1.57k
}
646
647
static DecodeStatus decodeImm1n_15Operand(MCInst *Inst, uint64_t Imm,
648
            int64_t Address, const void *Decoder)
649
3.41k
{
650
3.41k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
651
3.41k
        MCDisassembler_Fail);
652
3.41k
  if (!Imm)
653
276
    MCOperand_CreateImm0(Inst, (-1));
654
3.14k
  else
655
3.14k
    MCOperand_CreateImm0(Inst, (Imm));
656
3.41k
  return MCDisassembler_Success;
657
3.41k
}
658
659
static DecodeStatus decodeImm32n_95Operand(MCInst *Inst, uint64_t Imm,
660
             int64_t Address, const void *Decoder)
661
2.05k
{
662
2.05k
  CS_ASSERT_RET_VAL(isUIntN(7, Imm) && "Invalid immediate",
663
2.05k
        MCDisassembler_Fail);
664
2.05k
  if ((Imm & 0x60) == 0x60)
665
909
    MCOperand_CreateImm0(Inst, ((~0x1f) | Imm));
666
1.15k
  else
667
1.15k
    MCOperand_CreateImm0(Inst, (Imm));
668
2.05k
  return MCDisassembler_Success;
669
2.05k
}
670
671
static DecodeStatus decodeImm8n_7Operand(MCInst *Inst, uint64_t Imm,
672
           int64_t Address, const void *Decoder)
673
557
{
674
557
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
675
557
        MCDisassembler_Fail);
676
557
  if (Imm > 7)
677
34
    MCOperand_CreateImm0(Inst, (Imm - 16));
678
523
  else
679
523
    MCOperand_CreateImm0(Inst, (Imm));
680
557
  return MCDisassembler_Success;
681
557
}
682
683
static DecodeStatus decodeImm64n_4nOperand(MCInst *Inst, uint64_t Imm,
684
             int64_t Address, const void *Decoder)
685
202
{
686
202
  CS_ASSERT_RET_VAL(isUIntN(6, Imm) && ((Imm & 0x3) == 0) &&
687
202
          "Invalid immediate",
688
202
        MCDisassembler_Fail);
689
202
  MCOperand_CreateImm0(Inst, ((~0x3f) | (Imm)));
690
202
  return MCDisassembler_Success;
691
202
}
692
693
static DecodeStatus decodeOffset8m32Operand(MCInst *Inst, uint64_t Imm,
694
              int64_t Address,
695
              const void *Decoder)
696
857
{
697
857
  CS_ASSERT_RET_VAL(isUIntN(10, Imm) && ((Imm & 0x3) == 0) &&
698
857
          "Invalid immediate",
699
857
        MCDisassembler_Fail);
700
857
  MCOperand_CreateImm0(Inst, (Imm));
701
857
  return MCDisassembler_Success;
702
857
}
703
704
static DecodeStatus decodeEntry_Imm12OpValue(MCInst *Inst, uint64_t Imm,
705
               int64_t Address,
706
               const void *Decoder)
707
766
{
708
766
  CS_ASSERT_RET_VAL(isUIntN(15, Imm) && ((Imm & 0x7) == 0) &&
709
766
          "Invalid immediate",
710
766
        MCDisassembler_Fail);
711
766
  MCOperand_CreateImm0(Inst, (Imm));
712
766
  return MCDisassembler_Success;
713
766
}
714
715
static DecodeStatus decodeShimm1_31Operand(MCInst *Inst, uint64_t Imm,
716
             int64_t Address, const void *Decoder)
717
693
{
718
693
  CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate",
719
693
        MCDisassembler_Fail);
720
693
  MCOperand_CreateImm0(Inst, (32 - Imm));
721
693
  return MCDisassembler_Success;
722
693
}
723
724
//static DecodeStatus decodeShimm0_31Operand(MCInst *Inst, uint64_t Imm,
725
//             int64_t Address, const void *Decoder)
726
//{
727
//  CS_ASSERT_RET_VAL(isUIntN(5, Imm) && "Invalid immediate", MCDisassembler_Fail);
728
//  MCOperand_CreateImm0(Inst, (32 - Imm));
729
//  return MCDisassembler_Success;
730
//}
731
732
static DecodeStatus decodeImm7_22Operand(MCInst *Inst, uint64_t Imm,
733
           int64_t Address, const void *Decoder)
734
359
{
735
359
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
736
359
        MCDisassembler_Fail);
737
359
  MCOperand_CreateImm0(Inst, (Imm + 7));
738
359
  return MCDisassembler_Success;
739
359
}
740
741
static DecodeStatus decodeSelect_2Operand(MCInst *Inst, uint64_t Imm,
742
            int64_t Address, const void *Decoder)
743
1.62k
{
744
1.62k
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
745
1.62k
        MCDisassembler_Fail);
746
1.62k
  MCOperand_CreateImm0(Inst, (Imm));
747
1.62k
  return MCDisassembler_Success;
748
1.62k
}
749
750
static DecodeStatus decodeSelect_4Operand(MCInst *Inst, uint64_t Imm,
751
            int64_t Address, const void *Decoder)
752
2.23k
{
753
2.23k
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
754
2.23k
        MCDisassembler_Fail);
755
2.23k
  MCOperand_CreateImm0(Inst, (Imm));
756
2.23k
  return MCDisassembler_Success;
757
2.23k
}
758
759
static DecodeStatus decodeSelect_8Operand(MCInst *Inst, uint64_t Imm,
760
            int64_t Address, const void *Decoder)
761
1.79k
{
762
1.79k
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
763
1.79k
        MCDisassembler_Fail);
764
1.79k
  MCOperand_CreateImm0(Inst, (Imm));
765
1.79k
  return MCDisassembler_Success;
766
1.79k
}
767
768
static DecodeStatus decodeSelect_16Operand(MCInst *Inst, uint64_t Imm,
769
             int64_t Address, const void *Decoder)
770
482
{
771
482
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
772
482
        MCDisassembler_Fail);
773
482
  MCOperand_CreateImm0(Inst, (Imm));
774
482
  return MCDisassembler_Success;
775
482
}
776
777
static DecodeStatus decodeSelect_256Operand(MCInst *Inst, uint64_t Imm,
778
              int64_t Address,
779
              const void *Decoder)
780
95
{
781
95
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
782
95
        MCDisassembler_Fail);
783
95
  MCOperand_CreateImm0(Inst, (Imm));
784
95
  return MCDisassembler_Success;
785
95
}
786
787
static DecodeStatus decodeOffset_16_16Operand(MCInst *Inst, uint64_t Imm,
788
                int64_t Address,
789
                const void *Decoder)
790
781
{
791
781
  CS_ASSERT_RET_VAL(isIntN(8, Imm) && "Invalid immediate",
792
781
        MCDisassembler_Fail);
793
781
  if ((Imm & 0xf) != 0)
794
626
    MCOperand_CreateImm0(Inst, (Imm << 4));
795
155
  else
796
155
    MCOperand_CreateImm0(Inst, (Imm));
797
781
  return MCDisassembler_Success;
798
781
}
799
800
static DecodeStatus decodeOffset_256_8Operand(MCInst *Inst, uint64_t Imm,
801
                int64_t Address,
802
                const void *Decoder)
803
2.55k
{
804
2.55k
  CS_ASSERT_RET_VAL(isIntN(16, Imm) && "Invalid immediate",
805
2.55k
        MCDisassembler_Fail);
806
2.55k
  if ((Imm & 0x7) != 0)
807
2.02k
    MCOperand_CreateImm0(Inst, (Imm << 3));
808
530
  else
809
530
    MCOperand_CreateImm0(Inst, (Imm));
810
2.55k
  return MCDisassembler_Success;
811
2.55k
}
812
813
static DecodeStatus decodeOffset_256_16Operand(MCInst *Inst, uint64_t Imm,
814
                 int64_t Address,
815
                 const void *Decoder)
816
2.05k
{
817
2.05k
  CS_ASSERT_RET_VAL(isIntN(16, Imm) && "Invalid immediate",
818
2.05k
        MCDisassembler_Fail);
819
2.05k
  if ((Imm & 0xf) != 0)
820
1.81k
    MCOperand_CreateImm0(Inst, (Imm << 4));
821
236
  else
822
236
    MCOperand_CreateImm0(Inst, (Imm));
823
2.05k
  return MCDisassembler_Success;
824
2.05k
}
825
826
static DecodeStatus decodeOffset_256_4Operand(MCInst *Inst, uint64_t Imm,
827
                int64_t Address,
828
                const void *Decoder)
829
734
{
830
734
  CS_ASSERT_RET_VAL(isIntN(16, Imm) && "Invalid immediate",
831
734
        MCDisassembler_Fail);
832
734
  if ((Imm & 0x2) != 0)
833
447
    MCOperand_CreateImm0(Inst, (Imm << 2));
834
287
  else
835
287
    MCOperand_CreateImm0(Inst, (Imm));
836
734
  return MCDisassembler_Success;
837
734
}
838
839
static DecodeStatus decodeOffset_128_2Operand(MCInst *Inst, uint64_t Imm,
840
                int64_t Address,
841
                const void *Decoder)
842
988
{
843
988
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
844
988
        MCDisassembler_Fail);
845
988
  if ((Imm & 0x1) != 0)
846
579
    MCOperand_CreateImm0(Inst, (Imm << 1));
847
409
  else
848
409
    MCOperand_CreateImm0(Inst, (Imm));
849
988
  return MCDisassembler_Success;
850
988
}
851
852
static DecodeStatus decodeOffset_128_1Operand(MCInst *Inst, uint64_t Imm,
853
                int64_t Address,
854
                const void *Decoder)
855
99
{
856
99
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
857
99
        MCDisassembler_Fail);
858
99
  MCOperand_CreateImm0(Inst, (Imm));
859
99
  return MCDisassembler_Success;
860
99
}
861
862
static DecodeStatus decodeOffset_64_16Operand(MCInst *Inst, uint64_t Imm,
863
                int64_t Address,
864
                const void *Decoder)
865
4.57k
{
866
4.57k
  CS_ASSERT_RET_VAL(isIntN(16, Imm) && "Invalid immediate",
867
4.57k
        MCDisassembler_Fail);
868
4.57k
  if ((Imm & 0xf) != 0)
869
3.72k
    MCOperand_CreateImm0(Inst, (Imm << 4));
870
849
  else
871
849
    MCOperand_CreateImm0(Inst, (Imm));
872
4.57k
  return MCDisassembler_Success;
873
4.57k
}
874
875
static int64_t TableB4const[16] = { -1, 1,  2,  3,  4,  5,  6,   7,
876
            8,  10, 12, 16, 32, 64, 128, 256 };
877
static DecodeStatus decodeB4constOperand(MCInst *Inst, uint64_t Imm,
878
           int64_t Address, const void *Decoder)
879
6.18k
{
880
6.18k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
881
6.18k
        MCDisassembler_Fail);
882
883
6.18k
  MCOperand_CreateImm0(Inst, (TableB4const[Imm]));
884
6.18k
  return MCDisassembler_Success;
885
6.18k
}
886
887
static int64_t TableB4constu[16] = { 32768, 65536, 2,  3,  4,  5,  6, 7,
888
             8,     10,    12, 16, 32, 64, 128, 256 };
889
static DecodeStatus decodeB4constuOperand(MCInst *Inst, uint64_t Imm,
890
            int64_t Address, const void *Decoder)
891
9.17k
{
892
9.17k
  CS_ASSERT_RET_VAL(isUIntN(4, Imm) && "Invalid immediate",
893
9.17k
        MCDisassembler_Fail);
894
895
9.17k
  MCOperand_CreateImm0(Inst, (TableB4constu[Imm]));
896
9.17k
  return MCDisassembler_Success;
897
9.17k
}
898
899
static DecodeStatus decodeMem8Operand(MCInst *Inst, uint64_t Imm,
900
              int64_t Address, const void *Decoder)
901
1.46k
{
902
1.46k
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
903
1.46k
        MCDisassembler_Fail);
904
1.46k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
905
1.46k
  MCOperand_CreateImm0(Inst, ((Imm >> 4) & 0xff));
906
1.46k
  return MCDisassembler_Success;
907
1.46k
}
908
909
static DecodeStatus decodeMem16Operand(MCInst *Inst, uint64_t Imm,
910
               int64_t Address, const void *Decoder)
911
532
{
912
532
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
913
532
        MCDisassembler_Fail);
914
532
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
915
532
  MCOperand_CreateImm0(Inst, ((Imm >> 3) & 0x1fe));
916
532
  return MCDisassembler_Success;
917
532
}
918
919
static DecodeStatus decodeMem32Operand(MCInst *Inst, uint64_t Imm,
920
               int64_t Address, const void *Decoder)
921
2.13k
{
922
2.13k
  CS_ASSERT_RET_VAL(isUIntN(12, Imm) && "Invalid immediate",
923
2.13k
        MCDisassembler_Fail);
924
2.13k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
925
2.13k
  MCOperand_CreateImm0(Inst, ((Imm >> 2) & 0x3fc));
926
2.13k
  return MCDisassembler_Success;
927
2.13k
}
928
929
static DecodeStatus decodeMem32nOperand(MCInst *Inst, uint64_t Imm,
930
          int64_t Address, const void *Decoder)
931
11.3k
{
932
11.3k
  CS_ASSERT_RET_VAL(isUIntN(8, Imm) && "Invalid immediate",
933
11.3k
        MCDisassembler_Fail);
934
11.3k
  DecodeARRegisterClass(Inst, Imm & 0xf, Address, Decoder);
935
11.3k
  MCOperand_CreateImm0(Inst, ((Imm >> 2) & 0x3c));
936
11.3k
  return MCDisassembler_Success;
937
11.3k
}
938
939
/// Read two bytes from the ArrayRef and return 16 bit data sorted
940
/// according to the given endianness.
941
static DecodeStatus readInstruction16(MCInst *MI, const uint8_t *Bytes,
942
              size_t BytesLen, uint64_t Address,
943
              uint64_t *Size, uint64_t *Insn,
944
              bool IsLittleEndian)
945
116k
{
946
  // We want to read exactly 2 Bytes of data.
947
116k
  if (BytesLen < 2) {
948
346
    *Size = 0;
949
346
    return MCDisassembler_Fail;
950
346
  }
951
952
115k
  *Insn = readBytes16(MI, Bytes);
953
115k
  *Size = 2;
954
955
115k
  return MCDisassembler_Success;
956
116k
}
957
958
/// Read three bytes from the ArrayRef and return 24 bit data
959
static DecodeStatus readInstruction24(MCInst *MI, const uint8_t *Bytes,
960
              size_t BytesLen, uint64_t Address,
961
              uint64_t *Size, uint64_t *Insn,
962
              bool IsLittleEndian, bool CheckTIE)
963
116k
{
964
  // We want to read exactly 3 Bytes of data.
965
116k
  if (BytesLen < 3) {
966
212
    *Size = 0;
967
212
    return MCDisassembler_Fail;
968
212
  }
969
970
116k
  if (CheckTIE && (Bytes[0] & 0x8) != 0)
971
10.6k
    return MCDisassembler_Fail;
972
105k
  *Insn = readBytes24(MI, Bytes);
973
105k
  *Size = 3;
974
975
105k
  return MCDisassembler_Success;
976
116k
}
977
978
/// Read three bytes from the ArrayRef and return 32 bit data
979
static DecodeStatus readInstruction32(MCInst *MI, const uint8_t *Bytes,
980
              size_t BytesLen, uint64_t Address,
981
              uint64_t *Size, uint64_t *Insn,
982
              bool IsLittleEndian)
983
11.0k
{
984
  // We want to read exactly 4 Bytes of data.
985
11.0k
  if (BytesLen < 4) {
986
84
    *Size = 0;
987
84
    return MCDisassembler_Fail;
988
84
  }
989
990
10.9k
  if ((Bytes[0] & 0x8) == 0)
991
353
    return MCDisassembler_Fail;
992
10.6k
  *Insn = readBytes32(MI, Bytes);
993
10.6k
  *Size = 4;
994
995
10.6k
  return MCDisassembler_Success;
996
10.9k
}
997
998
/// Read InstSize bytes from the ArrayRef and return 24 bit data
999
/// InstSize cannot be larger than 8.
1000
static DecodeStatus readInstructionN(const uint8_t *Bytes, size_t BytesLen,
1001
             uint64_t Address, unsigned InstSize,
1002
             uint64_t *Size, uint64_t *Insn,
1003
             bool IsLittleEndian)
1004
156
{
1005
  // We want to read exactly 3 Bytes of data.
1006
156
  if (BytesLen < InstSize) {
1007
19
    *Size = 0;
1008
19
    return MCDisassembler_Fail;
1009
19
  }
1010
137
  if (InstSize > 8) {
1011
0
    InstSize = 8;
1012
0
  }
1013
1014
137
  *Insn = 0;
1015
959
  for (unsigned i = 0; i < InstSize; i++)
1016
822
    *Insn |= (uint64_t)(Bytes[i]) << (8 * i);
1017
1018
137
  *Size = InstSize;
1019
137
  return MCDisassembler_Success;
1020
156
}
1021
1022
#include "XtensaGenDisassemblerTables.inc"
1023
1024
FieldFromInstruction(fieldFromInstruction_2, uint64_t);
1025
24.7k
DecodeToMCInst(decodeToMCInst_2, fieldFromInstruction_2, uint64_t);
1026
115k
DecodeInstruction(decodeInstruction_2, fieldFromInstruction_2, decodeToMCInst_2,
1027
      uint64_t);
1028
1029
FieldFromInstruction(fieldFromInstruction_4, uint64_t);
1030
10.4k
DecodeToMCInst(decodeToMCInst_4, fieldFromInstruction_4, uint64_t);
1031
10.6k
DecodeInstruction(decodeInstruction_4, fieldFromInstruction_4, decodeToMCInst_4,
1032
      uint64_t);
1033
1034
FieldFromInstruction(fieldFromInstruction_6, uint64_t);
1035
0
DecodeToMCInst(decodeToMCInst_6, fieldFromInstruction_6, uint64_t);
1036
137
DecodeInstruction(decodeInstruction_6, fieldFromInstruction_6, decodeToMCInst_6,
1037
      uint64_t);
1038
1039
static bool hasDensity()
1040
116k
{
1041
116k
  return true;
1042
116k
}
1043
static bool hasESP32S3Ops()
1044
25.3k
{
1045
25.3k
  return true;
1046
25.3k
}
1047
static bool hasHIFI3()
1048
156
{
1049
156
  return true;
1050
156
}
1051
1052
static DecodeStatus getInstruction(MCInst *MI, uint64_t *Size,
1053
           const uint8_t *Bytes, size_t BytesLen,
1054
           uint64_t Address)
1055
116k
{
1056
116k
  uint64_t Insn;
1057
116k
  DecodeStatus Result;
1058
116k
  bool IsLittleEndian = MI->csh->mode & CS_MODE_LITTLE_ENDIAN;
1059
1060
  // Parse 16-bit instructions
1061
116k
  if (hasDensity()) {
1062
116k
    Result = readInstruction16(MI, Bytes, BytesLen, Address, Size,
1063
116k
             &Insn, IsLittleEndian);
1064
116k
    if (Result == MCDisassembler_Fail)
1065
346
      return MCDisassembler_Fail;
1066
1067
115k
    Result = decodeInstruction_2(DecoderTable16, MI, Insn, Address,
1068
115k
               NULL);
1069
115k
    if (Result != MCDisassembler_Fail) {
1070
24.7k
      *Size = 2;
1071
24.7k
      return Result;
1072
24.7k
    }
1073
115k
  }
1074
1075
  // Parse Core 24-bit instructions
1076
91.2k
  Result = readInstruction24(MI, Bytes, BytesLen, Address, Size, &Insn,
1077
91.2k
           IsLittleEndian, false);
1078
91.2k
  if (Result == MCDisassembler_Fail)
1079
212
    return MCDisassembler_Fail;
1080
1081
91.0k
  Result = decodeInstruction_3(DecoderTable24, MI, Insn, Address, NULL);
1082
91.0k
  if (Result != MCDisassembler_Fail) {
1083
65.6k
    *Size = 3;
1084
65.6k
    return Result;
1085
65.6k
  }
1086
1087
25.3k
  if (hasESP32S3Ops()) {
1088
    // Parse ESP32S3 24-bit instructions
1089
25.3k
    Result = readInstruction24(MI, Bytes, BytesLen, Address, Size,
1090
25.3k
             &Insn, IsLittleEndian, true);
1091
25.3k
    if (Result != MCDisassembler_Fail) {
1092
14.7k
      Result = decodeInstruction_3(DecoderTableESP32S324, MI,
1093
14.7k
                 Insn, Address, NULL);
1094
14.7k
      if (Result != MCDisassembler_Fail) {
1095
14.3k
        *Size = 3;
1096
14.3k
        return Result;
1097
14.3k
      }
1098
14.7k
    }
1099
1100
    // Parse ESP32S3 32-bit instructions
1101
11.0k
    Result = readInstruction32(MI, Bytes, BytesLen, Address, Size,
1102
11.0k
             &Insn, IsLittleEndian);
1103
11.0k
    if (Result == MCDisassembler_Fail)
1104
437
      return MCDisassembler_Fail;
1105
1106
10.6k
    Result = decodeInstruction_4(DecoderTableESP32S332, MI, Insn,
1107
10.6k
               Address, NULL);
1108
10.6k
    if (Result != MCDisassembler_Fail) {
1109
10.4k
      *Size = 4;
1110
10.4k
      return Result;
1111
10.4k
    }
1112
10.6k
  }
1113
1114
156
  if (hasHIFI3()) {
1115
156
    Result = decodeInstruction_3(DecoderTableHIFI324, MI, Insn,
1116
156
               Address, NULL);
1117
156
    if (Result != MCDisassembler_Fail)
1118
0
      return Result;
1119
1120
156
    Result = readInstructionN(Bytes, BytesLen, Address, 6, Size,
1121
156
            &Insn, IsLittleEndian);
1122
156
    if (Result == MCDisassembler_Fail)
1123
19
      return MCDisassembler_Fail;
1124
1125
137
    Result = decodeInstruction_6(DecoderTableHIFI348, MI, Insn,
1126
137
               Address, NULL);
1127
137
    if (Result != MCDisassembler_Fail)
1128
0
      return Result;
1129
137
  }
1130
137
  return Result;
1131
156
}
1132
1133
DecodeStatus Xtensa_LLVM_getInstruction(MCInst *MI, uint16_t *size16,
1134
          const uint8_t *Bytes,
1135
          unsigned BytesSize, uint64_t Address)
1136
116k
{
1137
116k
  uint64_t size64;
1138
116k
  DecodeStatus status =
1139
116k
    getInstruction(MI, &size64, Bytes, BytesSize, Address);
1140
116k
  CS_ASSERT_RET_VAL(size64 < 0xffff, MCDisassembler_Fail);
1141
116k
  *size16 = size64;
1142
116k
  return status;
1143
116k
}