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