Coverage Report

Created: 2025-12-05 06:11

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