Coverage Report

Created: 2026-04-29 06:06

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