VirtualBox

source: vbox/trunk/src/VBox/Disassembler/DisasmFormatYasm.cpp@ 96215

Last change on this file since 96215 was 95479, checked in by vboxsync, 2 years ago

DIS: Fixes for PMOVMSKB and VPMOVMSKB. bugref:9898 bugref:6251

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 70.5 KB
Line 
1/* $Id: DisasmFormatYasm.cpp 95479 2022-07-01 21:07:44Z vboxsync $ */
2/** @file
3 * VBox Disassembler - Yasm(/Nasm) Style Formatter.
4 */
5
6/*
7 * Copyright (C) 2008-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/dis.h>
23#include "DisasmInternal.h"
24#include <iprt/assert.h>
25#include <iprt/ctype.h>
26#include <iprt/err.h>
27#include <iprt/string.h>
28
29
30/*********************************************************************************************************************************
31* Global Variables *
32*********************************************************************************************************************************/
33static const char g_szSpaces[] =
34" ";
35static const char g_aszYasmRegGen8[20][5] =
36{
37 "al\0\0", "cl\0\0", "dl\0\0", "bl\0\0", "ah\0\0", "ch\0\0", "dh\0\0", "bh\0\0", "r8b\0", "r9b\0", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b", "spl\0", "bpl\0", "sil\0", "dil\0"
38};
39static const char g_aszYasmRegGen16[16][5] =
40{
41 "ax\0\0", "cx\0\0", "dx\0\0", "bx\0\0", "sp\0\0", "bp\0\0", "si\0\0", "di\0\0", "r8w\0", "r9w\0", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
42};
43#if 0 /* unused */
44static const char g_aszYasmRegGen1616[8][6] =
45{
46 "bx+si", "bx+di", "bp+si", "bp+di", "si\0\0\0", "di\0\0\0", "bp\0\0\0", "bx\0\0\0"
47};
48#endif
49static const char g_aszYasmRegGen32[16][5] =
50{
51 "eax\0", "ecx\0", "edx\0", "ebx\0", "esp\0", "ebp\0", "esi\0", "edi\0", "r8d\0", "r9d\0", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d"
52};
53static const char g_aszYasmRegGen64[16][4] =
54{
55 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8\0", "r9\0", "r10", "r11", "r12", "r13", "r14", "r15"
56};
57static const char g_aszYasmRegSeg[6][3] =
58{
59 "es", "cs", "ss", "ds", "fs", "gs"
60};
61static const char g_aszYasmRegFP[8][4] =
62{
63 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7"
64};
65static const char g_aszYasmRegMMX[8][4] =
66{
67 "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7"
68};
69static const char g_aszYasmRegXMM[16][6] =
70{
71 "xmm0\0", "xmm1\0", "xmm2\0", "xmm3\0", "xmm4\0", "xmm5\0", "xmm6\0", "xmm7\0", "xmm8\0", "xmm9\0", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"
72};
73static const char g_aszYasmRegYMM[16][6] =
74{
75 "ymm0\0", "ymm1\0", "ymm2\0", "ymm3\0", "ymm4\0", "ymm5\0", "ymm6\0", "ymm7\0", "ymm8\0", "ymm9\0", "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15"
76};
77static const char g_aszYasmRegCRx[16][5] =
78{
79 "cr0\0", "cr1\0", "cr2\0", "cr3\0", "cr4\0", "cr5\0", "cr6\0", "cr7\0", "cr8\0", "cr9\0", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15"
80};
81static const char g_aszYasmRegDRx[16][5] =
82{
83 "dr0\0", "dr1\0", "dr2\0", "dr3\0", "dr4\0", "dr5\0", "dr6\0", "dr7\0", "dr8\0", "dr9\0", "dr10", "dr11", "dr12", "dr13", "dr14", "dr15"
84};
85static const char g_aszYasmRegTRx[16][5] =
86{
87 "tr0\0", "tr1\0", "tr2\0", "tr3\0", "tr4\0", "tr5\0", "tr6\0", "tr7\0", "tr8\0", "tr9\0", "tr10", "tr11", "tr12", "tr13", "tr14", "tr15"
88};
89
90
91
92/**
93 * Gets the base register name for the given parameter.
94 *
95 * @returns Pointer to the register name.
96 * @param pDis The disassembler state.
97 * @param pParam The parameter.
98 * @param pcchReg Where to store the length of the name.
99 */
100static const char *disasmFormatYasmBaseReg(PCDISSTATE pDis, PCDISOPPARAM pParam, size_t *pcchReg)
101{
102 RT_NOREF_PV(pDis);
103
104 switch (pParam->fUse & ( DISUSE_REG_GEN8 | DISUSE_REG_GEN16 | DISUSE_REG_GEN32 | DISUSE_REG_GEN64
105 | DISUSE_REG_FP | DISUSE_REG_MMX | DISUSE_REG_XMM | DISUSE_REG_YMM
106 | DISUSE_REG_CR | DISUSE_REG_DBG | DISUSE_REG_SEG | DISUSE_REG_TEST))
107
108 {
109 case DISUSE_REG_GEN8:
110 {
111 Assert(pParam->Base.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen8));
112 const char *psz = g_aszYasmRegGen8[pParam->Base.idxGenReg];
113 *pcchReg = 2 + !!psz[2] + !!psz[3];
114 return psz;
115 }
116
117 case DISUSE_REG_GEN16:
118 {
119 Assert(pParam->Base.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen16));
120 const char *psz = g_aszYasmRegGen16[pParam->Base.idxGenReg];
121 *pcchReg = 2 + !!psz[2] + !!psz[3];
122 return psz;
123 }
124
125 // VSIB
126 case DISUSE_REG_XMM | DISUSE_REG_GEN32:
127 case DISUSE_REG_YMM | DISUSE_REG_GEN32:
128 case DISUSE_REG_GEN32:
129 {
130 Assert(pParam->Base.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen32));
131 const char *psz = g_aszYasmRegGen32[pParam->Base.idxGenReg];
132 *pcchReg = 2 + !!psz[2] + !!psz[3];
133 return psz;
134 }
135
136 // VSIB
137 case DISUSE_REG_XMM | DISUSE_REG_GEN64:
138 case DISUSE_REG_YMM | DISUSE_REG_GEN64:
139 case DISUSE_REG_GEN64:
140 {
141 Assert(pParam->Base.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen64));
142 const char *psz = g_aszYasmRegGen64[pParam->Base.idxGenReg];
143 *pcchReg = 2 + !!psz[2] + !!psz[3];
144 return psz;
145 }
146
147 case DISUSE_REG_FP:
148 {
149 Assert(pParam->Base.idxFpuReg < RT_ELEMENTS(g_aszYasmRegFP));
150 const char *psz = g_aszYasmRegFP[pParam->Base.idxFpuReg];
151 *pcchReg = 3;
152 return psz;
153 }
154
155 case DISUSE_REG_MMX:
156 {
157 Assert(pParam->Base.idxMmxReg < RT_ELEMENTS(g_aszYasmRegMMX));
158 const char *psz = g_aszYasmRegMMX[pParam->Base.idxMmxReg];
159 *pcchReg = 3;
160 return psz;
161 }
162
163 case DISUSE_REG_XMM:
164 {
165 Assert(pParam->Base.idxXmmReg < RT_ELEMENTS(g_aszYasmRegXMM));
166 const char *psz = g_aszYasmRegXMM[pParam->Base.idxXmmReg];
167 *pcchReg = 4 + !!psz[4];
168 return psz;
169 }
170
171 case DISUSE_REG_YMM:
172 {
173 Assert(pParam->Base.idxYmmReg < RT_ELEMENTS(g_aszYasmRegYMM));
174 const char *psz = g_aszYasmRegYMM[pParam->Base.idxYmmReg];
175 *pcchReg = 4 + !!psz[4];
176 return psz;
177 }
178
179 case DISUSE_REG_CR:
180 {
181 Assert(pParam->Base.idxCtrlReg < RT_ELEMENTS(g_aszYasmRegCRx));
182 const char *psz = g_aszYasmRegCRx[pParam->Base.idxCtrlReg];
183 *pcchReg = 3;
184 return psz;
185 }
186
187 case DISUSE_REG_DBG:
188 {
189 Assert(pParam->Base.idxDbgReg < RT_ELEMENTS(g_aszYasmRegDRx));
190 const char *psz = g_aszYasmRegDRx[pParam->Base.idxDbgReg];
191 *pcchReg = 3;
192 return psz;
193 }
194
195 case DISUSE_REG_SEG:
196 {
197 Assert(pParam->Base.idxSegReg < RT_ELEMENTS(g_aszYasmRegCRx));
198 const char *psz = g_aszYasmRegSeg[pParam->Base.idxSegReg];
199 *pcchReg = 2;
200 return psz;
201 }
202
203 case DISUSE_REG_TEST:
204 {
205 Assert(pParam->Base.idxTestReg < RT_ELEMENTS(g_aszYasmRegTRx));
206 const char *psz = g_aszYasmRegTRx[pParam->Base.idxTestReg];
207 *pcchReg = 3;
208 return psz;
209 }
210
211 default:
212 AssertMsgFailed(("%#x\n", pParam->fUse));
213 *pcchReg = 3;
214 return "r??";
215 }
216}
217
218
219/**
220 * Gets the index register name for the given parameter.
221 *
222 * @returns The index register name.
223 * @param pDis The disassembler state.
224 * @param pParam The parameter.
225 * @param pcchReg Where to store the length of the name.
226 */
227static const char *disasmFormatYasmIndexReg(PCDISSTATE pDis, PCDISOPPARAM pParam, size_t *pcchReg)
228{
229 if (pParam->fUse & DISUSE_REG_XMM)
230 {
231 Assert(pParam->Index.idxXmmReg < RT_ELEMENTS(g_aszYasmRegXMM));
232 const char *psz = g_aszYasmRegXMM[pParam->Index.idxXmmReg];
233 *pcchReg = 4 + !!psz[4];
234 return psz;
235 }
236 else if (pParam->fUse & DISUSE_REG_YMM)
237 {
238 Assert(pParam->Index.idxYmmReg < RT_ELEMENTS(g_aszYasmRegYMM));
239 const char *psz = g_aszYasmRegYMM[pParam->Index.idxYmmReg];
240 *pcchReg = 4 + !!psz[4];
241 return psz;
242
243 }
244 else
245 switch (pDis->uAddrMode)
246 {
247 case DISCPUMODE_16BIT:
248 {
249 Assert(pParam->Index.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen16));
250 const char *psz = g_aszYasmRegGen16[pParam->Index.idxGenReg];
251 *pcchReg = 2 + !!psz[2] + !!psz[3];
252 return psz;
253 }
254
255 case DISCPUMODE_32BIT:
256 {
257 Assert(pParam->Index.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen32));
258 const char *psz = g_aszYasmRegGen32[pParam->Index.idxGenReg];
259 *pcchReg = 2 + !!psz[2] + !!psz[3];
260 return psz;
261 }
262
263 case DISCPUMODE_64BIT:
264 {
265 Assert(pParam->Index.idxGenReg < RT_ELEMENTS(g_aszYasmRegGen64));
266 const char *psz = g_aszYasmRegGen64[pParam->Index.idxGenReg];
267 *pcchReg = 2 + !!psz[2] + !!psz[3];
268 return psz;
269 }
270
271 default:
272 AssertMsgFailed(("%#x %#x\n", pParam->fUse, pDis->uAddrMode));
273 *pcchReg = 3;
274 return "r??";
275 }
276}
277
278
279/**
280 * Formats the current instruction in Yasm (/ Nasm) style.
281 *
282 *
283 * @returns The number of output characters. If this is >= cchBuf, then the content
284 * of pszBuf will be truncated.
285 * @param pDis Pointer to the disassembler state.
286 * @param pszBuf The output buffer.
287 * @param cchBuf The size of the output buffer.
288 * @param fFlags Format flags, see DIS_FORMAT_FLAGS_*.
289 * @param pfnGetSymbol Get symbol name for a jmp or call target address. Optional.
290 * @param pvUser User argument for pfnGetSymbol.
291 */
292DISDECL(size_t) DISFormatYasmEx(PCDISSTATE pDis, char *pszBuf, size_t cchBuf, uint32_t fFlags,
293 PFNDISGETSYMBOL pfnGetSymbol, void *pvUser)
294{
295/** @todo monitor and mwait aren't formatted correctly in 64-bit mode. */
296 /*
297 * Input validation and massaging.
298 */
299 AssertPtr(pDis);
300 AssertPtrNull(pszBuf);
301 Assert(pszBuf || !cchBuf);
302 AssertPtrNull(pfnGetSymbol);
303 AssertMsg(DIS_FMT_FLAGS_IS_VALID(fFlags), ("%#x\n", fFlags));
304 if (fFlags & DIS_FMT_FLAGS_ADDR_COMMENT)
305 fFlags = (fFlags & ~DIS_FMT_FLAGS_ADDR_LEFT) | DIS_FMT_FLAGS_ADDR_RIGHT;
306 if (fFlags & DIS_FMT_FLAGS_BYTES_COMMENT)
307 fFlags = (fFlags & ~DIS_FMT_FLAGS_BYTES_LEFT) | DIS_FMT_FLAGS_BYTES_RIGHT;
308
309 PCDISOPCODE const pOp = pDis->pCurInstr;
310
311 /*
312 * Output macros
313 */
314 char *pszDst = pszBuf;
315 size_t cchDst = cchBuf;
316 size_t cchOutput = 0;
317#define PUT_C(ch) \
318 do { \
319 cchOutput++; \
320 if (cchDst > 1) \
321 { \
322 cchDst--; \
323 *pszDst++ = (ch); \
324 } \
325 } while (0)
326#define PUT_STR(pszSrc, cchSrc) \
327 do { \
328 cchOutput += (cchSrc); \
329 if (cchDst > (cchSrc)) \
330 { \
331 memcpy(pszDst, (pszSrc), (cchSrc)); \
332 pszDst += (cchSrc); \
333 cchDst -= (cchSrc); \
334 } \
335 else if (cchDst > 1) \
336 { \
337 memcpy(pszDst, (pszSrc), cchDst - 1); \
338 pszDst += cchDst - 1; \
339 cchDst = 1; \
340 } \
341 } while (0)
342#define PUT_SZ(sz) \
343 PUT_STR((sz), sizeof(sz) - 1)
344#define PUT_SZ_STRICT(szStrict, szRelaxed) \
345 do { if (fFlags & DIS_FMT_FLAGS_STRICT) PUT_SZ(szStrict); else PUT_SZ(szRelaxed); } while (0)
346#define PUT_PSZ(psz) \
347 do { const size_t cchTmp = strlen(psz); PUT_STR((psz), cchTmp); } while (0)
348#define PUT_NUM(cch, fmt, num) \
349 do { \
350 cchOutput += (cch); \
351 if (cchDst > 1) \
352 { \
353 const size_t cchTmp = RTStrPrintf(pszDst, cchDst, fmt, (num)); \
354 pszDst += cchTmp; \
355 cchDst -= cchTmp; \
356 Assert(cchTmp == (cch) || cchDst == 1); \
357 } \
358 } while (0)
359/** @todo add two flags for choosing between %X / %x and h / 0x. */
360#define PUT_NUM_8(num) PUT_NUM(4, "0%02xh", (uint8_t)(num))
361#define PUT_NUM_16(num) PUT_NUM(6, "0%04xh", (uint16_t)(num))
362#define PUT_NUM_32(num) PUT_NUM(10, "0%08xh", (uint32_t)(num))
363#define PUT_NUM_64(num) PUT_NUM(18, "0%016RX64h", (uint64_t)(num))
364
365#define PUT_NUM_SIGN(cch, fmt, num, stype, utype) \
366 do { \
367 if ((stype)(num) >= 0) \
368 { \
369 PUT_C('+'); \
370 PUT_NUM(cch, fmt, (utype)(num)); \
371 } \
372 else \
373 { \
374 PUT_C('-'); \
375 PUT_NUM(cch, fmt, (utype)-(stype)(num)); \
376 } \
377 } while (0)
378#define PUT_NUM_S8(num) PUT_NUM_SIGN(4, "0%02xh", num, int8_t, uint8_t)
379#define PUT_NUM_S16(num) PUT_NUM_SIGN(6, "0%04xh", num, int16_t, uint16_t)
380#define PUT_NUM_S32(num) PUT_NUM_SIGN(10, "0%08xh", num, int32_t, uint32_t)
381#define PUT_NUM_S64(num) PUT_NUM_SIGN(18, "0%016RX64h", num, int64_t, uint64_t)
382
383#define PUT_SYMBOL_TWO(a_rcSym, a_szStart, a_chEnd) \
384 do { \
385 if (RT_SUCCESS(a_rcSym)) \
386 { \
387 PUT_SZ(a_szStart); \
388 PUT_PSZ(szSymbol); \
389 if (off != 0) \
390 { \
391 if ((int8_t)off == off) \
392 PUT_NUM_S8(off); \
393 else if ((int16_t)off == off) \
394 PUT_NUM_S16(off); \
395 else if ((int32_t)off == off) \
396 PUT_NUM_S32(off); \
397 else \
398 PUT_NUM_S64(off); \
399 } \
400 PUT_C(a_chEnd); \
401 } \
402 } while (0)
403
404#define PUT_SYMBOL(a_uSeg, a_uAddr, a_szStart, a_chEnd) \
405 do { \
406 if (pfnGetSymbol) \
407 { \
408 int rcSym = pfnGetSymbol(pDis, a_uSeg, a_uAddr, szSymbol, sizeof(szSymbol), &off, pvUser); \
409 PUT_SYMBOL_TWO(rcSym, a_szStart, a_chEnd); \
410 } \
411 } while (0)
412
413
414 /*
415 * The address?
416 */
417 if (fFlags & DIS_FMT_FLAGS_ADDR_LEFT)
418 {
419#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
420 if (pDis->uInstrAddr >= _4G)
421 PUT_NUM(9, "%08x`", (uint32_t)(pDis->uInstrAddr >> 32));
422#endif
423 PUT_NUM(8, "%08x", (uint32_t)pDis->uInstrAddr);
424 PUT_C(' ');
425 }
426
427 /*
428 * The opcode bytes?
429 */
430 if (fFlags & DIS_FMT_FLAGS_BYTES_LEFT)
431 {
432 size_t cchTmp = disFormatBytes(pDis, pszDst, cchDst, fFlags);
433 cchOutput += cchTmp;
434 if (cchDst > 1)
435 {
436 if (cchTmp <= cchDst)
437 {
438 cchDst -= cchTmp;
439 pszDst += cchTmp;
440 }
441 else
442 {
443 pszDst += cchDst - 1;
444 cchDst = 1;
445 }
446 }
447
448 /* Some padding to align the instruction. */
449 size_t cchPadding = (7 * (2 + !!(fFlags & DIS_FMT_FLAGS_BYTES_SPACED)))
450 + !!(fFlags & DIS_FMT_FLAGS_BYTES_BRACKETS) * 2
451 + 2;
452 cchPadding = cchTmp + 1 >= cchPadding ? 1 : cchPadding - cchTmp;
453 PUT_STR(g_szSpaces, cchPadding);
454 }
455
456
457 /*
458 * Filter out invalid opcodes first as they need special
459 * treatment. UD2 is an exception and should be handled normally.
460 */
461 size_t const offInstruction = cchOutput;
462 if ( pOp->uOpcode == OP_INVALID
463 || ( pOp->uOpcode == OP_ILLUD2
464 && (pDis->fPrefix & DISPREFIX_LOCK)))
465 PUT_SZ("Illegal opcode");
466 else
467 {
468 /*
469 * Prefixes
470 */
471 if (pDis->fPrefix & DISPREFIX_LOCK)
472 PUT_SZ("lock ");
473 if (pDis->fPrefix & DISPREFIX_REP)
474 PUT_SZ("rep ");
475 else if(pDis->fPrefix & DISPREFIX_REPNE)
476 PUT_SZ("repne ");
477
478 /*
479 * Adjust the format string to the correct mnemonic
480 * or to avoid things the assembler cannot handle correctly.
481 */
482 char szTmpFmt[48];
483 const char *pszFmt = pOp->pszOpcode;
484 bool fIgnoresOpSize = false;
485 bool fMayNeedAddrSize = false;
486 switch (pOp->uOpcode)
487 {
488 case OP_JECXZ:
489 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "jcxz %Jb" : pDis->uOpMode == DISCPUMODE_32BIT ? "jecxz %Jb" : "jrcxz %Jb";
490 break;
491 case OP_PUSHF:
492 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "pushfw" : pDis->uOpMode == DISCPUMODE_32BIT ? "pushfd" : "pushfq";
493 break;
494 case OP_POPF:
495 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "popfw" : pDis->uOpMode == DISCPUMODE_32BIT ? "popfd" : "popfq";
496 break;
497 case OP_PUSHA:
498 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "pushaw" : "pushad";
499 break;
500 case OP_POPA:
501 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "popaw" : "popad";
502 break;
503 case OP_INSB:
504 pszFmt = "insb";
505 fIgnoresOpSize = fMayNeedAddrSize = true;
506 break;
507 case OP_INSWD:
508 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "insw" : pDis->uOpMode == DISCPUMODE_32BIT ? "insd" : "insq";
509 fMayNeedAddrSize = true;
510 break;
511 case OP_OUTSB:
512 pszFmt = "outsb";
513 fIgnoresOpSize = fMayNeedAddrSize = true;
514 break;
515 case OP_OUTSWD:
516 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "outsw" : pDis->uOpMode == DISCPUMODE_32BIT ? "outsd" : "outsq";
517 fMayNeedAddrSize = true;
518 break;
519 case OP_MOVSB:
520 pszFmt = "movsb";
521 fIgnoresOpSize = fMayNeedAddrSize = true;
522 break;
523 case OP_MOVSWD:
524 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "movsw" : pDis->uOpMode == DISCPUMODE_32BIT ? "movsd" : "movsq";
525 fMayNeedAddrSize = true;
526 break;
527 case OP_CMPSB:
528 pszFmt = "cmpsb";
529 fIgnoresOpSize = fMayNeedAddrSize = true;
530 break;
531 case OP_CMPWD:
532 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "cmpsw" : pDis->uOpMode == DISCPUMODE_32BIT ? "cmpsd" : "cmpsq";
533 fMayNeedAddrSize = true;
534 break;
535 case OP_SCASB:
536 pszFmt = "scasb";
537 fIgnoresOpSize = fMayNeedAddrSize = true;
538 break;
539 case OP_SCASWD:
540 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "scasw" : pDis->uOpMode == DISCPUMODE_32BIT ? "scasd" : "scasq";
541 fMayNeedAddrSize = true;
542 break;
543 case OP_LODSB:
544 pszFmt = "lodsb";
545 fIgnoresOpSize = fMayNeedAddrSize = true;
546 break;
547 case OP_LODSWD:
548 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "lodsw" : pDis->uOpMode == DISCPUMODE_32BIT ? "lodsd" : "lodsq";
549 fMayNeedAddrSize = true;
550 break;
551 case OP_STOSB:
552 pszFmt = "stosb";
553 fIgnoresOpSize = fMayNeedAddrSize = true;
554 break;
555 case OP_STOSWD:
556 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "stosw" : pDis->uOpMode == DISCPUMODE_32BIT ? "stosd" : "stosq";
557 fMayNeedAddrSize = true;
558 break;
559 case OP_CBW:
560 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "cbw" : pDis->uOpMode == DISCPUMODE_32BIT ? "cwde" : "cdqe";
561 break;
562 case OP_CWD:
563 pszFmt = pDis->uOpMode == DISCPUMODE_16BIT ? "cwd" : pDis->uOpMode == DISCPUMODE_32BIT ? "cdq" : "cqo";
564 break;
565 case OP_SHL:
566 Assert(pszFmt[3] == '/');
567 pszFmt += 4;
568 break;
569 case OP_XLAT:
570 pszFmt = "xlatb";
571 break;
572 case OP_INT3:
573 pszFmt = "int3";
574 break;
575
576 /*
577 * Don't know how to tell yasm to generate complicated nop stuff, so 'db' it.
578 */
579 case OP_NOP:
580 if (pDis->bOpCode == 0x90)
581 /* fine, fine */;
582 else if (pszFmt[sizeof("nop %Ev") - 1] == '/' && pszFmt[sizeof("nop %Ev")] == 'p')
583 pszFmt = "prefetch %Eb";
584 else if (pDis->bOpCode == 0x1f)
585 {
586 Assert(pDis->cbInstr >= 3);
587 PUT_SZ("db 00fh, 01fh,");
588 PUT_NUM_8(MAKE_MODRM(pDis->ModRM.Bits.Mod, pDis->ModRM.Bits.Reg, pDis->ModRM.Bits.Rm));
589 for (unsigned i = 3; i < pDis->cbInstr; i++)
590 {
591 PUT_C(',');
592 PUT_NUM_8(0x90); /// @todo fixme.
593 }
594 pszFmt = "";
595 }
596 break;
597
598 default:
599 /* ST(X) -> stX (floating point) */
600 if (*pszFmt == 'f' && strchr(pszFmt, '('))
601 {
602 char *pszFmtDst = szTmpFmt;
603 char ch;
604 do
605 {
606 ch = *pszFmt++;
607 if (ch == 'S' && pszFmt[0] == 'T' && pszFmt[1] == '(')
608 {
609 *pszFmtDst++ = 's';
610 *pszFmtDst++ = 't';
611 pszFmt += 2;
612 ch = *pszFmt;
613 Assert(pszFmt[1] == ')');
614 pszFmt += 2;
615 *pszFmtDst++ = ch;
616 }
617 else
618 *pszFmtDst++ = ch;
619 } while (ch != '\0');
620 pszFmt = szTmpFmt;
621 }
622 if (strchr("#@&", *pszFmt))
623 {
624 const char *pszDelim = strchr(pszFmt, '/');
625 const char *pszSpace = (pszDelim ? strchr(pszDelim, ' ') : NULL);
626 if (pszDelim != NULL)
627 {
628 char *pszFmtDst = szTmpFmt;
629 if (pszSpace == NULL) pszSpace = strchr(pszDelim, 0);
630 if ( (*pszFmt == '#' && !pDis->bVexWFlag) /** @todo check this*/
631 || (*pszFmt == '@' && !VEXREG_IS256B(pDis->bVexDestReg))
632 || (*pszFmt == '&' && ( DISUSE_IS_EFFECTIVE_ADDR(pDis->Param1.fUse)
633 || DISUSE_IS_EFFECTIVE_ADDR(pDis->Param2.fUse)
634 || DISUSE_IS_EFFECTIVE_ADDR(pDis->Param3.fUse)
635 || DISUSE_IS_EFFECTIVE_ADDR(pDis->Param4.fUse))))
636 {
637 strncpy(pszFmtDst, pszFmt + 1, pszDelim - pszFmt - 1);
638 pszFmtDst += pszDelim - pszFmt - 1;
639 }
640 else
641 {
642 strncpy(pszFmtDst, pszDelim + 1, pszSpace - pszDelim - 1);
643 pszFmtDst += pszSpace - pszDelim - 1;
644 }
645 strcpy (pszFmtDst, pszSpace);
646 pszFmt = szTmpFmt;
647 }
648 }
649 break;
650
651 /*
652 * Horrible hacks.
653 */
654 case OP_FLD:
655 if (pDis->bOpCode == 0xdb) /* m80fp workaround. */
656 *(int *)&pDis->Param1.fParam &= ~0x1f; /* make it pure OP_PARM_M */
657 break;
658 case OP_LAR: /* hack w -> v, probably not correct. */
659 *(int *)&pDis->Param2.fParam &= ~0x1f;
660 *(int *)&pDis->Param2.fParam |= OP_PARM_v;
661 break;
662 }
663
664 /*
665 * Add operand size and address prefixes for outsb, movsb, etc.
666 */
667 if (pDis->fPrefix & (DISPREFIX_OPSIZE | DISPREFIX_ADDRSIZE))
668 {
669 if (fIgnoresOpSize && (pDis->fPrefix & DISPREFIX_OPSIZE) )
670 {
671 if (pDis->uCpuMode == DISCPUMODE_16BIT)
672 PUT_SZ("o32 ");
673 else
674 PUT_SZ("o16 ");
675 }
676 if (fMayNeedAddrSize && (pDis->fPrefix & DISPREFIX_ADDRSIZE) )
677 {
678 if (pDis->uCpuMode == DISCPUMODE_16BIT)
679 PUT_SZ("a32 ");
680 else
681 PUT_SZ("a16 ");
682 }
683 }
684
685 /*
686 * Formatting context and associated macros.
687 */
688 PCDISOPPARAM pParam = &pDis->Param1;
689 int iParam = 1;
690
691#define PUT_FAR() \
692 do { \
693 if ( OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_p \
694 && pOp->uOpcode != OP_LDS /* table bugs? */ \
695 && pOp->uOpcode != OP_LES \
696 && pOp->uOpcode != OP_LFS \
697 && pOp->uOpcode != OP_LGS \
698 && pOp->uOpcode != OP_LSS ) \
699 PUT_SZ("far "); \
700 } while (0)
701 /** @todo mov ah,ch ends up with a byte 'override'... - check if this wasn't fixed. */
702 /** @todo drop the work/dword/qword override when the src/dst is a register (except for movsx/movzx). */
703#define PUT_SIZE_OVERRIDE() \
704 do { \
705 switch (OP_PARM_VSUBTYPE(pParam->fParam)) \
706 { \
707 case OP_PARM_v: \
708 case OP_PARM_y: \
709 switch (pDis->uOpMode) \
710 { \
711 case DISCPUMODE_16BIT: if (OP_PARM_VSUBTYPE(pParam->fParam) != OP_PARM_y) PUT_SZ("word "); break; \
712 case DISCPUMODE_32BIT: \
713 if (pDis->pCurInstr->uOpcode != OP_GATHER || pDis->bVexWFlag) { PUT_SZ("dword "); break; } \
714 RT_FALL_THRU(); \
715 case DISCPUMODE_64BIT: PUT_SZ("qword "); break; \
716 default: break; \
717 } \
718 break; \
719 case OP_PARM_b: PUT_SZ("byte "); break; \
720 case OP_PARM_w: \
721 if ( OP_PARM_VTYPE(pParam->fParam) == OP_PARM_W \
722 || OP_PARM_VTYPE(pParam->fParam) == OP_PARM_M) \
723 { \
724 if (VEXREG_IS256B(pDis->bVexDestReg)) PUT_SZ("dword "); \
725 else PUT_SZ("word "); \
726 } \
727 break; \
728 case OP_PARM_d: \
729 if ( OP_PARM_VTYPE(pParam->fParam) == OP_PARM_W \
730 || OP_PARM_VTYPE(pParam->fParam) == OP_PARM_M) \
731 { \
732 if (VEXREG_IS256B(pDis->bVexDestReg)) PUT_SZ("qword "); \
733 else PUT_SZ("dword "); \
734 } \
735 break; \
736 case OP_PARM_q: \
737 if ( OP_PARM_VTYPE(pParam->fParam) == OP_PARM_W \
738 || OP_PARM_VTYPE(pParam->fParam) == OP_PARM_M) \
739 { \
740 if (VEXREG_IS256B(pDis->bVexDestReg)) PUT_SZ("oword "); \
741 else PUT_SZ("qword "); \
742 } \
743 break; \
744 case OP_PARM_ps: \
745 case OP_PARM_pd: \
746 case OP_PARM_x: if (VEXREG_IS256B(pDis->bVexDestReg)) { PUT_SZ("yword "); break; } RT_FALL_THRU(); \
747 case OP_PARM_ss: \
748 case OP_PARM_sd: \
749 case OP_PARM_dq: PUT_SZ("oword "); break; \
750 case OP_PARM_qq: PUT_SZ("yword "); break; \
751 case OP_PARM_p: break; /* see PUT_FAR */ \
752 case OP_PARM_s: if (pParam->fUse & DISUSE_REG_FP) PUT_SZ("tword "); break; /* ?? */ \
753 case OP_PARM_z: break; \
754 case OP_PARM_NONE: \
755 if ( OP_PARM_VTYPE(pParam->fParam) == OP_PARM_M \
756 && ((pParam->fUse & DISUSE_REG_FP) || pOp->uOpcode == OP_FLD)) \
757 PUT_SZ("tword "); \
758 break; \
759 default: break; /*no pointer type specified/necessary*/ \
760 } \
761 } while (0)
762 static const char s_szSegPrefix[6][4] = { "es:", "cs:", "ss:", "ds:", "fs:", "gs:" };
763#define PUT_SEGMENT_OVERRIDE() \
764 do { \
765 if (pDis->fPrefix & DISPREFIX_SEG) \
766 PUT_STR(s_szSegPrefix[pDis->idxSegPrefix], 3); \
767 } while (0)
768
769
770 /*
771 * Segment prefixing for instructions that doesn't do memory access.
772 */
773 if ( (pDis->fPrefix & DISPREFIX_SEG)
774 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param1.fUse)
775 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param2.fUse)
776 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param3.fUse))
777 {
778 PUT_STR(s_szSegPrefix[pDis->idxSegPrefix], 2);
779 PUT_C(' ');
780 }
781
782
783 /*
784 * The formatting loop.
785 */
786 RTINTPTR off;
787 char szSymbol[128];
788 char ch;
789 while ((ch = *pszFmt++) != '\0')
790 {
791 if (ch == '%')
792 {
793 ch = *pszFmt++;
794 switch (ch)
795 {
796 /*
797 * ModRM - Register only / VEX.vvvv.
798 */
799 case 'C': /* Control register (ParseModRM / UseModRM). */
800 case 'D': /* Debug register (ParseModRM / UseModRM). */
801 case 'G': /* ModRM selects general register (ParseModRM / UseModRM). */
802 case 'S': /* ModRM byte selects a segment register (ParseModRM / UseModRM). */
803 case 'T': /* ModRM byte selects a test register (ParseModRM / UseModRM). */
804 case 'V': /* ModRM byte selects an XMM/SSE register (ParseModRM / UseModRM). */
805 case 'P': /* ModRM byte selects MMX register (ParseModRM / UseModRM). */
806 case 'H': /* The VEX.vvvv field of the VEX prefix selects a XMM/YMM register. */
807 case 'B': /* The VEX.vvvv field of the VEX prefix selects a general register (ParseVexDest). */
808 case 'L': /* The upper 4 bits of the 8-bit immediate selects a XMM/YMM register. */
809 {
810 pszFmt += RT_C_IS_ALPHA(pszFmt[0]) ? RT_C_IS_ALPHA(pszFmt[1]) ? 2 : 1 : 0;
811 Assert(!(pParam->fUse & (DISUSE_INDEX | DISUSE_SCALE) /* No SIB here... */));
812 Assert(!(pParam->fUse & (DISUSE_DISPLACEMENT8 | DISUSE_DISPLACEMENT16 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT64 | DISUSE_RIPDISPLACEMENT32)));
813
814 size_t cchReg;
815 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
816 PUT_STR(pszReg, cchReg);
817 break;
818 }
819
820 /*
821 * ModRM - Register or memory.
822 */
823 case 'E': /* ModRM specifies parameter (ParseModRM / UseModRM / UseSIB). */
824 case 'Q': /* ModRM byte selects MMX register or memory address (ParseModRM / UseModRM). */
825 case 'R': /* ModRM byte may only refer to a general register (ParseModRM / UseModRM). */
826 case 'W': /* ModRM byte selects an XMM/SSE register or a memory address (ParseModRM / UseModRM). */
827 case 'U': /* ModRM byte may only refer to a XMM/SSE register (ParseModRM / UseModRM). */
828 case 'M': /* ModRM byte may only refer to memory (ParseModRM / UseModRM). */
829 {
830 pszFmt += RT_C_IS_ALPHA(pszFmt[0]) ? RT_C_IS_ALPHA(pszFmt[1]) ? 2 : 1 : 0;
831
832 PUT_FAR();
833 uint32_t const fUse = pParam->fUse;
834 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
835 {
836 /* Work around mov seg,[mem16] and mov [mem16],seg as these always make a 16-bit mem
837 while the register variants deals with 16, 32 & 64 in the normal fashion. */
838 if ( pParam->fParam != OP_PARM_Ev
839 || pOp->uOpcode != OP_MOV
840 || ( pOp->fParam1 != OP_PARM_Sw
841 && pOp->fParam2 != OP_PARM_Sw))
842 PUT_SIZE_OVERRIDE();
843 PUT_C('[');
844 }
845 if ( (fFlags & DIS_FMT_FLAGS_STRICT)
846 && (fUse & (DISUSE_DISPLACEMENT8 | DISUSE_DISPLACEMENT16 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT64 | DISUSE_RIPDISPLACEMENT32)))
847 {
848 if ( (fUse & DISUSE_DISPLACEMENT8)
849 && !pParam->uDisp.i8)
850 PUT_SZ("byte ");
851 else if ( (fUse & DISUSE_DISPLACEMENT16)
852 && (int8_t)pParam->uDisp.i16 == (int16_t)pParam->uDisp.i16)
853 PUT_SZ("word ");
854 else if ( (fUse & DISUSE_DISPLACEMENT32)
855 && (int16_t)pParam->uDisp.i32 == (int32_t)pParam->uDisp.i32) //??
856 PUT_SZ("dword ");
857 else if ( (fUse & DISUSE_DISPLACEMENT64)
858 && (pDis->SIB.Bits.Base != 5 || pDis->ModRM.Bits.Mod != 0)
859 && (int32_t)pParam->uDisp.i64 == (int64_t)pParam->uDisp.i64) //??
860 PUT_SZ("qword ");
861 }
862 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
863 PUT_SEGMENT_OVERRIDE();
864
865 bool fBase = (fUse & DISUSE_BASE) /* When exactly is DISUSE_BASE supposed to be set? disasmModRMReg doesn't set it. */
866 || ( (fUse & ( DISUSE_REG_GEN8
867 | DISUSE_REG_GEN16
868 | DISUSE_REG_GEN32
869 | DISUSE_REG_GEN64
870 | DISUSE_REG_FP
871 | DISUSE_REG_MMX
872 | DISUSE_REG_XMM
873 | DISUSE_REG_YMM
874 | DISUSE_REG_CR
875 | DISUSE_REG_DBG
876 | DISUSE_REG_SEG
877 | DISUSE_REG_TEST ))
878 && !DISUSE_IS_EFFECTIVE_ADDR(fUse));
879 if (fBase)
880 {
881 size_t cchReg;
882 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
883 PUT_STR(pszReg, cchReg);
884 }
885
886 if (fUse & DISUSE_INDEX)
887 {
888 if (fBase)
889 PUT_C('+');
890
891 size_t cchReg;
892 const char *pszReg = disasmFormatYasmIndexReg(pDis, pParam, &cchReg);
893 PUT_STR(pszReg, cchReg);
894
895 if (fUse & DISUSE_SCALE)
896 {
897 PUT_C('*');
898 PUT_C('0' + pParam->uScale);
899 }
900 }
901 else
902 Assert(!(fUse & DISUSE_SCALE));
903
904 int64_t off2 = 0;
905 if (fUse & (DISUSE_DISPLACEMENT8 | DISUSE_DISPLACEMENT16 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT64 | DISUSE_RIPDISPLACEMENT32))
906 {
907 if (fUse & DISUSE_DISPLACEMENT8)
908 off2 = pParam->uDisp.i8;
909 else if (fUse & DISUSE_DISPLACEMENT16)
910 off2 = pParam->uDisp.i16;
911 else if (fUse & (DISUSE_DISPLACEMENT32 | DISUSE_RIPDISPLACEMENT32))
912 off2 = pParam->uDisp.i32;
913 else if (fUse & DISUSE_DISPLACEMENT64)
914 off2 = pParam->uDisp.i64;
915 else
916 {
917 AssertFailed();
918 off2 = 0;
919 }
920
921 int64_t off3 = off2;
922 if (fBase || (fUse & (DISUSE_INDEX | DISUSE_RIPDISPLACEMENT32)))
923 {
924 PUT_C(off3 >= 0 ? '+' : '-');
925 if (off3 < 0)
926 off3 = -off3;
927 }
928 if (fUse & DISUSE_DISPLACEMENT8)
929 PUT_NUM_8( off3);
930 else if (fUse & DISUSE_DISPLACEMENT16)
931 PUT_NUM_16(off3);
932 else if (fUse & DISUSE_DISPLACEMENT32)
933 PUT_NUM_32(off3);
934 else if (fUse & DISUSE_DISPLACEMENT64)
935 PUT_NUM_64(off3);
936 else
937 {
938 PUT_NUM_32(off3);
939 PUT_SZ(" wrt rip (");
940 off2 += pDis->uInstrAddr + pDis->cbInstr;
941 PUT_NUM_64(off2);
942 if (pfnGetSymbol)
943 PUT_SYMBOL((pDis->fPrefix & DISPREFIX_SEG)
944 ? DIS_FMT_SEL_FROM_REG(pDis->idxSegPrefix)
945 : DIS_FMT_SEL_FROM_REG(DISSELREG_DS),
946 pDis->uAddrMode == DISCPUMODE_64BIT
947 ? (uint64_t)off2
948 : pDis->uAddrMode == DISCPUMODE_32BIT
949 ? (uint32_t)off2
950 : (uint16_t)off2,
951 " = ",
952 ')');
953 else
954 PUT_C(')');
955 }
956 }
957
958 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
959 {
960 if (pfnGetSymbol && !fBase && !(fUse & (DISUSE_INDEX | DISUSE_RIPDISPLACEMENT32)) && off2 != 0)
961 PUT_SYMBOL((pDis->fPrefix & DISPREFIX_SEG)
962 ? DIS_FMT_SEL_FROM_REG(pDis->idxSegPrefix)
963 : DIS_FMT_SEL_FROM_REG(DISSELREG_DS),
964 pDis->uAddrMode == DISCPUMODE_64BIT
965 ? (uint64_t)off2
966 : pDis->uAddrMode == DISCPUMODE_32BIT
967 ? (uint32_t)off2
968 : (uint16_t)off2,
969 " (=",
970 ')');
971 PUT_C(']');
972 }
973 break;
974 }
975
976 case 'F': /* Eflags register (0 - popf/pushf only, avoided in adjustments above). */
977 AssertFailed();
978 break;
979
980 case 'I': /* Immediate data (ParseImmByte, ParseImmByteSX, ParseImmV, ParseImmUshort, ParseImmZ). */
981 Assert(*pszFmt == 'b' || *pszFmt == 'v' || *pszFmt == 'w' || *pszFmt == 'z'); pszFmt++;
982 switch (pParam->fUse & ( DISUSE_IMMEDIATE8 | DISUSE_IMMEDIATE16 | DISUSE_IMMEDIATE32 | DISUSE_IMMEDIATE64
983 | DISUSE_IMMEDIATE16_SX8 | DISUSE_IMMEDIATE32_SX8 | DISUSE_IMMEDIATE64_SX8))
984 {
985 case DISUSE_IMMEDIATE8:
986 if ( (fFlags & DIS_FMT_FLAGS_STRICT)
987 && ( (pOp->fParam1 >= OP_PARM_REG_GEN8_START && pOp->fParam1 <= OP_PARM_REG_GEN8_END)
988 || (pOp->fParam2 >= OP_PARM_REG_GEN8_START && pOp->fParam2 <= OP_PARM_REG_GEN8_END))
989 )
990 PUT_SZ("strict byte ");
991 PUT_NUM_8(pParam->uValue);
992 break;
993
994 case DISUSE_IMMEDIATE16:
995 if ( pDis->uCpuMode != pDis->uOpMode
996 || ( (fFlags & DIS_FMT_FLAGS_STRICT)
997 && ( (int8_t)pParam->uValue == (int16_t)pParam->uValue
998 || (pOp->fParam1 >= OP_PARM_REG_GEN16_START && pOp->fParam1 <= OP_PARM_REG_GEN16_END)
999 || (pOp->fParam2 >= OP_PARM_REG_GEN16_START && pOp->fParam2 <= OP_PARM_REG_GEN16_END))
1000 )
1001 )
1002 {
1003 if (OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_b)
1004 PUT_SZ_STRICT("strict byte ", "byte ");
1005 else if ( OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_v
1006 || OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_z)
1007 PUT_SZ_STRICT("strict word ", "word ");
1008 }
1009 PUT_NUM_16(pParam->uValue);
1010 break;
1011
1012 case DISUSE_IMMEDIATE16_SX8:
1013 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1014 || pDis->pCurInstr->uOpcode != OP_PUSH)
1015 PUT_SZ_STRICT("strict byte ", "byte ");
1016 else
1017 PUT_SZ("word ");
1018 PUT_NUM_16(pParam->uValue);
1019 break;
1020
1021 case DISUSE_IMMEDIATE32:
1022 if ( pDis->uOpMode != (pDis->uCpuMode == DISCPUMODE_16BIT ? DISCPUMODE_16BIT : DISCPUMODE_32BIT) /* not perfect */
1023 || ( (fFlags & DIS_FMT_FLAGS_STRICT)
1024 && ( (int8_t)pParam->uValue == (int32_t)pParam->uValue
1025 || (pOp->fParam1 >= OP_PARM_REG_GEN32_START && pOp->fParam1 <= OP_PARM_REG_GEN32_END)
1026 || (pOp->fParam2 >= OP_PARM_REG_GEN32_START && pOp->fParam2 <= OP_PARM_REG_GEN32_END))
1027 )
1028 )
1029 {
1030 if (OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_b)
1031 PUT_SZ_STRICT("strict byte ", "byte ");
1032 else if ( OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_v
1033 || OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_z)
1034 PUT_SZ_STRICT("strict dword ", "dword ");
1035 }
1036 PUT_NUM_32(pParam->uValue);
1037 if (pDis->uCpuMode == DISCPUMODE_32BIT)
1038 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uValue, " (=", ')');
1039 break;
1040
1041 case DISUSE_IMMEDIATE32_SX8:
1042 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1043 || pDis->pCurInstr->uOpcode != OP_PUSH)
1044 PUT_SZ_STRICT("strict byte ", "byte ");
1045 else
1046 PUT_SZ("dword ");
1047 PUT_NUM_32(pParam->uValue);
1048 break;
1049
1050 case DISUSE_IMMEDIATE64_SX8:
1051 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1052 || pDis->pCurInstr->uOpcode != OP_PUSH)
1053 PUT_SZ_STRICT("strict byte ", "byte ");
1054 else
1055 PUT_SZ("qword ");
1056 PUT_NUM_64(pParam->uValue);
1057 break;
1058
1059 case DISUSE_IMMEDIATE64:
1060 PUT_NUM_64(pParam->uValue);
1061 break;
1062
1063 default:
1064 AssertFailed();
1065 break;
1066 }
1067 break;
1068
1069 case 'J': /* Relative jump offset (ParseImmBRel + ParseImmVRel). */
1070 {
1071 int32_t offDisplacement;
1072 Assert(iParam == 1);
1073 bool fPrefix = (fFlags & DIS_FMT_FLAGS_STRICT)
1074 && pOp->uOpcode != OP_CALL
1075 && pOp->uOpcode != OP_LOOP
1076 && pOp->uOpcode != OP_LOOPE
1077 && pOp->uOpcode != OP_LOOPNE
1078 && pOp->uOpcode != OP_JECXZ;
1079 if (pOp->uOpcode == OP_CALL)
1080 fFlags &= ~DIS_FMT_FLAGS_RELATIVE_BRANCH;
1081
1082 if (pParam->fUse & DISUSE_IMMEDIATE8_REL)
1083 {
1084 if (fPrefix)
1085 PUT_SZ("short ");
1086 offDisplacement = (int8_t)pParam->uValue;
1087 Assert(*pszFmt == 'b'); pszFmt++;
1088
1089 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1090 PUT_NUM_S8(offDisplacement);
1091 }
1092 else if (pParam->fUse & DISUSE_IMMEDIATE16_REL)
1093 {
1094 if (fPrefix)
1095 PUT_SZ("near ");
1096 offDisplacement = (int16_t)pParam->uValue;
1097 Assert(*pszFmt == 'v'); pszFmt++;
1098
1099 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1100 PUT_NUM_S16(offDisplacement);
1101 }
1102 else
1103 {
1104 if (fPrefix)
1105 PUT_SZ("near ");
1106 offDisplacement = (int32_t)pParam->uValue;
1107 Assert(pParam->fUse & (DISUSE_IMMEDIATE32_REL | DISUSE_IMMEDIATE64_REL));
1108 Assert(*pszFmt == 'v'); pszFmt++;
1109
1110 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1111 PUT_NUM_S32(offDisplacement);
1112 }
1113 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1114 PUT_SZ(" (");
1115
1116 RTUINTPTR uTrgAddr = pDis->uInstrAddr + pDis->cbInstr + offDisplacement;
1117 if (pDis->uCpuMode == DISCPUMODE_16BIT)
1118 PUT_NUM_16(uTrgAddr);
1119 else if (pDis->uCpuMode == DISCPUMODE_32BIT)
1120 PUT_NUM_32(uTrgAddr);
1121 else
1122 PUT_NUM_64(uTrgAddr);
1123
1124 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1125 {
1126 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), uTrgAddr, " = ", ' ');
1127 PUT_C(')');
1128 }
1129 else
1130 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), uTrgAddr, " (", ')');
1131 break;
1132 }
1133
1134 case 'A': /* Direct (jump/call) address (ParseImmAddr). */
1135 {
1136 Assert(*pszFmt == 'p'); pszFmt++;
1137 PUT_FAR();
1138 PUT_SIZE_OVERRIDE();
1139 PUT_SEGMENT_OVERRIDE();
1140 off = 0;
1141 int rc = VERR_SYMBOL_NOT_FOUND;
1142 switch (pParam->fUse & (DISUSE_IMMEDIATE_ADDR_16_16 | DISUSE_IMMEDIATE_ADDR_16_32 | DISUSE_DISPLACEMENT64 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT16))
1143 {
1144 case DISUSE_IMMEDIATE_ADDR_16_16:
1145 PUT_NUM_16(pParam->uValue >> 16);
1146 PUT_C(':');
1147 PUT_NUM_16(pParam->uValue);
1148 if (pfnGetSymbol)
1149 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1150 break;
1151 case DISUSE_IMMEDIATE_ADDR_16_32:
1152 PUT_NUM_16(pParam->uValue >> 32);
1153 PUT_C(':');
1154 PUT_NUM_32(pParam->uValue);
1155 if (pfnGetSymbol)
1156 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1157 break;
1158 case DISUSE_DISPLACEMENT16:
1159 PUT_NUM_16(pParam->uValue);
1160 if (pfnGetSymbol)
1161 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1162 break;
1163 case DISUSE_DISPLACEMENT32:
1164 PUT_NUM_32(pParam->uValue);
1165 if (pfnGetSymbol)
1166 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1167 break;
1168 case DISUSE_DISPLACEMENT64:
1169 PUT_NUM_64(pParam->uValue);
1170 if (pfnGetSymbol)
1171 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint64_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1172 break;
1173 default:
1174 AssertFailed();
1175 break;
1176 }
1177
1178 PUT_SYMBOL_TWO(rc, " [", ']');
1179 break;
1180 }
1181
1182 case 'O': /* No ModRM byte (ParseImmAddr). */
1183 {
1184 Assert(*pszFmt == 'b' || *pszFmt == 'v'); pszFmt++;
1185 PUT_FAR();
1186 PUT_SIZE_OVERRIDE();
1187 PUT_C('[');
1188 PUT_SEGMENT_OVERRIDE();
1189 off = 0;
1190 int rc = VERR_SYMBOL_NOT_FOUND;
1191 switch (pParam->fUse & (DISUSE_IMMEDIATE_ADDR_16_16 | DISUSE_IMMEDIATE_ADDR_16_32 | DISUSE_DISPLACEMENT64 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT16))
1192 {
1193 case DISUSE_IMMEDIATE_ADDR_16_16:
1194 PUT_NUM_16(pParam->uValue >> 16);
1195 PUT_C(':');
1196 PUT_NUM_16(pParam->uValue);
1197 if (pfnGetSymbol)
1198 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1199 break;
1200 case DISUSE_IMMEDIATE_ADDR_16_32:
1201 PUT_NUM_16(pParam->uValue >> 32);
1202 PUT_C(':');
1203 PUT_NUM_32(pParam->uValue);
1204 if (pfnGetSymbol)
1205 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1206 break;
1207 case DISUSE_DISPLACEMENT16:
1208 PUT_NUM_16(pParam->uDisp.i16);
1209 if (pfnGetSymbol)
1210 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u16, szSymbol, sizeof(szSymbol), &off, pvUser);
1211 break;
1212 case DISUSE_DISPLACEMENT32:
1213 PUT_NUM_32(pParam->uDisp.i32);
1214 if (pfnGetSymbol)
1215 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u32, szSymbol, sizeof(szSymbol), &off, pvUser);
1216 break;
1217 case DISUSE_DISPLACEMENT64:
1218 PUT_NUM_64(pParam->uDisp.i64);
1219 if (pfnGetSymbol)
1220 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u64, szSymbol, sizeof(szSymbol), &off, pvUser);
1221 break;
1222 default:
1223 AssertFailed();
1224 break;
1225 }
1226 PUT_C(']');
1227
1228 PUT_SYMBOL_TWO(rc, " (", ')');
1229 break;
1230 }
1231
1232 case 'X': /* DS:SI (ParseXb, ParseXv). */
1233 case 'Y': /* ES:DI (ParseYb, ParseYv). */
1234 {
1235 Assert(*pszFmt == 'b' || *pszFmt == 'v'); pszFmt++;
1236 PUT_FAR();
1237 PUT_SIZE_OVERRIDE();
1238 PUT_C('[');
1239 if (pParam->fUse & DISUSE_POINTER_DS_BASED)
1240 PUT_SZ("ds:");
1241 else
1242 PUT_SZ("es:");
1243
1244 size_t cchReg;
1245 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
1246 PUT_STR(pszReg, cchReg);
1247 PUT_C(']');
1248 break;
1249 }
1250
1251 case 'e': /* Register based on operand size (e.g. %eAX, %eAH) (ParseFixedReg). */
1252 {
1253 Assert(RT_C_IS_ALPHA(pszFmt[0]) && RT_C_IS_ALPHA(pszFmt[1]) && !RT_C_IS_ALPHA(pszFmt[2]));
1254 pszFmt += 2;
1255 size_t cchReg;
1256 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
1257 PUT_STR(pszReg, cchReg);
1258 break;
1259 }
1260
1261 default:
1262 AssertMsgFailed(("%c%s!\n", ch, pszFmt));
1263 break;
1264 }
1265 AssertMsg(*pszFmt == ',' || *pszFmt == '\0', ("%c%s\n", ch, pszFmt));
1266 }
1267 else
1268 {
1269 PUT_C(ch);
1270 if (ch == ',')
1271 {
1272 Assert(*pszFmt != ' ');
1273 PUT_C(' ');
1274 switch (++iParam)
1275 {
1276 case 2: pParam = &pDis->Param2; break;
1277 case 3: pParam = &pDis->Param3; break;
1278 case 4: pParam = &pDis->Param4; break;
1279 default: pParam = NULL; break;
1280 }
1281 }
1282 }
1283 } /* while more to format */
1284 }
1285
1286 /*
1287 * Any additional output to the right of the instruction?
1288 */
1289 if (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_RIGHT))
1290 {
1291 /* some up front padding. */
1292 size_t cchPadding = cchOutput - offInstruction;
1293 cchPadding = cchPadding + 1 >= 42 ? 1 : 42 - cchPadding;
1294 PUT_STR(g_szSpaces, cchPadding);
1295
1296 /* comment? */
1297 if (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_RIGHT))
1298 PUT_SZ(";");
1299
1300 /*
1301 * The address?
1302 */
1303 if (fFlags & DIS_FMT_FLAGS_ADDR_RIGHT)
1304 {
1305 PUT_C(' ');
1306#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
1307 if (pDis->uInstrAddr >= _4G)
1308 PUT_NUM(9, "%08x`", (uint32_t)(pDis->uInstrAddr >> 32));
1309#endif
1310 PUT_NUM(8, "%08x", (uint32_t)pDis->uInstrAddr);
1311 }
1312
1313 /*
1314 * Opcode bytes?
1315 */
1316 if (fFlags & DIS_FMT_FLAGS_BYTES_RIGHT)
1317 {
1318 PUT_C(' ');
1319 size_t cchTmp = disFormatBytes(pDis, pszDst, cchDst, fFlags);
1320 cchOutput += cchTmp;
1321 if (cchTmp >= cchDst)
1322 cchTmp = cchDst - (cchDst != 0);
1323 cchDst -= cchTmp;
1324 pszDst += cchTmp;
1325 }
1326 }
1327
1328 /*
1329 * Terminate it - on overflow we'll have reserved one byte for this.
1330 */
1331 if (cchDst > 0)
1332 *pszDst = '\0';
1333 else
1334 Assert(!cchBuf);
1335
1336 /* clean up macros */
1337#undef PUT_PSZ
1338#undef PUT_SZ
1339#undef PUT_STR
1340#undef PUT_C
1341 return cchOutput;
1342}
1343
1344
1345/**
1346 * Formats the current instruction in Yasm (/ Nasm) style.
1347 *
1348 * This is a simplified version of DISFormatYasmEx() provided for your convenience.
1349 *
1350 *
1351 * @returns The number of output characters. If this is >= cchBuf, then the content
1352 * of pszBuf will be truncated.
1353 * @param pDis Pointer to the disassembler state.
1354 * @param pszBuf The output buffer.
1355 * @param cchBuf The size of the output buffer.
1356 */
1357DISDECL(size_t) DISFormatYasm(PCDISSTATE pDis, char *pszBuf, size_t cchBuf)
1358{
1359 return DISFormatYasmEx(pDis, pszBuf, cchBuf, 0 /* fFlags */, NULL /* pfnGetSymbol */, NULL /* pvUser */);
1360}
1361
1362
1363/**
1364 * Checks if the encoding of the given disassembled instruction is something we
1365 * can never get YASM to produce.
1366 *
1367 * @returns true if it's odd, false if it isn't.
1368 * @param pDis The disassembler output. The byte fetcher callback will
1369 * be used if present as we might need to fetch opcode
1370 * bytes.
1371 */
1372DISDECL(bool) DISFormatYasmIsOddEncoding(PDISSTATE pDis)
1373{
1374 /*
1375 * Mod rm + SIB: Check for duplicate EBP encodings that yasm won't use for very good reasons.
1376 */
1377 if ( pDis->uAddrMode != DISCPUMODE_16BIT /// @todo correct?
1378 && pDis->ModRM.Bits.Rm == 4
1379 && pDis->ModRM.Bits.Mod != 3)
1380 {
1381 /* No scaled index SIB (index=4), except for ESP. */
1382 if ( pDis->SIB.Bits.Index == 4
1383 && pDis->SIB.Bits.Base != 4)
1384 return true;
1385
1386 /* EBP + displacement */
1387 if ( pDis->ModRM.Bits.Mod != 0
1388 && pDis->SIB.Bits.Base == 5
1389 && pDis->SIB.Bits.Scale == 0)
1390 return true;
1391 }
1392
1393 /*
1394 * Seems to be an instruction alias here, but I cannot find any docs on it... hrmpf!
1395 */
1396 if ( pDis->pCurInstr->uOpcode == OP_SHL
1397 && pDis->ModRM.Bits.Reg == 6)
1398 return true;
1399
1400 /*
1401 * Check for multiple prefixes of the same kind.
1402 */
1403 uint8_t off1stSeg = UINT8_MAX;
1404 uint8_t offOpSize = UINT8_MAX;
1405 uint8_t offAddrSize = UINT8_MAX;
1406 uint32_t fPrefixes = 0;
1407 for (uint32_t offOpcode = 0; offOpcode < RT_ELEMENTS(pDis->abInstr); offOpcode++)
1408 {
1409 uint32_t f;
1410 switch (pDis->abInstr[offOpcode])
1411 {
1412 case 0xf0:
1413 f = DISPREFIX_LOCK;
1414 break;
1415
1416 case 0xf2:
1417 case 0xf3:
1418 f = DISPREFIX_REP; /* yes, both */
1419 break;
1420
1421 case 0x2e:
1422 case 0x3e:
1423 case 0x26:
1424 case 0x36:
1425 case 0x64:
1426 case 0x65:
1427 if (off1stSeg == UINT8_MAX)
1428 off1stSeg = offOpcode;
1429 f = DISPREFIX_SEG;
1430 break;
1431
1432 case 0x66:
1433 if (offOpSize == UINT8_MAX)
1434 offOpSize = offOpcode;
1435 f = DISPREFIX_OPSIZE;
1436 break;
1437
1438 case 0x67:
1439 if (offAddrSize == UINT8_MAX)
1440 offAddrSize = offOpcode;
1441 f = DISPREFIX_ADDRSIZE;
1442 break;
1443
1444 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
1445 case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
1446 f = pDis->uCpuMode == DISCPUMODE_64BIT ? DISPREFIX_REX : 0;
1447 break;
1448
1449 default:
1450 f = 0;
1451 break;
1452 }
1453 if (!f)
1454 break; /* done */
1455 if (fPrefixes & f)
1456 return true;
1457 fPrefixes |= f;
1458 }
1459
1460 /* segment overrides are fun */
1461 if (fPrefixes & DISPREFIX_SEG)
1462 {
1463 /* no effective address which it may apply to. */
1464 Assert((pDis->fPrefix & DISPREFIX_SEG) || pDis->uCpuMode == DISCPUMODE_64BIT);
1465 if ( !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param1.fUse)
1466 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param2.fUse)
1467 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param3.fUse))
1468 return true;
1469
1470 /* Yasm puts the segment prefixes before the operand prefix with no
1471 way of overriding it. */
1472 if (offOpSize < off1stSeg)
1473 return true;
1474 }
1475
1476 /* fixed register + addr override doesn't go down all that well. */
1477 if (fPrefixes & DISPREFIX_ADDRSIZE)
1478 {
1479 Assert(pDis->fPrefix & DISPREFIX_ADDRSIZE);
1480 if ( pDis->pCurInstr->fParam3 == OP_PARM_NONE
1481 && pDis->pCurInstr->fParam2 == OP_PARM_NONE
1482 && ( pDis->pCurInstr->fParam1 >= OP_PARM_REG_GEN32_START
1483 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_GEN32_END))
1484 return true;
1485 }
1486
1487 /* Almost all prefixes are bad for jumps. */
1488 if (fPrefixes)
1489 {
1490 switch (pDis->pCurInstr->uOpcode)
1491 {
1492 /* nop w/ prefix(es). */
1493 case OP_NOP:
1494 return true;
1495
1496 case OP_JMP:
1497 if ( pDis->pCurInstr->fParam1 != OP_PARM_Jb
1498 && pDis->pCurInstr->fParam1 != OP_PARM_Jv)
1499 break;
1500 RT_FALL_THRU();
1501 case OP_JO:
1502 case OP_JNO:
1503 case OP_JC:
1504 case OP_JNC:
1505 case OP_JE:
1506 case OP_JNE:
1507 case OP_JBE:
1508 case OP_JNBE:
1509 case OP_JS:
1510 case OP_JNS:
1511 case OP_JP:
1512 case OP_JNP:
1513 case OP_JL:
1514 case OP_JNL:
1515 case OP_JLE:
1516 case OP_JNLE:
1517 /** @todo branch hinting 0x2e/0x3e... */
1518 return true;
1519 }
1520
1521 }
1522
1523 /* All but the segment prefix is bad news for push/pop. */
1524 if (fPrefixes & ~DISPREFIX_SEG)
1525 {
1526 switch (pDis->pCurInstr->uOpcode)
1527 {
1528 case OP_POP:
1529 case OP_PUSH:
1530 if ( pDis->pCurInstr->fParam1 >= OP_PARM_REG_SEG_START
1531 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_SEG_END)
1532 return true;
1533 if ( (fPrefixes & ~DISPREFIX_OPSIZE)
1534 && pDis->pCurInstr->fParam1 >= OP_PARM_REG_GEN32_START
1535 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_GEN32_END)
1536 return true;
1537 break;
1538
1539 case OP_POPA:
1540 case OP_POPF:
1541 case OP_PUSHA:
1542 case OP_PUSHF:
1543 if (fPrefixes & ~DISPREFIX_OPSIZE)
1544 return true;
1545 break;
1546 }
1547 }
1548
1549 /* Implicit 8-bit register instructions doesn't mix with operand size. */
1550 if ( (fPrefixes & DISPREFIX_OPSIZE)
1551 && ( ( pDis->pCurInstr->fParam1 == OP_PARM_Gb /* r8 */
1552 && pDis->pCurInstr->fParam2 == OP_PARM_Eb /* r8/mem8 */)
1553 || ( pDis->pCurInstr->fParam2 == OP_PARM_Gb /* r8 */
1554 && pDis->pCurInstr->fParam1 == OP_PARM_Eb /* r8/mem8 */))
1555 )
1556 {
1557 switch (pDis->pCurInstr->uOpcode)
1558 {
1559 case OP_ADD:
1560 case OP_OR:
1561 case OP_ADC:
1562 case OP_SBB:
1563 case OP_AND:
1564 case OP_SUB:
1565 case OP_XOR:
1566 case OP_CMP:
1567 return true;
1568 default:
1569 break;
1570 }
1571 }
1572
1573 /* Instructions taking no address or operand which thus may be annoyingly
1574 difficult to format for yasm. */
1575 if (fPrefixes)
1576 {
1577 switch (pDis->pCurInstr->uOpcode)
1578 {
1579 case OP_STI:
1580 case OP_STC:
1581 case OP_CLI:
1582 case OP_CLD:
1583 case OP_CLC:
1584 case OP_INT:
1585 case OP_INT3:
1586 case OP_INTO:
1587 case OP_HLT:
1588 /** @todo Many more to can be added here. */
1589 return true;
1590 default:
1591 break;
1592 }
1593 }
1594
1595 /* FPU and other instructions that ignores operand size override. */
1596 if (fPrefixes & DISPREFIX_OPSIZE)
1597 {
1598 switch (pDis->pCurInstr->uOpcode)
1599 {
1600 /* FPU: */
1601 case OP_FIADD:
1602 case OP_FIMUL:
1603 case OP_FISUB:
1604 case OP_FISUBR:
1605 case OP_FIDIV:
1606 case OP_FIDIVR:
1607 /** @todo there are many more. */
1608 return true;
1609
1610 case OP_MOV:
1611 /** @todo could be that we're not disassembling these correctly. */
1612 if (pDis->pCurInstr->fParam1 == OP_PARM_Sw)
1613 return true;
1614 /** @todo what about the other way? */
1615 break;
1616
1617 default:
1618 break;
1619 }
1620 }
1621
1622
1623 /*
1624 * Check for the version of xyz reg,reg instruction that the assembler doesn't use.
1625 *
1626 * For example:
1627 * expected: 1aee sbb ch, dh ; SBB r8, r/m8
1628 * yasm: 18F5 sbb ch, dh ; SBB r/m8, r8
1629 */
1630 if (pDis->ModRM.Bits.Mod == 3 /* reg,reg */)
1631 {
1632 switch (pDis->pCurInstr->uOpcode)
1633 {
1634 case OP_ADD:
1635 case OP_OR:
1636 case OP_ADC:
1637 case OP_SBB:
1638 case OP_AND:
1639 case OP_SUB:
1640 case OP_XOR:
1641 case OP_CMP:
1642 if ( ( pDis->pCurInstr->fParam1 == OP_PARM_Gb /* r8 */
1643 && pDis->pCurInstr->fParam2 == OP_PARM_Eb /* r8/mem8 */)
1644 || ( pDis->pCurInstr->fParam1 == OP_PARM_Gv /* rX */
1645 && pDis->pCurInstr->fParam2 == OP_PARM_Ev /* rX/memX */))
1646 return true;
1647
1648 /* 82 (see table A-6). */
1649 if (pDis->bOpCode == 0x82)
1650 return true;
1651 break;
1652
1653 /* ff /0, fe /0, ff /1, fe /0 */
1654 case OP_DEC:
1655 case OP_INC:
1656 return true;
1657
1658 case OP_POP:
1659 case OP_PUSH:
1660 Assert(pDis->bOpCode == 0x8f);
1661 return true;
1662
1663 case OP_MOV:
1664 if ( pDis->bOpCode == 0x8a
1665 || pDis->bOpCode == 0x8b)
1666 return true;
1667 break;
1668
1669 default:
1670 break;
1671 }
1672 }
1673
1674 /* shl eax,1 will be assembled to the form without the immediate byte. */
1675 if ( pDis->pCurInstr->fParam2 == OP_PARM_Ib
1676 && (uint8_t)pDis->Param2.uValue == 1)
1677 {
1678 switch (pDis->pCurInstr->uOpcode)
1679 {
1680 case OP_SHL:
1681 case OP_SHR:
1682 case OP_SAR:
1683 case OP_RCL:
1684 case OP_RCR:
1685 case OP_ROL:
1686 case OP_ROR:
1687 return true;
1688 }
1689 }
1690
1691 /* And some more - see table A-6. */
1692 if (pDis->bOpCode == 0x82)
1693 {
1694 switch (pDis->pCurInstr->uOpcode)
1695 {
1696 case OP_ADD:
1697 case OP_OR:
1698 case OP_ADC:
1699 case OP_SBB:
1700 case OP_AND:
1701 case OP_SUB:
1702 case OP_XOR:
1703 case OP_CMP:
1704 return true;
1705 break;
1706 }
1707 }
1708
1709
1710 /* check for REX.X = 1 without SIB. */
1711
1712 /* Yasm encodes setnbe al with /2 instead of /0 like the AMD manual
1713 says (intel doesn't appear to care). */
1714 switch (pDis->pCurInstr->uOpcode)
1715 {
1716 case OP_SETO:
1717 case OP_SETNO:
1718 case OP_SETC:
1719 case OP_SETNC:
1720 case OP_SETE:
1721 case OP_SETNE:
1722 case OP_SETBE:
1723 case OP_SETNBE:
1724 case OP_SETS:
1725 case OP_SETNS:
1726 case OP_SETP:
1727 case OP_SETNP:
1728 case OP_SETL:
1729 case OP_SETNL:
1730 case OP_SETLE:
1731 case OP_SETNLE:
1732 AssertMsg(pDis->bOpCode >= 0x90 && pDis->bOpCode <= 0x9f, ("%#x\n", pDis->bOpCode));
1733 if (pDis->ModRM.Bits.Reg != 2)
1734 return true;
1735 break;
1736 }
1737
1738 /*
1739 * The MOVZX reg32,mem16 instruction without an operand size prefix
1740 * doesn't quite make sense...
1741 */
1742 if ( pDis->pCurInstr->uOpcode == OP_MOVZX
1743 && pDis->bOpCode == 0xB7
1744 && (pDis->uCpuMode == DISCPUMODE_16BIT) != !!(fPrefixes & DISPREFIX_OPSIZE))
1745 return true;
1746
1747 return false;
1748}
1749
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette