NASM 2.00rc1
[nasm/avx512.git] / disasm.c
blob18911f0e227d6a668219b881bdff01b69ae48b28
1 /* disasm.c where all the _work_ gets done in the Netwide Disassembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 27/iii/95 by Simon Tatham
9 */
11 #include "compiler.h"
13 #include <stdio.h>
14 #include <string.h>
15 #include <limits.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "disasm.h"
20 #include "sync.h"
21 #include "insns.h"
23 #include "names.c"
26 * Flags that go into the `segment' field of `insn' structures
27 * during disassembly.
29 #define SEG_RELATIVE 1
30 #define SEG_32BIT 2
31 #define SEG_RMREG 4
32 #define SEG_DISP8 8
33 #define SEG_DISP16 16
34 #define SEG_DISP32 32
35 #define SEG_NODISP 64
36 #define SEG_SIGNED 128
37 #define SEG_64BIT 256
39 #include "regdis.c"
42 * Prefix information
44 struct prefix_info {
45 uint8_t osize; /* Operand size */
46 uint8_t asize; /* Address size */
47 uint8_t osp; /* Operand size prefix present */
48 uint8_t asp; /* Address size prefix present */
49 uint8_t rep; /* Rep prefix present */
50 uint8_t seg; /* Segment override prefix present */
51 uint8_t lock; /* Lock prefix present */
52 uint8_t rex; /* Rex prefix present */
55 #define getu8(x) (*(uint8_t *)(x))
56 #if X86_MEMORY
57 /* Littleendian CPU which can handle unaligned references */
58 #define getu16(x) (*(uint16_t *)(x))
59 #define getu32(x) (*(uint32_t *)(x))
60 #define getu64(x) (*(uint64_t *)(x))
61 #else
62 static uint16_t getu16(uint8_t *data)
64 return (uint16_t)data[0] + ((uint16_t)data[1] << 8);
66 static uint32_t getu32(uint8_t *data)
68 return (uint32_t)getu16(data) + ((uint32_t)getu16(data+2) << 16);
70 static uint64_t getu64(uint8_t *data)
72 return (uint64_t)getu32(data) + ((uint64_t)getu32(data+4) << 32);
74 #endif
76 #define gets8(x) ((int8_t)getu8(x))
77 #define gets16(x) ((int16_t)getu16(x))
78 #define gets32(x) ((int32_t)getu32(x))
79 #define gets64(x) ((int64_t)getu64(x))
81 /* Important: regval must already have been adjusted for rex extensions */
82 static enum reg_enum whichreg(int32_t regflags, int regval, int rex)
84 if (!(regflags & (REGISTER|REGMEM)))
85 return 0; /* Registers not permissible?! */
87 regflags |= REGISTER;
89 if (!(REG_AL & ~regflags))
90 return R_AL;
91 if (!(REG_AX & ~regflags))
92 return R_AX;
93 if (!(REG_EAX & ~regflags))
94 return R_EAX;
95 if (!(REG_RAX & ~regflags))
96 return R_RAX;
97 if (!(REG_DL & ~regflags))
98 return R_DL;
99 if (!(REG_DX & ~regflags))
100 return R_DX;
101 if (!(REG_EDX & ~regflags))
102 return R_EDX;
103 if (!(REG_RDX & ~regflags))
104 return R_RDX;
105 if (!(REG_CL & ~regflags))
106 return R_CL;
107 if (!(REG_CX & ~regflags))
108 return R_CX;
109 if (!(REG_ECX & ~regflags))
110 return R_ECX;
111 if (!(REG_RCX & ~regflags))
112 return R_RCX;
113 if (!(FPU0 & ~regflags))
114 return R_ST0;
115 if (!(REG_CS & ~regflags))
116 return (regval == 1) ? R_CS : 0;
117 if (!(REG_DESS & ~regflags))
118 return (regval == 0 || regval == 2
119 || regval == 3 ? rd_sreg[regval] : 0);
120 if (!(REG_FSGS & ~regflags))
121 return (regval == 4 || regval == 5 ? rd_sreg[regval] : 0);
122 if (!(REG_SEG67 & ~regflags))
123 return (regval == 6 || regval == 7 ? rd_sreg[regval] : 0);
125 /* All the entries below look up regval in an 16-entry array */
126 if (regval < 0 || regval > 15)
127 return 0;
129 if (!(REG8 & ~regflags)) {
130 if (rex & REX_P)
131 return rd_reg8_rex[regval];
132 else
133 return rd_reg8[regval];
135 if (!(REG16 & ~regflags))
136 return rd_reg16[regval];
137 if (!(REG32 & ~regflags))
138 return rd_reg32[regval];
139 if (!(REG64 & ~regflags))
140 return rd_reg64[regval];
141 if (!(REG_SREG & ~regflags))
142 return rd_sreg[regval & 7]; /* Ignore REX */
143 if (!(REG_CREG & ~regflags))
144 return rd_creg[regval];
145 if (!(REG_DREG & ~regflags))
146 return rd_dreg[regval];
147 if (!(REG_TREG & ~regflags)) {
148 if (rex & REX_P)
149 return 0; /* TR registers are ill-defined with rex */
150 return rd_treg[regval];
152 if (!(FPUREG & ~regflags))
153 return rd_fpureg[regval & 7]; /* Ignore REX */
154 if (!(MMXREG & ~regflags))
155 return rd_mmxreg[regval & 7]; /* Ignore REX */
156 if (!(XMMREG & ~regflags))
157 return rd_xmmreg[regval];
159 return 0;
162 static const char *whichcond(int condval)
164 static int conds[] = {
165 C_O, C_NO, C_C, C_NC, C_Z, C_NZ, C_NA, C_A,
166 C_S, C_NS, C_PE, C_PO, C_L, C_NL, C_NG, C_G
168 return conditions[conds[condval]];
172 * Process a DREX suffix
174 static uint8_t *do_drex(uint8_t *data, insn *ins)
176 uint8_t drex = *data++;
177 operand *dst = &ins->oprs[ins->drexdst];
179 if ((drex & 8) != ((ins->rex & REX_OC) ? 8 : 0))
180 return NULL; /* OC0 mismatch */
181 ins->rex = (ins->rex & ~7) | (drex & 7);
183 dst->segment = SEG_RMREG;
184 dst->basereg = drex >> 4;
185 return data;
190 * Process an effective address (ModRM) specification.
192 static uint8_t *do_ea(uint8_t *data, int modrm, int asize,
193 int segsize, operand * op, insn *ins)
195 int mod, rm, scale, index, base;
196 int rex;
197 uint8_t sib = 0;
199 mod = (modrm >> 6) & 03;
200 rm = modrm & 07;
202 if (mod != 3 && rm == 4 && asize != 16)
203 sib = *data++;
205 if (ins->rex & REX_D) {
206 data = do_drex(data, ins);
207 if (!data)
208 return NULL;
210 rex = ins->rex;
212 if (mod == 3) { /* pure register version */
213 op->basereg = rm+(rex & REX_B ? 8 : 0);
214 op->segment |= SEG_RMREG;
215 return data;
218 op->disp_size = 0;
219 op->eaflags = 0;
221 if (asize == 16) {
223 * <mod> specifies the displacement size (none, byte or
224 * word), and <rm> specifies the register combination.
225 * Exception: mod=0,rm=6 does not specify [BP] as one might
226 * expect, but instead specifies [disp16].
228 op->indexreg = op->basereg = -1;
229 op->scale = 1; /* always, in 16 bits */
230 switch (rm) {
231 case 0:
232 op->basereg = R_BX;
233 op->indexreg = R_SI;
234 break;
235 case 1:
236 op->basereg = R_BX;
237 op->indexreg = R_DI;
238 break;
239 case 2:
240 op->basereg = R_BP;
241 op->indexreg = R_SI;
242 break;
243 case 3:
244 op->basereg = R_BP;
245 op->indexreg = R_DI;
246 break;
247 case 4:
248 op->basereg = R_SI;
249 break;
250 case 5:
251 op->basereg = R_DI;
252 break;
253 case 6:
254 op->basereg = R_BP;
255 break;
256 case 7:
257 op->basereg = R_BX;
258 break;
260 if (rm == 6 && mod == 0) { /* special case */
261 op->basereg = -1;
262 if (segsize != 16)
263 op->disp_size = 16;
264 mod = 2; /* fake disp16 */
266 switch (mod) {
267 case 0:
268 op->segment |= SEG_NODISP;
269 break;
270 case 1:
271 op->segment |= SEG_DISP8;
272 op->offset = (int8_t)*data++;
273 break;
274 case 2:
275 op->segment |= SEG_DISP16;
276 op->offset = *data++;
277 op->offset |= ((unsigned)*data++) << 8;
278 break;
280 return data;
281 } else {
283 * Once again, <mod> specifies displacement size (this time
284 * none, byte or *dword*), while <rm> specifies the base
285 * register. Again, [EBP] is missing, replaced by a pure
286 * disp32 (this time that's mod=0,rm=*5*) in 32-bit mode,
287 * and RIP-relative addressing in 64-bit mode.
289 * However, rm=4
290 * indicates not a single base register, but instead the
291 * presence of a SIB byte...
293 int a64 = asize == 64;
295 op->indexreg = -1;
297 if (a64)
298 op->basereg = rd_reg64[rm | ((rex & REX_B) ? 8 : 0)];
299 else
300 op->basereg = rd_reg32[rm | ((rex & REX_B) ? 8 : 0)];
302 if (rm == 5 && mod == 0) {
303 if (segsize == 64) {
304 op->eaflags |= EAF_REL;
305 op->segment |= SEG_RELATIVE;
306 mod = 2; /* fake disp32 */
309 if (asize != 64)
310 op->disp_size = asize;
312 op->basereg = -1;
313 mod = 2; /* fake disp32 */
316 if (rm == 4) { /* process SIB */
317 scale = (sib >> 6) & 03;
318 index = (sib >> 3) & 07;
319 base = sib & 07;
321 op->scale = 1 << scale;
323 if (index == 4)
324 op->indexreg = -1; /* ESP/RSP/R12 cannot be an index */
325 else if (a64)
326 op->indexreg = rd_reg64[index | ((rex & REX_X) ? 8 : 0)];
327 else
328 op->indexreg = rd_reg64[index | ((rex & REX_X) ? 8 : 0)];
330 if (base == 5 && mod == 0) {
331 op->basereg = -1;
332 mod = 2; /* Fake disp32 */
333 } else if (a64)
334 op->basereg = rd_reg64[base | ((rex & REX_B) ? 8 : 0)];
335 else
336 op->basereg = rd_reg32[base | ((rex & REX_B) ? 8 : 0)];
338 if (segsize == 16)
339 op->disp_size = 32;
342 switch (mod) {
343 case 0:
344 op->segment |= SEG_NODISP;
345 break;
346 case 1:
347 op->segment |= SEG_DISP8;
348 op->offset = gets8(data);
349 data++;
350 break;
351 case 2:
352 op->segment |= SEG_DISP32;
353 op->offset = getu32(data);
354 data += 4;
355 break;
357 return data;
362 * Determine whether the instruction template in t corresponds to the data
363 * stream in data. Return the number of bytes matched if so.
365 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
367 static int matches(const struct itemplate *t, uint8_t *data,
368 const struct prefix_info *prefix, int segsize, insn *ins)
370 uint8_t *r = (uint8_t *)(t->code);
371 uint8_t *origdata = data;
372 bool a_used = false, o_used = false;
373 enum prefixes drep = 0;
374 uint8_t lock = prefix->lock;
375 int osize = prefix->osize;
376 int asize = prefix->asize;
377 int i, c;
378 struct operand *opx;
380 for (i = 0; i < MAX_OPERANDS; i++) {
381 ins->oprs[i].segment = ins->oprs[i].disp_size =
382 (segsize == 64 ? SEG_64BIT : segsize == 32 ? SEG_32BIT : 0);
384 ins->condition = -1;
385 ins->rex = prefix->rex;
386 memset(ins->prefixes, 0, sizeof ins->prefixes);
388 if (t->flags & (segsize == 64 ? IF_NOLONG : IF_LONG))
389 return false;
391 if (prefix->rep == 0xF2)
392 drep = P_REPNE;
393 else if (prefix->rep == 0xF3)
394 drep = P_REP;
396 while ((c = *r++) != 0) {
397 opx = &ins->oprs[c & 3];
399 switch (c) {
400 case 01:
401 case 02:
402 case 03:
403 while (c--)
404 if (*r++ != *data++)
405 return false;
406 break;
408 case 04:
409 switch (*data++) {
410 case 0x07:
411 ins->oprs[0].basereg = 0;
412 break;
413 case 0x17:
414 ins->oprs[0].basereg = 2;
415 break;
416 case 0x1F:
417 ins->oprs[0].basereg = 3;
418 break;
419 default:
420 return false;
422 break;
424 case 05:
425 switch (*data++) {
426 case 0xA1:
427 ins->oprs[0].basereg = 4;
428 break;
429 case 0xA9:
430 ins->oprs[0].basereg = 5;
431 break;
432 default:
433 return false;
435 break;
437 case 06:
438 switch (*data++) {
439 case 0x06:
440 ins->oprs[0].basereg = 0;
441 break;
442 case 0x0E:
443 ins->oprs[0].basereg = 1;
444 break;
445 case 0x16:
446 ins->oprs[0].basereg = 2;
447 break;
448 case 0x1E:
449 ins->oprs[0].basereg = 3;
450 break;
451 default:
452 return false;
454 break;
456 case 07:
457 switch (*data++) {
458 case 0xA0:
459 ins->oprs[0].basereg = 4;
460 break;
461 case 0xA8:
462 ins->oprs[0].basereg = 5;
463 break;
464 default:
465 return false;
467 break;
469 case4(010):
471 int t = *r++, d = *data++;
472 if (d < t || d > t + 7)
473 return false;
474 else {
475 opx->basereg = (d-t)+
476 (ins->rex & REX_B ? 8 : 0);
477 opx->segment |= SEG_RMREG;
479 break;
482 case4(014):
483 opx->offset = (int8_t)*data++;
484 opx->segment |= SEG_SIGNED;
485 break;
487 case4(020):
488 opx->offset = *data++;
489 break;
491 case4(024):
492 opx->offset = *data++;
493 break;
495 case4(030):
496 opx->offset = getu16(data);
497 data += 2;
498 break;
500 case4(034):
501 if (osize == 32) {
502 opx->offset = getu32(data);
503 data += 4;
504 } else {
505 opx->offset = getu16(data);
506 data += 2;
508 if (segsize != asize)
509 opx->disp_size = asize;
510 break;
512 case4(040):
513 opx->offset = getu32(data);
514 data += 4;
515 break;
517 case4(044):
518 switch (asize) {
519 case 16:
520 opx->offset = getu16(data);
521 data += 2;
522 if (segsize != 16)
523 opx->disp_size = 16;
524 break;
525 case 32:
526 opx->offset = getu32(data);
527 data += 4;
528 if (segsize == 16)
529 opx->disp_size = 32;
530 break;
531 case 64:
532 opx->offset = getu64(data);
533 opx->disp_size = 64;
534 data += 8;
535 break;
537 break;
539 case4(050):
540 opx->offset = gets8(data++);
541 opx->segment |= SEG_RELATIVE;
542 break;
544 case4(054):
545 opx->offset = getu64(data);
546 data += 8;
547 break;
549 case4(060):
550 opx->offset = gets16(data);
551 data += 2;
552 opx->segment |= SEG_RELATIVE;
553 opx->segment &= ~SEG_32BIT;
554 break;
556 case4(064):
557 opx->segment |= SEG_RELATIVE;
558 if (osize == 16) {
559 opx->offset = getu16(data);
560 data += 2;
561 opx->segment &= ~(SEG_32BIT|SEG_64BIT);
562 } else if (osize == 32) {
563 opx->offset = getu32(data);
564 data += 4;
565 opx->segment &= ~SEG_64BIT;
566 opx->segment |= SEG_32BIT;
568 if (segsize != osize) {
569 opx->type =
570 (opx->type & ~SIZE_MASK)
571 | ((osize == 16) ? BITS16 : BITS32);
573 break;
575 case4(070):
576 opx->offset = getu32(data);
577 data += 4;
578 opx->segment |= SEG_32BIT | SEG_RELATIVE;
579 break;
581 case4(0100):
582 case4(0110):
583 case4(0120):
584 case4(0130):
586 int modrm = *data++;
587 opx->segment |= SEG_RMREG;
588 data = do_ea(data, modrm, asize, segsize,
589 &ins->oprs[(c >> 3) & 3], ins);
590 if (!data)
591 return false;
592 opx->basereg = ((modrm >> 3)&7)+
593 (ins->rex & REX_R ? 8 : 0);
594 break;
597 case4(0140):
598 opx->offset = getu16(data);
599 data += 2;
600 break;
602 case4(0150):
603 opx->offset = getu32(data);
604 data += 4;
605 break;
607 case4(0160):
608 ins->rex |= REX_D;
609 ins->drexdst = c & 3;
610 break;
612 case4(0164):
613 ins->rex |= REX_D|REX_OC;
614 ins->drexdst = c & 3;
615 break;
617 case 0170:
618 if (*data++)
619 return false;
620 break;
622 case 0171:
623 data = do_drex(data, ins);
624 if (!data)
625 return false;
626 break;
628 case4(0200):
629 case4(0204):
630 case4(0210):
631 case4(0214):
632 case4(0220):
633 case4(0224):
634 case4(0230):
635 case4(0234):
637 int modrm = *data++;
638 if (((modrm >> 3) & 07) != (c & 07))
639 return false; /* spare field doesn't match up */
640 data = do_ea(data, modrm, asize, segsize,
641 &ins->oprs[(c >> 3) & 07], ins);
642 if (!data)
643 return false;
644 break;
647 case 0310:
648 if (asize != 16)
649 return false;
650 else
651 a_used = true;
652 break;
654 case 0311:
655 if (asize == 16)
656 return false;
657 else
658 a_used = true;
659 break;
661 case 0312:
662 if (asize != segsize)
663 return false;
664 else
665 a_used = true;
666 break;
668 case 0313:
669 if (asize != 64)
670 return false;
671 else
672 a_used = true;
673 break;
675 case 0314:
676 if (prefix->rex & REX_B)
677 return false;
678 break;
680 case 0315:
681 if (prefix->rex & REX_X)
682 return false;
683 break;
685 case 0316:
686 if (prefix->rex & REX_R)
687 return false;
688 break;
690 case 0317:
691 if (prefix->rex & REX_W)
692 return false;
693 break;
695 case 0320:
696 if (osize != 16)
697 return false;
698 else
699 o_used = true;
700 break;
702 case 0321:
703 if (osize != 32)
704 return false;
705 else
706 o_used = true;
707 break;
709 case 0322:
710 if (osize != (segsize == 16) ? 16 : 32)
711 return false;
712 else
713 o_used = true;
714 break;
716 case 0323:
717 ins->rex |= REX_W; /* 64-bit only instruction */
718 osize = 64;
719 o_used = true;
720 break;
722 case 0324:
723 if (!(ins->rex & (REX_P|REX_W)) || osize != 64)
724 return false;
725 o_used = true;
726 break;
728 case 0330:
730 int t = *r++, d = *data++;
731 if (d < t || d > t + 15)
732 return false;
733 else
734 ins->condition = d - t;
735 break;
738 case 0331:
739 if (prefix->rep)
740 return false;
741 break;
743 case 0332:
744 if (prefix->rep != 0xF2)
745 return false;
746 break;
748 case 0333:
749 if (prefix->rep != 0xF3)
750 return false;
751 drep = 0;
752 break;
754 case 0334:
755 if (lock) {
756 ins->rex |= REX_R;
757 lock = 0;
759 break;
761 case 0335:
762 if (drep == P_REP)
763 drep = P_REPE;
764 break;
766 case 0340:
767 return false;
769 case 0364:
770 if (prefix->osp)
771 return false;
772 break;
774 case 0365:
775 if (prefix->asp)
776 return false;
777 break;
779 case 0366:
780 if (!prefix->osp)
781 return false;
782 o_used = true;
783 break;
785 case 0367:
786 if (!prefix->asp)
787 return false;
788 a_used = true;
789 break;
791 default:
792 return false; /* Unknown code */
796 /* REX cannot be combined with DREX */
797 if ((ins->rex & REX_D) && (prefix->rex))
798 return false;
801 * Check for unused rep or a/o prefixes.
803 for (i = 0; i < t->operands; i++) {
804 if (ins->oprs[i].segment != SEG_RMREG)
805 a_used = true;
808 if (lock) {
809 if (ins->prefixes[PPS_LREP])
810 return false;
811 ins->prefixes[PPS_LREP] = P_LOCK;
813 if (drep) {
814 if (ins->prefixes[PPS_LREP])
815 return false;
816 ins->prefixes[PPS_LREP] = drep;
818 if (!o_used) {
819 if (osize != ((segsize == 16) ? 16 : 32)) {
820 enum prefixes pfx = 0;
822 switch (osize) {
823 case 16:
824 pfx = P_O16;
825 break;
826 case 32:
827 pfx = P_O32;
828 break;
829 case 64:
830 pfx = P_O64;
831 break;
834 if (ins->prefixes[PPS_OSIZE])
835 return false;
836 ins->prefixes[PPS_OSIZE] = pfx;
839 if (!a_used && asize != segsize) {
840 if (ins->prefixes[PPS_ASIZE])
841 return false;
842 ins->prefixes[PPS_ASIZE] = asize == 16 ? P_A16 : P_A32;
845 /* Fix: check for redundant REX prefixes */
847 return data - origdata;
850 int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize,
851 int32_t offset, int autosync, uint32_t prefer)
853 const struct itemplate * const *p, * const *best_p;
854 const struct disasm_index *ix;
855 uint8_t *dp;
856 int length, best_length = 0;
857 char *segover;
858 int i, slen, colon, n;
859 uint8_t *origdata;
860 int works;
861 insn tmp_ins, ins;
862 uint32_t goodness, best;
863 int best_pref;
864 struct prefix_info prefix;
865 bool end_prefix;
867 memset(&ins, 0, sizeof ins);
870 * Scan for prefixes.
872 memset(&prefix, 0, sizeof prefix);
873 prefix.asize = segsize;
874 prefix.osize = (segsize == 64) ? 32 : segsize;
875 segover = NULL;
876 origdata = data;
878 for (end_prefix = false; !end_prefix; ) {
879 switch (*data) {
880 case 0xF2:
881 case 0xF3:
882 prefix.rep = *data++;
883 break;
884 case 0xF0:
885 prefix.lock = *data++;
886 break;
887 case 0x2E:
888 segover = "cs", prefix.seg = *data++;
889 break;
890 case 0x36:
891 segover = "ss", prefix.seg = *data++;
892 break;
893 case 0x3E:
894 segover = "ds", prefix.seg = *data++;
895 break;
896 case 0x26:
897 segover = "es", prefix.seg = *data++;
898 break;
899 case 0x64:
900 segover = "fs", prefix.seg = *data++;
901 break;
902 case 0x65:
903 segover = "gs", prefix.seg = *data++;
904 break;
905 case 0x66:
906 prefix.osize = (segsize == 16) ? 32 : 16;
907 prefix.osp = *data++;
908 break;
909 case 0x67:
910 prefix.asize = (segsize == 32) ? 16 : 32;
911 prefix.asp = *data++;
912 break;
913 default:
914 if (segsize == 64 && (*data & 0xf0) == REX_P) {
915 prefix.rex = *data++;
916 if (prefix.rex & REX_W)
917 prefix.osize = 64;
918 end_prefix = true;
919 } else {
920 end_prefix = true;
925 best = -1; /* Worst possible */
926 best_p = NULL;
927 best_pref = INT_MAX;
929 dp = data;
930 ix = itable + *dp++;
931 while (ix->n == -1) {
932 ix = (const struct disasm_index *)ix->p + *dp++;
935 p = (const struct itemplate * const *)ix->p;
936 for (n = ix->n; n; n--, p++) {
937 if ((length = matches(*p, data, &prefix, segsize, &tmp_ins))) {
938 works = true;
940 * Final check to make sure the types of r/m match up.
941 * XXX: Need to make sure this is actually correct.
943 for (i = 0; i < (*p)->operands; i++) {
944 if (!((*p)->opd[i] & SAME_AS) &&
946 /* If it's a mem-only EA but we have a
947 register, die. */
948 ((tmp_ins.oprs[i].segment & SEG_RMREG) &&
949 !(MEMORY & ~(*p)->opd[i])) ||
950 /* If it's a reg-only EA but we have a memory
951 ref, die. */
952 (!(tmp_ins.oprs[i].segment & SEG_RMREG) &&
953 !(REG_EA & ~(*p)->opd[i]) &&
954 !((*p)->opd[i] & REG_SMASK)) ||
955 /* Register type mismatch (eg FS vs REG_DESS):
956 die. */
957 ((((*p)->opd[i] & (REGISTER | FPUREG)) ||
958 (tmp_ins.oprs[i].segment & SEG_RMREG)) &&
959 !whichreg((*p)->opd[i],
960 tmp_ins.oprs[i].basereg, tmp_ins.rex))
961 )) {
962 works = false;
963 break;
968 * Note: we always prefer instructions which incorporate
969 * prefixes in the instructions themselves. This is to allow
970 * e.g. PAUSE to be preferred to REP NOP, and deal with
971 * MMX/SSE instructions where prefixes are used to select
972 * between MMX and SSE register sets or outright opcode
973 * selection.
975 if (works) {
976 int i, nprefix;
977 goodness = ((*p)->flags & IF_PFMASK) ^ prefer;
978 nprefix = 0;
979 for (i = 0; i < MAXPREFIX; i++)
980 if (tmp_ins.prefixes[i])
981 nprefix++;
982 if (nprefix < best_pref ||
983 (nprefix == best_pref && goodness < best)) {
984 /* This is the best one found so far */
985 best = goodness;
986 best_p = p;
987 best_pref = nprefix;
988 best_length = length;
989 ins = tmp_ins;
995 if (!best_p)
996 return 0; /* no instruction was matched */
998 /* Pick the best match */
999 p = best_p;
1000 length = best_length;
1002 slen = 0;
1004 /* TODO: snprintf returns the value that the string would have if
1005 * the buffer were long enough, and not the actual length of
1006 * the returned string, so each instance of using the return
1007 * value of snprintf should actually be checked to assure that
1008 * the return value is "sane." Maybe a macro wrapper could
1009 * be used for that purpose.
1011 for (i = 0; i < MAXPREFIX; i++)
1012 switch (ins.prefixes[i]) {
1013 case P_LOCK:
1014 slen += snprintf(output + slen, outbufsize - slen, "lock ");
1015 break;
1016 case P_REP:
1017 slen += snprintf(output + slen, outbufsize - slen, "rep ");
1018 break;
1019 case P_REPE:
1020 slen += snprintf(output + slen, outbufsize - slen, "repe ");
1021 break;
1022 case P_REPNE:
1023 slen += snprintf(output + slen, outbufsize - slen, "repne ");
1024 break;
1025 case P_A16:
1026 slen += snprintf(output + slen, outbufsize - slen, "a16 ");
1027 break;
1028 case P_A32:
1029 slen += snprintf(output + slen, outbufsize - slen, "a32 ");
1030 break;
1031 case P_A64:
1032 slen += snprintf(output + slen, outbufsize - slen, "a64 ");
1033 break;
1034 case P_O16:
1035 slen += snprintf(output + slen, outbufsize - slen, "o16 ");
1036 break;
1037 case P_O32:
1038 slen += snprintf(output + slen, outbufsize - slen, "o32 ");
1039 break;
1040 case P_O64:
1041 slen += snprintf(output + slen, outbufsize - slen, "o64 ");
1042 break;
1043 default:
1044 break;
1047 for (i = 0; i < (int)elements(ico); i++)
1048 if ((*p)->opcode == ico[i]) {
1049 slen +=
1050 snprintf(output + slen, outbufsize - slen, "%s%s", icn[i],
1051 whichcond(ins.condition));
1052 break;
1054 if (i >= (int)elements(ico))
1055 slen +=
1056 snprintf(output + slen, outbufsize - slen, "%s",
1057 insn_names[(*p)->opcode]);
1058 colon = false;
1059 length += data - origdata; /* fix up for prefixes */
1060 for (i = 0; i < (*p)->operands; i++) {
1061 opflags_t t = (*p)->opd[i];
1062 const operand *o = &ins.oprs[i];
1063 int64_t offs;
1065 if (t & SAME_AS) {
1066 o = &ins.oprs[t & ~SAME_AS];
1067 t = (*p)->opd[t & ~SAME_AS];
1070 output[slen++] = (colon ? ':' : i == 0 ? ' ' : ',');
1072 offs = o->offset;
1073 if (o->segment & SEG_RELATIVE) {
1074 offs += offset + length;
1076 * sort out wraparound
1078 if (!(o->segment & (SEG_32BIT|SEG_64BIT)))
1079 offs &= 0xffff;
1081 * add sync marker, if autosync is on
1083 if (autosync)
1084 add_sync(offs, 0L);
1087 if (t & COLON)
1088 colon = true;
1089 else
1090 colon = false;
1092 if ((t & (REGISTER | FPUREG)) ||
1093 (o->segment & SEG_RMREG)) {
1094 enum reg_enum reg;
1095 reg = whichreg(t, o->basereg, ins.rex);
1096 if (t & TO)
1097 slen += snprintf(output + slen, outbufsize - slen, "to ");
1098 slen += snprintf(output + slen, outbufsize - slen, "%s",
1099 reg_names[reg - EXPR_REG_START]);
1100 } else if (!(UNITY & ~t)) {
1101 output[slen++] = '1';
1102 } else if (t & IMMEDIATE) {
1103 if (t & BITS8) {
1104 slen +=
1105 snprintf(output + slen, outbufsize - slen, "byte ");
1106 if (o->segment & SEG_SIGNED) {
1107 if (offs < 0) {
1108 offs *= -1;
1109 output[slen++] = '-';
1110 } else
1111 output[slen++] = '+';
1113 } else if (t & BITS16) {
1114 slen +=
1115 snprintf(output + slen, outbufsize - slen, "word ");
1116 } else if (t & BITS32) {
1117 slen +=
1118 snprintf(output + slen, outbufsize - slen, "dword ");
1119 } else if (t & BITS64) {
1120 slen +=
1121 snprintf(output + slen, outbufsize - slen, "qword ");
1122 } else if (t & NEAR) {
1123 slen +=
1124 snprintf(output + slen, outbufsize - slen, "near ");
1125 } else if (t & SHORT) {
1126 slen +=
1127 snprintf(output + slen, outbufsize - slen, "short ");
1129 slen +=
1130 snprintf(output + slen, outbufsize - slen, "0x%"PRIx64"",
1131 offs);
1132 } else if (!(MEM_OFFS & ~t)) {
1133 slen +=
1134 snprintf(output + slen, outbufsize - slen,
1135 "[%s%s%s0x%"PRIx64"]",
1136 (segover ? segover : ""),
1137 (segover ? ":" : ""),
1138 (o->disp_size == 64 ? "qword " :
1139 o->disp_size == 32 ? "dword " :
1140 o->disp_size == 16 ? "word " : ""), offs);
1141 segover = NULL;
1142 } else if (!(REGMEM & ~t)) {
1143 int started = false;
1144 if (t & BITS8)
1145 slen +=
1146 snprintf(output + slen, outbufsize - slen, "byte ");
1147 if (t & BITS16)
1148 slen +=
1149 snprintf(output + slen, outbufsize - slen, "word ");
1150 if (t & BITS32)
1151 slen +=
1152 snprintf(output + slen, outbufsize - slen, "dword ");
1153 if (t & BITS64)
1154 slen +=
1155 snprintf(output + slen, outbufsize - slen, "qword ");
1156 if (t & BITS80)
1157 slen +=
1158 snprintf(output + slen, outbufsize - slen, "tword ");
1159 if (t & BITS128)
1160 slen +=
1161 snprintf(output + slen, outbufsize - slen, "oword ");
1162 if (t & FAR)
1163 slen += snprintf(output + slen, outbufsize - slen, "far ");
1164 if (t & NEAR)
1165 slen +=
1166 snprintf(output + slen, outbufsize - slen, "near ");
1167 output[slen++] = '[';
1168 if (o->disp_size)
1169 slen += snprintf(output + slen, outbufsize - slen, "%s",
1170 (o->disp_size == 64 ? "qword " :
1171 o->disp_size == 32 ? "dword " :
1172 o->disp_size == 16 ? "word " :
1173 ""));
1174 if (o->eaflags & EAF_REL)
1175 slen += snprintf(output + slen, outbufsize - slen, "rel ");
1176 if (segover) {
1177 slen +=
1178 snprintf(output + slen, outbufsize - slen, "%s:",
1179 segover);
1180 segover = NULL;
1182 if (o->basereg != -1) {
1183 slen += snprintf(output + slen, outbufsize - slen, "%s",
1184 reg_names[(o->basereg -
1185 EXPR_REG_START)]);
1186 started = true;
1188 if (o->indexreg != -1) {
1189 if (started)
1190 output[slen++] = '+';
1191 slen += snprintf(output + slen, outbufsize - slen, "%s",
1192 reg_names[(o->indexreg -
1193 EXPR_REG_START)]);
1194 if (o->scale > 1)
1195 slen +=
1196 snprintf(output + slen, outbufsize - slen, "*%d",
1197 o->scale);
1198 started = true;
1202 if (o->segment & SEG_DISP8) {
1203 const char *prefix;
1204 uint8_t offset = offs;
1205 if ((int8_t)offset < 0) {
1206 prefix = "-";
1207 offset = -offset;
1208 } else {
1209 prefix = "+";
1211 slen +=
1212 snprintf(output + slen, outbufsize - slen, "%s0x%"PRIx8"",
1213 prefix, offset);
1214 } else if (o->segment & SEG_DISP16) {
1215 const char *prefix;
1216 uint16_t offset = offs;
1217 if ((int16_t)offset < 0 && started) {
1218 offset = -offset;
1219 prefix = "-";
1220 } else {
1221 prefix = started ? "+" : "";
1223 slen +=
1224 snprintf(output + slen, outbufsize - slen,
1225 "%s0x%"PRIx16"", prefix, offset);
1226 } else if (o->segment & SEG_DISP32) {
1227 if (prefix.asize == 64) {
1228 const char *prefix;
1229 uint64_t offset = (int64_t)(int32_t)offs;
1230 if ((int32_t)offs < 0 && started) {
1231 offset = -offset;
1232 prefix = "-";
1233 } else {
1234 prefix = started ? "+" : "";
1236 slen +=
1237 snprintf(output + slen, outbufsize - slen,
1238 "%s0x%"PRIx64"", prefix, offset);
1239 } else {
1240 const char *prefix;
1241 uint32_t offset = offs;
1242 if ((int32_t) offset < 0 && started) {
1243 offset = -offset;
1244 prefix = "-";
1245 } else {
1246 prefix = started ? "+" : "";
1248 slen +=
1249 snprintf(output + slen, outbufsize - slen,
1250 "%s0x%"PRIx32"", prefix, offset);
1253 output[slen++] = ']';
1254 } else {
1255 slen +=
1256 snprintf(output + slen, outbufsize - slen, "<operand%d>",
1260 output[slen] = '\0';
1261 if (segover) { /* unused segment override */
1262 char *p = output;
1263 int count = slen + 1;
1264 while (count--)
1265 p[count + 3] = p[count];
1266 strncpy(output, segover, 2);
1267 output[2] = ' ';
1269 return length;
1272 int32_t eatbyte(uint8_t *data, char *output, int outbufsize)
1274 snprintf(output, outbufsize, "db 0x%02X", *data);
1275 return 1;