VirtualBox

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

Last change on this file since 95406 was 95314, checked in by vboxsync, 3 years ago

DIS: Added blsr, blsmsk and blsr; corrected some vex parsing/formatting issues. bugref:9898 bugref:6251

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 70.4 KB
Line 
1/* $Id: DisasmFormatYasm.cpp 95314 2022-06-21 00:25:45Z 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 'M': /* ModRM may only refer to memory (ParseModRM / UseModRM). */
828 {
829 pszFmt += RT_C_IS_ALPHA(pszFmt[0]) ? RT_C_IS_ALPHA(pszFmt[1]) ? 2 : 1 : 0;
830
831 PUT_FAR();
832 uint32_t const fUse = pParam->fUse;
833 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
834 {
835 /* Work around mov seg,[mem16] and mov [mem16],seg as these always make a 16-bit mem
836 while the register variants deals with 16, 32 & 64 in the normal fashion. */
837 if ( pParam->fParam != OP_PARM_Ev
838 || pOp->uOpcode != OP_MOV
839 || ( pOp->fParam1 != OP_PARM_Sw
840 && pOp->fParam2 != OP_PARM_Sw))
841 PUT_SIZE_OVERRIDE();
842 PUT_C('[');
843 }
844 if ( (fFlags & DIS_FMT_FLAGS_STRICT)
845 && (fUse & (DISUSE_DISPLACEMENT8 | DISUSE_DISPLACEMENT16 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT64 | DISUSE_RIPDISPLACEMENT32)))
846 {
847 if ( (fUse & DISUSE_DISPLACEMENT8)
848 && !pParam->uDisp.i8)
849 PUT_SZ("byte ");
850 else if ( (fUse & DISUSE_DISPLACEMENT16)
851 && (int8_t)pParam->uDisp.i16 == (int16_t)pParam->uDisp.i16)
852 PUT_SZ("word ");
853 else if ( (fUse & DISUSE_DISPLACEMENT32)
854 && (int16_t)pParam->uDisp.i32 == (int32_t)pParam->uDisp.i32) //??
855 PUT_SZ("dword ");
856 else if ( (fUse & DISUSE_DISPLACEMENT64)
857 && (pDis->SIB.Bits.Base != 5 || pDis->ModRM.Bits.Mod != 0)
858 && (int32_t)pParam->uDisp.i64 == (int64_t)pParam->uDisp.i64) //??
859 PUT_SZ("qword ");
860 }
861 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
862 PUT_SEGMENT_OVERRIDE();
863
864 bool fBase = (fUse & DISUSE_BASE) /* When exactly is DISUSE_BASE supposed to be set? disasmModRMReg doesn't set it. */
865 || ( (fUse & ( DISUSE_REG_GEN8
866 | DISUSE_REG_GEN16
867 | DISUSE_REG_GEN32
868 | DISUSE_REG_GEN64
869 | DISUSE_REG_FP
870 | DISUSE_REG_MMX
871 | DISUSE_REG_XMM
872 | DISUSE_REG_YMM
873 | DISUSE_REG_CR
874 | DISUSE_REG_DBG
875 | DISUSE_REG_SEG
876 | DISUSE_REG_TEST ))
877 && !DISUSE_IS_EFFECTIVE_ADDR(fUse));
878 if (fBase)
879 {
880 size_t cchReg;
881 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
882 PUT_STR(pszReg, cchReg);
883 }
884
885 if (fUse & DISUSE_INDEX)
886 {
887 if (fBase)
888 PUT_C('+');
889
890 size_t cchReg;
891 const char *pszReg = disasmFormatYasmIndexReg(pDis, pParam, &cchReg);
892 PUT_STR(pszReg, cchReg);
893
894 if (fUse & DISUSE_SCALE)
895 {
896 PUT_C('*');
897 PUT_C('0' + pParam->uScale);
898 }
899 }
900 else
901 Assert(!(fUse & DISUSE_SCALE));
902
903 int64_t off2 = 0;
904 if (fUse & (DISUSE_DISPLACEMENT8 | DISUSE_DISPLACEMENT16 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT64 | DISUSE_RIPDISPLACEMENT32))
905 {
906 if (fUse & DISUSE_DISPLACEMENT8)
907 off2 = pParam->uDisp.i8;
908 else if (fUse & DISUSE_DISPLACEMENT16)
909 off2 = pParam->uDisp.i16;
910 else if (fUse & (DISUSE_DISPLACEMENT32 | DISUSE_RIPDISPLACEMENT32))
911 off2 = pParam->uDisp.i32;
912 else if (fUse & DISUSE_DISPLACEMENT64)
913 off2 = pParam->uDisp.i64;
914 else
915 {
916 AssertFailed();
917 off2 = 0;
918 }
919
920 int64_t off3 = off2;
921 if (fBase || (fUse & (DISUSE_INDEX | DISUSE_RIPDISPLACEMENT32)))
922 {
923 PUT_C(off3 >= 0 ? '+' : '-');
924 if (off3 < 0)
925 off3 = -off3;
926 }
927 if (fUse & DISUSE_DISPLACEMENT8)
928 PUT_NUM_8( off3);
929 else if (fUse & DISUSE_DISPLACEMENT16)
930 PUT_NUM_16(off3);
931 else if (fUse & DISUSE_DISPLACEMENT32)
932 PUT_NUM_32(off3);
933 else if (fUse & DISUSE_DISPLACEMENT64)
934 PUT_NUM_64(off3);
935 else
936 {
937 PUT_NUM_32(off3);
938 PUT_SZ(" wrt rip (");
939 off2 += pDis->uInstrAddr + pDis->cbInstr;
940 PUT_NUM_64(off2);
941 if (pfnGetSymbol)
942 PUT_SYMBOL((pDis->fPrefix & DISPREFIX_SEG)
943 ? DIS_FMT_SEL_FROM_REG(pDis->idxSegPrefix)
944 : DIS_FMT_SEL_FROM_REG(DISSELREG_DS),
945 pDis->uAddrMode == DISCPUMODE_64BIT
946 ? (uint64_t)off2
947 : pDis->uAddrMode == DISCPUMODE_32BIT
948 ? (uint32_t)off2
949 : (uint16_t)off2,
950 " = ",
951 ')');
952 else
953 PUT_C(')');
954 }
955 }
956
957 if (DISUSE_IS_EFFECTIVE_ADDR(fUse))
958 {
959 if (pfnGetSymbol && !fBase && !(fUse & (DISUSE_INDEX | DISUSE_RIPDISPLACEMENT32)) && off2 != 0)
960 PUT_SYMBOL((pDis->fPrefix & DISPREFIX_SEG)
961 ? DIS_FMT_SEL_FROM_REG(pDis->idxSegPrefix)
962 : DIS_FMT_SEL_FROM_REG(DISSELREG_DS),
963 pDis->uAddrMode == DISCPUMODE_64BIT
964 ? (uint64_t)off2
965 : pDis->uAddrMode == DISCPUMODE_32BIT
966 ? (uint32_t)off2
967 : (uint16_t)off2,
968 " (=",
969 ')');
970 PUT_C(']');
971 }
972 break;
973 }
974
975 case 'F': /* Eflags register (0 - popf/pushf only, avoided in adjustments above). */
976 AssertFailed();
977 break;
978
979 case 'I': /* Immediate data (ParseImmByte, ParseImmByteSX, ParseImmV, ParseImmUshort, ParseImmZ). */
980 Assert(*pszFmt == 'b' || *pszFmt == 'v' || *pszFmt == 'w' || *pszFmt == 'z'); pszFmt++;
981 switch (pParam->fUse & ( DISUSE_IMMEDIATE8 | DISUSE_IMMEDIATE16 | DISUSE_IMMEDIATE32 | DISUSE_IMMEDIATE64
982 | DISUSE_IMMEDIATE16_SX8 | DISUSE_IMMEDIATE32_SX8 | DISUSE_IMMEDIATE64_SX8))
983 {
984 case DISUSE_IMMEDIATE8:
985 if ( (fFlags & DIS_FMT_FLAGS_STRICT)
986 && ( (pOp->fParam1 >= OP_PARM_REG_GEN8_START && pOp->fParam1 <= OP_PARM_REG_GEN8_END)
987 || (pOp->fParam2 >= OP_PARM_REG_GEN8_START && pOp->fParam2 <= OP_PARM_REG_GEN8_END))
988 )
989 PUT_SZ("strict byte ");
990 PUT_NUM_8(pParam->uValue);
991 break;
992
993 case DISUSE_IMMEDIATE16:
994 if ( pDis->uCpuMode != pDis->uOpMode
995 || ( (fFlags & DIS_FMT_FLAGS_STRICT)
996 && ( (int8_t)pParam->uValue == (int16_t)pParam->uValue
997 || (pOp->fParam1 >= OP_PARM_REG_GEN16_START && pOp->fParam1 <= OP_PARM_REG_GEN16_END)
998 || (pOp->fParam2 >= OP_PARM_REG_GEN16_START && pOp->fParam2 <= OP_PARM_REG_GEN16_END))
999 )
1000 )
1001 {
1002 if (OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_b)
1003 PUT_SZ_STRICT("strict byte ", "byte ");
1004 else if ( OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_v
1005 || OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_z)
1006 PUT_SZ_STRICT("strict word ", "word ");
1007 }
1008 PUT_NUM_16(pParam->uValue);
1009 break;
1010
1011 case DISUSE_IMMEDIATE16_SX8:
1012 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1013 || pDis->pCurInstr->uOpcode != OP_PUSH)
1014 PUT_SZ_STRICT("strict byte ", "byte ");
1015 else
1016 PUT_SZ("word ");
1017 PUT_NUM_16(pParam->uValue);
1018 break;
1019
1020 case DISUSE_IMMEDIATE32:
1021 if ( pDis->uOpMode != (pDis->uCpuMode == DISCPUMODE_16BIT ? DISCPUMODE_16BIT : DISCPUMODE_32BIT) /* not perfect */
1022 || ( (fFlags & DIS_FMT_FLAGS_STRICT)
1023 && ( (int8_t)pParam->uValue == (int32_t)pParam->uValue
1024 || (pOp->fParam1 >= OP_PARM_REG_GEN32_START && pOp->fParam1 <= OP_PARM_REG_GEN32_END)
1025 || (pOp->fParam2 >= OP_PARM_REG_GEN32_START && pOp->fParam2 <= OP_PARM_REG_GEN32_END))
1026 )
1027 )
1028 {
1029 if (OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_b)
1030 PUT_SZ_STRICT("strict byte ", "byte ");
1031 else if ( OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_v
1032 || OP_PARM_VSUBTYPE(pParam->fParam) == OP_PARM_z)
1033 PUT_SZ_STRICT("strict dword ", "dword ");
1034 }
1035 PUT_NUM_32(pParam->uValue);
1036 if (pDis->uCpuMode == DISCPUMODE_32BIT)
1037 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uValue, " (=", ')');
1038 break;
1039
1040 case DISUSE_IMMEDIATE32_SX8:
1041 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1042 || pDis->pCurInstr->uOpcode != OP_PUSH)
1043 PUT_SZ_STRICT("strict byte ", "byte ");
1044 else
1045 PUT_SZ("dword ");
1046 PUT_NUM_32(pParam->uValue);
1047 break;
1048
1049 case DISUSE_IMMEDIATE64_SX8:
1050 if ( !(pDis->fPrefix & DISPREFIX_OPSIZE)
1051 || pDis->pCurInstr->uOpcode != OP_PUSH)
1052 PUT_SZ_STRICT("strict byte ", "byte ");
1053 else
1054 PUT_SZ("qword ");
1055 PUT_NUM_64(pParam->uValue);
1056 break;
1057
1058 case DISUSE_IMMEDIATE64:
1059 PUT_NUM_64(pParam->uValue);
1060 break;
1061
1062 default:
1063 AssertFailed();
1064 break;
1065 }
1066 break;
1067
1068 case 'J': /* Relative jump offset (ParseImmBRel + ParseImmVRel). */
1069 {
1070 int32_t offDisplacement;
1071 Assert(iParam == 1);
1072 bool fPrefix = (fFlags & DIS_FMT_FLAGS_STRICT)
1073 && pOp->uOpcode != OP_CALL
1074 && pOp->uOpcode != OP_LOOP
1075 && pOp->uOpcode != OP_LOOPE
1076 && pOp->uOpcode != OP_LOOPNE
1077 && pOp->uOpcode != OP_JECXZ;
1078 if (pOp->uOpcode == OP_CALL)
1079 fFlags &= ~DIS_FMT_FLAGS_RELATIVE_BRANCH;
1080
1081 if (pParam->fUse & DISUSE_IMMEDIATE8_REL)
1082 {
1083 if (fPrefix)
1084 PUT_SZ("short ");
1085 offDisplacement = (int8_t)pParam->uValue;
1086 Assert(*pszFmt == 'b'); pszFmt++;
1087
1088 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1089 PUT_NUM_S8(offDisplacement);
1090 }
1091 else if (pParam->fUse & DISUSE_IMMEDIATE16_REL)
1092 {
1093 if (fPrefix)
1094 PUT_SZ("near ");
1095 offDisplacement = (int16_t)pParam->uValue;
1096 Assert(*pszFmt == 'v'); pszFmt++;
1097
1098 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1099 PUT_NUM_S16(offDisplacement);
1100 }
1101 else
1102 {
1103 if (fPrefix)
1104 PUT_SZ("near ");
1105 offDisplacement = (int32_t)pParam->uValue;
1106 Assert(pParam->fUse & (DISUSE_IMMEDIATE32_REL | DISUSE_IMMEDIATE64_REL));
1107 Assert(*pszFmt == 'v'); pszFmt++;
1108
1109 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1110 PUT_NUM_S32(offDisplacement);
1111 }
1112 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1113 PUT_SZ(" (");
1114
1115 RTUINTPTR uTrgAddr = pDis->uInstrAddr + pDis->cbInstr + offDisplacement;
1116 if (pDis->uCpuMode == DISCPUMODE_16BIT)
1117 PUT_NUM_16(uTrgAddr);
1118 else if (pDis->uCpuMode == DISCPUMODE_32BIT)
1119 PUT_NUM_32(uTrgAddr);
1120 else
1121 PUT_NUM_64(uTrgAddr);
1122
1123 if (fFlags & DIS_FMT_FLAGS_RELATIVE_BRANCH)
1124 {
1125 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), uTrgAddr, " = ", ' ');
1126 PUT_C(')');
1127 }
1128 else
1129 PUT_SYMBOL(DIS_FMT_SEL_FROM_REG(DISSELREG_CS), uTrgAddr, " (", ')');
1130 break;
1131 }
1132
1133 case 'A': /* Direct (jump/call) address (ParseImmAddr). */
1134 {
1135 Assert(*pszFmt == 'p'); pszFmt++;
1136 PUT_FAR();
1137 PUT_SIZE_OVERRIDE();
1138 PUT_SEGMENT_OVERRIDE();
1139 off = 0;
1140 int rc = VERR_SYMBOL_NOT_FOUND;
1141 switch (pParam->fUse & (DISUSE_IMMEDIATE_ADDR_16_16 | DISUSE_IMMEDIATE_ADDR_16_32 | DISUSE_DISPLACEMENT64 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT16))
1142 {
1143 case DISUSE_IMMEDIATE_ADDR_16_16:
1144 PUT_NUM_16(pParam->uValue >> 16);
1145 PUT_C(':');
1146 PUT_NUM_16(pParam->uValue);
1147 if (pfnGetSymbol)
1148 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1149 break;
1150 case DISUSE_IMMEDIATE_ADDR_16_32:
1151 PUT_NUM_16(pParam->uValue >> 32);
1152 PUT_C(':');
1153 PUT_NUM_32(pParam->uValue);
1154 if (pfnGetSymbol)
1155 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1156 break;
1157 case DISUSE_DISPLACEMENT16:
1158 PUT_NUM_16(pParam->uValue);
1159 if (pfnGetSymbol)
1160 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1161 break;
1162 case DISUSE_DISPLACEMENT32:
1163 PUT_NUM_32(pParam->uValue);
1164 if (pfnGetSymbol)
1165 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1166 break;
1167 case DISUSE_DISPLACEMENT64:
1168 PUT_NUM_64(pParam->uValue);
1169 if (pfnGetSymbol)
1170 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), (uint64_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1171 break;
1172 default:
1173 AssertFailed();
1174 break;
1175 }
1176
1177 PUT_SYMBOL_TWO(rc, " [", ']');
1178 break;
1179 }
1180
1181 case 'O': /* No ModRM byte (ParseImmAddr). */
1182 {
1183 Assert(*pszFmt == 'b' || *pszFmt == 'v'); pszFmt++;
1184 PUT_FAR();
1185 PUT_SIZE_OVERRIDE();
1186 PUT_C('[');
1187 PUT_SEGMENT_OVERRIDE();
1188 off = 0;
1189 int rc = VERR_SYMBOL_NOT_FOUND;
1190 switch (pParam->fUse & (DISUSE_IMMEDIATE_ADDR_16_16 | DISUSE_IMMEDIATE_ADDR_16_32 | DISUSE_DISPLACEMENT64 | DISUSE_DISPLACEMENT32 | DISUSE_DISPLACEMENT16))
1191 {
1192 case DISUSE_IMMEDIATE_ADDR_16_16:
1193 PUT_NUM_16(pParam->uValue >> 16);
1194 PUT_C(':');
1195 PUT_NUM_16(pParam->uValue);
1196 if (pfnGetSymbol)
1197 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint16_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1198 break;
1199 case DISUSE_IMMEDIATE_ADDR_16_32:
1200 PUT_NUM_16(pParam->uValue >> 32);
1201 PUT_C(':');
1202 PUT_NUM_32(pParam->uValue);
1203 if (pfnGetSymbol)
1204 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_VALUE(pParam->uValue >> 16), (uint32_t)pParam->uValue, szSymbol, sizeof(szSymbol), &off, pvUser);
1205 break;
1206 case DISUSE_DISPLACEMENT16:
1207 PUT_NUM_16(pParam->uDisp.i16);
1208 if (pfnGetSymbol)
1209 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u16, szSymbol, sizeof(szSymbol), &off, pvUser);
1210 break;
1211 case DISUSE_DISPLACEMENT32:
1212 PUT_NUM_32(pParam->uDisp.i32);
1213 if (pfnGetSymbol)
1214 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u32, szSymbol, sizeof(szSymbol), &off, pvUser);
1215 break;
1216 case DISUSE_DISPLACEMENT64:
1217 PUT_NUM_64(pParam->uDisp.i64);
1218 if (pfnGetSymbol)
1219 rc = pfnGetSymbol(pDis, DIS_FMT_SEL_FROM_REG(DISSELREG_CS), pParam->uDisp.u64, szSymbol, sizeof(szSymbol), &off, pvUser);
1220 break;
1221 default:
1222 AssertFailed();
1223 break;
1224 }
1225 PUT_C(']');
1226
1227 PUT_SYMBOL_TWO(rc, " (", ')');
1228 break;
1229 }
1230
1231 case 'X': /* DS:SI (ParseXb, ParseXv). */
1232 case 'Y': /* ES:DI (ParseYb, ParseYv). */
1233 {
1234 Assert(*pszFmt == 'b' || *pszFmt == 'v'); pszFmt++;
1235 PUT_FAR();
1236 PUT_SIZE_OVERRIDE();
1237 PUT_C('[');
1238 if (pParam->fUse & DISUSE_POINTER_DS_BASED)
1239 PUT_SZ("ds:");
1240 else
1241 PUT_SZ("es:");
1242
1243 size_t cchReg;
1244 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
1245 PUT_STR(pszReg, cchReg);
1246 PUT_C(']');
1247 break;
1248 }
1249
1250 case 'e': /* Register based on operand size (e.g. %eAX, %eAH) (ParseFixedReg). */
1251 {
1252 Assert(RT_C_IS_ALPHA(pszFmt[0]) && RT_C_IS_ALPHA(pszFmt[1]) && !RT_C_IS_ALPHA(pszFmt[2]));
1253 pszFmt += 2;
1254 size_t cchReg;
1255 const char *pszReg = disasmFormatYasmBaseReg(pDis, pParam, &cchReg);
1256 PUT_STR(pszReg, cchReg);
1257 break;
1258 }
1259
1260 default:
1261 AssertMsgFailed(("%c%s!\n", ch, pszFmt));
1262 break;
1263 }
1264 AssertMsg(*pszFmt == ',' || *pszFmt == '\0', ("%c%s\n", ch, pszFmt));
1265 }
1266 else
1267 {
1268 PUT_C(ch);
1269 if (ch == ',')
1270 {
1271 Assert(*pszFmt != ' ');
1272 PUT_C(' ');
1273 switch (++iParam)
1274 {
1275 case 2: pParam = &pDis->Param2; break;
1276 case 3: pParam = &pDis->Param3; break;
1277 case 4: pParam = &pDis->Param4; break;
1278 default: pParam = NULL; break;
1279 }
1280 }
1281 }
1282 } /* while more to format */
1283 }
1284
1285 /*
1286 * Any additional output to the right of the instruction?
1287 */
1288 if (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_RIGHT))
1289 {
1290 /* some up front padding. */
1291 size_t cchPadding = cchOutput - offInstruction;
1292 cchPadding = cchPadding + 1 >= 42 ? 1 : 42 - cchPadding;
1293 PUT_STR(g_szSpaces, cchPadding);
1294
1295 /* comment? */
1296 if (fFlags & (DIS_FMT_FLAGS_BYTES_RIGHT | DIS_FMT_FLAGS_ADDR_RIGHT))
1297 PUT_SZ(";");
1298
1299 /*
1300 * The address?
1301 */
1302 if (fFlags & DIS_FMT_FLAGS_ADDR_RIGHT)
1303 {
1304 PUT_C(' ');
1305#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
1306 if (pDis->uInstrAddr >= _4G)
1307 PUT_NUM(9, "%08x`", (uint32_t)(pDis->uInstrAddr >> 32));
1308#endif
1309 PUT_NUM(8, "%08x", (uint32_t)pDis->uInstrAddr);
1310 }
1311
1312 /*
1313 * Opcode bytes?
1314 */
1315 if (fFlags & DIS_FMT_FLAGS_BYTES_RIGHT)
1316 {
1317 PUT_C(' ');
1318 size_t cchTmp = disFormatBytes(pDis, pszDst, cchDst, fFlags);
1319 cchOutput += cchTmp;
1320 if (cchTmp >= cchDst)
1321 cchTmp = cchDst - (cchDst != 0);
1322 cchDst -= cchTmp;
1323 pszDst += cchTmp;
1324 }
1325 }
1326
1327 /*
1328 * Terminate it - on overflow we'll have reserved one byte for this.
1329 */
1330 if (cchDst > 0)
1331 *pszDst = '\0';
1332 else
1333 Assert(!cchBuf);
1334
1335 /* clean up macros */
1336#undef PUT_PSZ
1337#undef PUT_SZ
1338#undef PUT_STR
1339#undef PUT_C
1340 return cchOutput;
1341}
1342
1343
1344/**
1345 * Formats the current instruction in Yasm (/ Nasm) style.
1346 *
1347 * This is a simplified version of DISFormatYasmEx() provided for your convenience.
1348 *
1349 *
1350 * @returns The number of output characters. If this is >= cchBuf, then the content
1351 * of pszBuf will be truncated.
1352 * @param pDis Pointer to the disassembler state.
1353 * @param pszBuf The output buffer.
1354 * @param cchBuf The size of the output buffer.
1355 */
1356DISDECL(size_t) DISFormatYasm(PCDISSTATE pDis, char *pszBuf, size_t cchBuf)
1357{
1358 return DISFormatYasmEx(pDis, pszBuf, cchBuf, 0 /* fFlags */, NULL /* pfnGetSymbol */, NULL /* pvUser */);
1359}
1360
1361
1362/**
1363 * Checks if the encoding of the given disassembled instruction is something we
1364 * can never get YASM to produce.
1365 *
1366 * @returns true if it's odd, false if it isn't.
1367 * @param pDis The disassembler output. The byte fetcher callback will
1368 * be used if present as we might need to fetch opcode
1369 * bytes.
1370 */
1371DISDECL(bool) DISFormatYasmIsOddEncoding(PDISSTATE pDis)
1372{
1373 /*
1374 * Mod rm + SIB: Check for duplicate EBP encodings that yasm won't use for very good reasons.
1375 */
1376 if ( pDis->uAddrMode != DISCPUMODE_16BIT /// @todo correct?
1377 && pDis->ModRM.Bits.Rm == 4
1378 && pDis->ModRM.Bits.Mod != 3)
1379 {
1380 /* No scaled index SIB (index=4), except for ESP. */
1381 if ( pDis->SIB.Bits.Index == 4
1382 && pDis->SIB.Bits.Base != 4)
1383 return true;
1384
1385 /* EBP + displacement */
1386 if ( pDis->ModRM.Bits.Mod != 0
1387 && pDis->SIB.Bits.Base == 5
1388 && pDis->SIB.Bits.Scale == 0)
1389 return true;
1390 }
1391
1392 /*
1393 * Seems to be an instruction alias here, but I cannot find any docs on it... hrmpf!
1394 */
1395 if ( pDis->pCurInstr->uOpcode == OP_SHL
1396 && pDis->ModRM.Bits.Reg == 6)
1397 return true;
1398
1399 /*
1400 * Check for multiple prefixes of the same kind.
1401 */
1402 uint8_t off1stSeg = UINT8_MAX;
1403 uint8_t offOpSize = UINT8_MAX;
1404 uint8_t offAddrSize = UINT8_MAX;
1405 uint32_t fPrefixes = 0;
1406 for (uint32_t offOpcode = 0; offOpcode < RT_ELEMENTS(pDis->abInstr); offOpcode++)
1407 {
1408 uint32_t f;
1409 switch (pDis->abInstr[offOpcode])
1410 {
1411 case 0xf0:
1412 f = DISPREFIX_LOCK;
1413 break;
1414
1415 case 0xf2:
1416 case 0xf3:
1417 f = DISPREFIX_REP; /* yes, both */
1418 break;
1419
1420 case 0x2e:
1421 case 0x3e:
1422 case 0x26:
1423 case 0x36:
1424 case 0x64:
1425 case 0x65:
1426 if (off1stSeg == UINT8_MAX)
1427 off1stSeg = offOpcode;
1428 f = DISPREFIX_SEG;
1429 break;
1430
1431 case 0x66:
1432 if (offOpSize == UINT8_MAX)
1433 offOpSize = offOpcode;
1434 f = DISPREFIX_OPSIZE;
1435 break;
1436
1437 case 0x67:
1438 if (offAddrSize == UINT8_MAX)
1439 offAddrSize = offOpcode;
1440 f = DISPREFIX_ADDRSIZE;
1441 break;
1442
1443 case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
1444 case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
1445 f = pDis->uCpuMode == DISCPUMODE_64BIT ? DISPREFIX_REX : 0;
1446 break;
1447
1448 default:
1449 f = 0;
1450 break;
1451 }
1452 if (!f)
1453 break; /* done */
1454 if (fPrefixes & f)
1455 return true;
1456 fPrefixes |= f;
1457 }
1458
1459 /* segment overrides are fun */
1460 if (fPrefixes & DISPREFIX_SEG)
1461 {
1462 /* no effective address which it may apply to. */
1463 Assert((pDis->fPrefix & DISPREFIX_SEG) || pDis->uCpuMode == DISCPUMODE_64BIT);
1464 if ( !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param1.fUse)
1465 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param2.fUse)
1466 && !DISUSE_IS_EFFECTIVE_ADDR(pDis->Param3.fUse))
1467 return true;
1468
1469 /* Yasm puts the segment prefixes before the operand prefix with no
1470 way of overriding it. */
1471 if (offOpSize < off1stSeg)
1472 return true;
1473 }
1474
1475 /* fixed register + addr override doesn't go down all that well. */
1476 if (fPrefixes & DISPREFIX_ADDRSIZE)
1477 {
1478 Assert(pDis->fPrefix & DISPREFIX_ADDRSIZE);
1479 if ( pDis->pCurInstr->fParam3 == OP_PARM_NONE
1480 && pDis->pCurInstr->fParam2 == OP_PARM_NONE
1481 && ( pDis->pCurInstr->fParam1 >= OP_PARM_REG_GEN32_START
1482 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_GEN32_END))
1483 return true;
1484 }
1485
1486 /* Almost all prefixes are bad for jumps. */
1487 if (fPrefixes)
1488 {
1489 switch (pDis->pCurInstr->uOpcode)
1490 {
1491 /* nop w/ prefix(es). */
1492 case OP_NOP:
1493 return true;
1494
1495 case OP_JMP:
1496 if ( pDis->pCurInstr->fParam1 != OP_PARM_Jb
1497 && pDis->pCurInstr->fParam1 != OP_PARM_Jv)
1498 break;
1499 RT_FALL_THRU();
1500 case OP_JO:
1501 case OP_JNO:
1502 case OP_JC:
1503 case OP_JNC:
1504 case OP_JE:
1505 case OP_JNE:
1506 case OP_JBE:
1507 case OP_JNBE:
1508 case OP_JS:
1509 case OP_JNS:
1510 case OP_JP:
1511 case OP_JNP:
1512 case OP_JL:
1513 case OP_JNL:
1514 case OP_JLE:
1515 case OP_JNLE:
1516 /** @todo branch hinting 0x2e/0x3e... */
1517 return true;
1518 }
1519
1520 }
1521
1522 /* All but the segment prefix is bad news for push/pop. */
1523 if (fPrefixes & ~DISPREFIX_SEG)
1524 {
1525 switch (pDis->pCurInstr->uOpcode)
1526 {
1527 case OP_POP:
1528 case OP_PUSH:
1529 if ( pDis->pCurInstr->fParam1 >= OP_PARM_REG_SEG_START
1530 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_SEG_END)
1531 return true;
1532 if ( (fPrefixes & ~DISPREFIX_OPSIZE)
1533 && pDis->pCurInstr->fParam1 >= OP_PARM_REG_GEN32_START
1534 && pDis->pCurInstr->fParam1 <= OP_PARM_REG_GEN32_END)
1535 return true;
1536 break;
1537
1538 case OP_POPA:
1539 case OP_POPF:
1540 case OP_PUSHA:
1541 case OP_PUSHF:
1542 if (fPrefixes & ~DISPREFIX_OPSIZE)
1543 return true;
1544 break;
1545 }
1546 }
1547
1548 /* Implicit 8-bit register instructions doesn't mix with operand size. */
1549 if ( (fPrefixes & DISPREFIX_OPSIZE)
1550 && ( ( pDis->pCurInstr->fParam1 == OP_PARM_Gb /* r8 */
1551 && pDis->pCurInstr->fParam2 == OP_PARM_Eb /* r8/mem8 */)
1552 || ( pDis->pCurInstr->fParam2 == OP_PARM_Gb /* r8 */
1553 && pDis->pCurInstr->fParam1 == OP_PARM_Eb /* r8/mem8 */))
1554 )
1555 {
1556 switch (pDis->pCurInstr->uOpcode)
1557 {
1558 case OP_ADD:
1559 case OP_OR:
1560 case OP_ADC:
1561 case OP_SBB:
1562 case OP_AND:
1563 case OP_SUB:
1564 case OP_XOR:
1565 case OP_CMP:
1566 return true;
1567 default:
1568 break;
1569 }
1570 }
1571
1572 /* Instructions taking no address or operand which thus may be annoyingly
1573 difficult to format for yasm. */
1574 if (fPrefixes)
1575 {
1576 switch (pDis->pCurInstr->uOpcode)
1577 {
1578 case OP_STI:
1579 case OP_STC:
1580 case OP_CLI:
1581 case OP_CLD:
1582 case OP_CLC:
1583 case OP_INT:
1584 case OP_INT3:
1585 case OP_INTO:
1586 case OP_HLT:
1587 /** @todo Many more to can be added here. */
1588 return true;
1589 default:
1590 break;
1591 }
1592 }
1593
1594 /* FPU and other instructions that ignores operand size override. */
1595 if (fPrefixes & DISPREFIX_OPSIZE)
1596 {
1597 switch (pDis->pCurInstr->uOpcode)
1598 {
1599 /* FPU: */
1600 case OP_FIADD:
1601 case OP_FIMUL:
1602 case OP_FISUB:
1603 case OP_FISUBR:
1604 case OP_FIDIV:
1605 case OP_FIDIVR:
1606 /** @todo there are many more. */
1607 return true;
1608
1609 case OP_MOV:
1610 /** @todo could be that we're not disassembling these correctly. */
1611 if (pDis->pCurInstr->fParam1 == OP_PARM_Sw)
1612 return true;
1613 /** @todo what about the other way? */
1614 break;
1615
1616 default:
1617 break;
1618 }
1619 }
1620
1621
1622 /*
1623 * Check for the version of xyz reg,reg instruction that the assembler doesn't use.
1624 *
1625 * For example:
1626 * expected: 1aee sbb ch, dh ; SBB r8, r/m8
1627 * yasm: 18F5 sbb ch, dh ; SBB r/m8, r8
1628 */
1629 if (pDis->ModRM.Bits.Mod == 3 /* reg,reg */)
1630 {
1631 switch (pDis->pCurInstr->uOpcode)
1632 {
1633 case OP_ADD:
1634 case OP_OR:
1635 case OP_ADC:
1636 case OP_SBB:
1637 case OP_AND:
1638 case OP_SUB:
1639 case OP_XOR:
1640 case OP_CMP:
1641 if ( ( pDis->pCurInstr->fParam1 == OP_PARM_Gb /* r8 */
1642 && pDis->pCurInstr->fParam2 == OP_PARM_Eb /* r8/mem8 */)
1643 || ( pDis->pCurInstr->fParam1 == OP_PARM_Gv /* rX */
1644 && pDis->pCurInstr->fParam2 == OP_PARM_Ev /* rX/memX */))
1645 return true;
1646
1647 /* 82 (see table A-6). */
1648 if (pDis->bOpCode == 0x82)
1649 return true;
1650 break;
1651
1652 /* ff /0, fe /0, ff /1, fe /0 */
1653 case OP_DEC:
1654 case OP_INC:
1655 return true;
1656
1657 case OP_POP:
1658 case OP_PUSH:
1659 Assert(pDis->bOpCode == 0x8f);
1660 return true;
1661
1662 case OP_MOV:
1663 if ( pDis->bOpCode == 0x8a
1664 || pDis->bOpCode == 0x8b)
1665 return true;
1666 break;
1667
1668 default:
1669 break;
1670 }
1671 }
1672
1673 /* shl eax,1 will be assembled to the form without the immediate byte. */
1674 if ( pDis->pCurInstr->fParam2 == OP_PARM_Ib
1675 && (uint8_t)pDis->Param2.uValue == 1)
1676 {
1677 switch (pDis->pCurInstr->uOpcode)
1678 {
1679 case OP_SHL:
1680 case OP_SHR:
1681 case OP_SAR:
1682 case OP_RCL:
1683 case OP_RCR:
1684 case OP_ROL:
1685 case OP_ROR:
1686 return true;
1687 }
1688 }
1689
1690 /* And some more - see table A-6. */
1691 if (pDis->bOpCode == 0x82)
1692 {
1693 switch (pDis->pCurInstr->uOpcode)
1694 {
1695 case OP_ADD:
1696 case OP_OR:
1697 case OP_ADC:
1698 case OP_SBB:
1699 case OP_AND:
1700 case OP_SUB:
1701 case OP_XOR:
1702 case OP_CMP:
1703 return true;
1704 break;
1705 }
1706 }
1707
1708
1709 /* check for REX.X = 1 without SIB. */
1710
1711 /* Yasm encodes setnbe al with /2 instead of /0 like the AMD manual
1712 says (intel doesn't appear to care). */
1713 switch (pDis->pCurInstr->uOpcode)
1714 {
1715 case OP_SETO:
1716 case OP_SETNO:
1717 case OP_SETC:
1718 case OP_SETNC:
1719 case OP_SETE:
1720 case OP_SETNE:
1721 case OP_SETBE:
1722 case OP_SETNBE:
1723 case OP_SETS:
1724 case OP_SETNS:
1725 case OP_SETP:
1726 case OP_SETNP:
1727 case OP_SETL:
1728 case OP_SETNL:
1729 case OP_SETLE:
1730 case OP_SETNLE:
1731 AssertMsg(pDis->bOpCode >= 0x90 && pDis->bOpCode <= 0x9f, ("%#x\n", pDis->bOpCode));
1732 if (pDis->ModRM.Bits.Reg != 2)
1733 return true;
1734 break;
1735 }
1736
1737 /*
1738 * The MOVZX reg32,mem16 instruction without an operand size prefix
1739 * doesn't quite make sense...
1740 */
1741 if ( pDis->pCurInstr->uOpcode == OP_MOVZX
1742 && pDis->bOpCode == 0xB7
1743 && (pDis->uCpuMode == DISCPUMODE_16BIT) != !!(fPrefixes & DISPREFIX_OPSIZE))
1744 return true;
1745
1746 return false;
1747}
1748
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