1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * disasm.c where all the _work_ gets done in the Netwide Disassembler
53 * Flags that go into the `segment' field of `insn' structures
56 #define SEG_RELATIVE 1
63 #define SEG_SIGNED 128
70 uint8_t osize
; /* Operand size */
71 uint8_t asize
; /* Address size */
72 uint8_t osp
; /* Operand size prefix present */
73 uint8_t asp
; /* Address size prefix present */
74 uint8_t rep
; /* Rep prefix present */
75 uint8_t seg
; /* Segment override prefix present */
76 uint8_t wait
; /* WAIT "prefix" present */
77 uint8_t lock
; /* Lock prefix present */
78 uint8_t vex
[3]; /* VEX prefix present */
79 uint8_t vex_c
; /* VEX "class" (VEX, XOP, ...) */
80 uint8_t vex_m
; /* VEX.M field */
82 uint8_t vex_lp
; /* VEX.LP fields */
83 uint32_t rex
; /* REX prefix present */
86 #define getu8(x) (*(uint8_t *)(x))
88 /* Littleendian CPU which can handle unaligned references */
89 #define getu16(x) (*(uint16_t *)(x))
90 #define getu32(x) (*(uint32_t *)(x))
91 #define getu64(x) (*(uint64_t *)(x))
93 static uint16_t getu16(uint8_t *data
)
95 return (uint16_t)data
[0] + ((uint16_t)data
[1] << 8);
97 static uint32_t getu32(uint8_t *data
)
99 return (uint32_t)getu16(data
) + ((uint32_t)getu16(data
+2) << 16);
101 static uint64_t getu64(uint8_t *data
)
103 return (uint64_t)getu32(data
) + ((uint64_t)getu32(data
+4) << 32);
107 #define gets8(x) ((int8_t)getu8(x))
108 #define gets16(x) ((int16_t)getu16(x))
109 #define gets32(x) ((int32_t)getu32(x))
110 #define gets64(x) ((int64_t)getu64(x))
112 /* Important: regval must already have been adjusted for rex extensions */
113 static enum reg_enum
whichreg(int32_t regflags
, int regval
, int rex
)
115 if (!(regflags
& (REGISTER
|REGMEM
)))
116 return 0; /* Registers not permissible?! */
118 regflags
|= REGISTER
;
120 if (!(REG_AL
& ~regflags
))
122 if (!(REG_AX
& ~regflags
))
124 if (!(REG_EAX
& ~regflags
))
126 if (!(REG_RAX
& ~regflags
))
128 if (!(REG_DL
& ~regflags
))
130 if (!(REG_DX
& ~regflags
))
132 if (!(REG_EDX
& ~regflags
))
134 if (!(REG_RDX
& ~regflags
))
136 if (!(REG_CL
& ~regflags
))
138 if (!(REG_CX
& ~regflags
))
140 if (!(REG_ECX
& ~regflags
))
142 if (!(REG_RCX
& ~regflags
))
144 if (!(FPU0
& ~regflags
))
146 if (!(XMM0
& ~regflags
))
148 if (!(YMM0
& ~regflags
))
150 if (!(REG_CS
& ~regflags
))
151 return (regval
== 1) ? R_CS
: 0;
152 if (!(REG_DESS
& ~regflags
))
153 return (regval
== 0 || regval
== 2
154 || regval
== 3 ? nasm_rd_sreg
[regval
] : 0);
155 if (!(REG_FSGS
& ~regflags
))
156 return (regval
== 4 || regval
== 5 ? nasm_rd_sreg
[regval
] : 0);
157 if (!(REG_SEG67
& ~regflags
))
158 return (regval
== 6 || regval
== 7 ? nasm_rd_sreg
[regval
] : 0);
160 /* All the entries below look up regval in an 16-entry array */
161 if (regval
< 0 || regval
> 15)
164 if (!(REG8
& ~regflags
)) {
165 if (rex
& (REX_P
|REX_NH
))
166 return nasm_rd_reg8_rex
[regval
];
168 return nasm_rd_reg8
[regval
];
170 if (!(REG16
& ~regflags
))
171 return nasm_rd_reg16
[regval
];
172 if (!(REG32
& ~regflags
))
173 return nasm_rd_reg32
[regval
];
174 if (!(REG64
& ~regflags
))
175 return nasm_rd_reg64
[regval
];
176 if (!(REG_SREG
& ~regflags
))
177 return nasm_rd_sreg
[regval
& 7]; /* Ignore REX */
178 if (!(REG_CREG
& ~regflags
))
179 return nasm_rd_creg
[regval
];
180 if (!(REG_DREG
& ~regflags
))
181 return nasm_rd_dreg
[regval
];
182 if (!(REG_TREG
& ~regflags
)) {
184 return 0; /* TR registers are ill-defined with rex */
185 return nasm_rd_treg
[regval
];
187 if (!(FPUREG
& ~regflags
))
188 return nasm_rd_fpureg
[regval
& 7]; /* Ignore REX */
189 if (!(MMXREG
& ~regflags
))
190 return nasm_rd_mmxreg
[regval
& 7]; /* Ignore REX */
191 if (!(XMMREG
& ~regflags
))
192 return nasm_rd_xmmreg
[regval
];
193 if (!(YMMREG
& ~regflags
))
194 return nasm_rd_ymmreg
[regval
];
200 * Process a DREX suffix
202 static uint8_t *do_drex(uint8_t *data
, insn
*ins
)
204 uint8_t drex
= *data
++;
205 operand
*dst
= &ins
->oprs
[ins
->drexdst
];
207 if ((drex
& 8) != ((ins
->rex
& REX_OC
) ? 8 : 0))
208 return NULL
; /* OC0 mismatch */
209 ins
->rex
= (ins
->rex
& ~7) | (drex
& 7);
211 dst
->segment
= SEG_RMREG
;
212 dst
->basereg
= drex
>> 4;
218 * Process an effective address (ModRM) specification.
220 static uint8_t *do_ea(uint8_t *data
, int modrm
, int asize
,
221 int segsize
, operand
* op
, insn
*ins
)
223 int mod
, rm
, scale
, index
, base
;
227 mod
= (modrm
>> 6) & 03;
230 if (mod
!= 3 && rm
== 4 && asize
!= 16)
233 if (ins
->rex
& REX_D
) {
234 data
= do_drex(data
, ins
);
240 if (mod
== 3) { /* pure register version */
241 op
->basereg
= rm
+(rex
& REX_B
? 8 : 0);
242 op
->segment
|= SEG_RMREG
;
251 * <mod> specifies the displacement size (none, byte or
252 * word), and <rm> specifies the register combination.
253 * Exception: mod=0,rm=6 does not specify [BP] as one might
254 * expect, but instead specifies [disp16].
256 op
->indexreg
= op
->basereg
= -1;
257 op
->scale
= 1; /* always, in 16 bits */
288 if (rm
== 6 && mod
== 0) { /* special case */
292 mod
= 2; /* fake disp16 */
296 op
->segment
|= SEG_NODISP
;
299 op
->segment
|= SEG_DISP8
;
300 op
->offset
= (int8_t)*data
++;
303 op
->segment
|= SEG_DISP16
;
304 op
->offset
= *data
++;
305 op
->offset
|= ((unsigned)*data
++) << 8;
311 * Once again, <mod> specifies displacement size (this time
312 * none, byte or *dword*), while <rm> specifies the base
313 * register. Again, [EBP] is missing, replaced by a pure
314 * disp32 (this time that's mod=0,rm=*5*) in 32-bit mode,
315 * and RIP-relative addressing in 64-bit mode.
318 * indicates not a single base register, but instead the
319 * presence of a SIB byte...
321 int a64
= asize
== 64;
326 op
->basereg
= nasm_rd_reg64
[rm
| ((rex
& REX_B
) ? 8 : 0)];
328 op
->basereg
= nasm_rd_reg32
[rm
| ((rex
& REX_B
) ? 8 : 0)];
330 if (rm
== 5 && mod
== 0) {
332 op
->eaflags
|= EAF_REL
;
333 op
->segment
|= SEG_RELATIVE
;
334 mod
= 2; /* fake disp32 */
338 op
->disp_size
= asize
;
341 mod
= 2; /* fake disp32 */
344 if (rm
== 4) { /* process SIB */
345 scale
= (sib
>> 6) & 03;
346 index
= (sib
>> 3) & 07;
349 op
->scale
= 1 << scale
;
351 if (index
== 4 && !(rex
& REX_X
))
352 op
->indexreg
= -1; /* ESP/RSP cannot be an index */
354 op
->indexreg
= nasm_rd_reg64
[index
| ((rex
& REX_X
) ? 8 : 0)];
356 op
->indexreg
= nasm_rd_reg32
[index
| ((rex
& REX_X
) ? 8 : 0)];
358 if (base
== 5 && mod
== 0) {
360 mod
= 2; /* Fake disp32 */
362 op
->basereg
= nasm_rd_reg64
[base
| ((rex
& REX_B
) ? 8 : 0)];
364 op
->basereg
= nasm_rd_reg32
[base
| ((rex
& REX_B
) ? 8 : 0)];
372 op
->segment
|= SEG_NODISP
;
375 op
->segment
|= SEG_DISP8
;
376 op
->offset
= gets8(data
);
380 op
->segment
|= SEG_DISP32
;
381 op
->offset
= gets32(data
);
390 * Determine whether the instruction template in t corresponds to the data
391 * stream in data. Return the number of bytes matched if so.
393 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
395 static int matches(const struct itemplate
*t
, uint8_t *data
,
396 const struct prefix_info
*prefix
, int segsize
, insn
*ins
)
398 uint8_t *r
= (uint8_t *)(t
->code
);
399 uint8_t *origdata
= data
;
400 bool a_used
= false, o_used
= false;
401 enum prefixes drep
= 0;
402 enum prefixes dwait
= 0;
403 uint8_t lock
= prefix
->lock
;
404 int osize
= prefix
->osize
;
405 int asize
= prefix
->asize
;
408 struct operand
*opx
, *opy
;
410 int s_field_for
= -1; /* No 144/154 series code encountered */
412 int regmask
= (segsize
== 64) ? 15 : 7;
414 for (i
= 0; i
< MAX_OPERANDS
; i
++) {
415 ins
->oprs
[i
].segment
= ins
->oprs
[i
].disp_size
=
416 (segsize
== 64 ? SEG_64BIT
: segsize
== 32 ? SEG_32BIT
: 0);
419 ins
->rex
= prefix
->rex
;
420 memset(ins
->prefixes
, 0, sizeof ins
->prefixes
);
422 if (t
->flags
& (segsize
== 64 ? IF_NOLONG
: IF_LONG
))
425 if (prefix
->rep
== 0xF2)
427 else if (prefix
->rep
== 0xF3)
430 dwait
= prefix
->wait
? P_WAIT
: 0;
432 while ((c
= *r
++) != 0) {
433 op1
= (c
& 3) + ((opex
& 1) << 2);
434 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
435 opx
= &ins
->oprs
[op1
];
436 opy
= &ins
->oprs
[op2
];
457 int t
= *r
++, d
= *data
++;
458 if (d
< t
|| d
> t
+ 7)
461 opx
->basereg
= (d
-t
)+
462 (ins
->rex
& REX_B
? 8 : 0);
463 opx
->segment
|= SEG_RMREG
;
470 opx
->offset
= (int8_t)*data
++;
471 opx
->segment
|= SEG_SIGNED
;
475 opx
->offset
= *data
++;
479 opx
->offset
= *data
++;
483 opx
->offset
= getu16(data
);
489 opx
->offset
= getu32(data
);
492 opx
->offset
= getu16(data
);
495 if (segsize
!= asize
)
496 opx
->disp_size
= asize
;
501 opx
->offset
= getu32(data
);
508 opx
->offset
= getu16(data
);
514 opx
->offset
= getu32(data
);
520 opx
->offset
= getu64(data
);
528 opx
->offset
= gets8(data
++);
529 opx
->segment
|= SEG_RELATIVE
;
533 opx
->offset
= getu64(data
);
538 opx
->offset
= gets16(data
);
540 opx
->segment
|= SEG_RELATIVE
;
541 opx
->segment
&= ~SEG_32BIT
;
545 opx
->segment
|= SEG_RELATIVE
;
547 opx
->offset
= gets16(data
);
549 opx
->segment
&= ~(SEG_32BIT
|SEG_64BIT
);
550 } else if (osize
== 32) {
551 opx
->offset
= gets32(data
);
553 opx
->segment
&= ~SEG_64BIT
;
554 opx
->segment
|= SEG_32BIT
;
556 if (segsize
!= osize
) {
558 (opx
->type
& ~SIZE_MASK
)
559 | ((osize
== 16) ? BITS16
: BITS32
);
564 opx
->offset
= gets32(data
);
566 opx
->segment
|= SEG_32BIT
| SEG_RELATIVE
;
575 opx
->segment
|= SEG_RMREG
;
576 data
= do_ea(data
, modrm
, asize
, segsize
, opy
, ins
);
579 opx
->basereg
= ((modrm
>> 3) & 7) + (ins
->rex
& REX_R
? 8 : 0);
584 if (s_field_for
== op1
) {
585 opx
->offset
= gets8(data
);
588 opx
->offset
= getu16(data
);
595 s_field_for
= (*data
& 0x02) ? op1
: -1;
596 if ((*data
++ & ~0x02) != *r
++)
601 if (s_field_for
== op1
) {
602 opx
->offset
= gets8(data
);
605 opx
->offset
= getu32(data
);
616 ins
->rex
|= REX_D
|REX_OC
;
621 data
= do_drex(data
, ins
);
628 uint8_t ximm
= *data
++;
630 ins
->oprs
[c
>> 3].basereg
= (ximm
>> 4) & regmask
;
631 ins
->oprs
[c
>> 3].segment
|= SEG_RMREG
;
632 ins
->oprs
[c
& 7].offset
= ximm
& 15;
638 uint8_t ximm
= *data
++;
644 ins
->oprs
[c
>> 4].basereg
= (ximm
>> 4) & regmask
;
645 ins
->oprs
[c
>> 4].segment
|= SEG_RMREG
;
651 uint8_t ximm
= *data
++;
654 ins
->oprs
[c
].basereg
= (ximm
>> 4) & regmask
;
655 ins
->oprs
[c
].segment
|= SEG_RMREG
;
669 if (((modrm
>> 3) & 07) != (c
& 07))
670 return false; /* spare field doesn't match up */
671 data
= do_ea(data
, modrm
, asize
, segsize
, opy
, ins
);
682 if ((prefix
->rex
& (REX_V
|REX_D
|REX_P
)) != REX_V
)
685 if ((vexm
& 0x1f) != prefix
->vex_m
)
688 switch (vexwlp
& 030) {
690 if (prefix
->rex
& REX_W
)
694 if (!(prefix
->rex
& REX_W
))
698 case 020: /* VEX.W is a don't care */
705 if ((vexwlp
& 007) != prefix
->vex_lp
)
708 opx
->segment
|= SEG_RMREG
;
709 opx
->basereg
= prefix
->vex_v
;
719 if ((prefix
->rex
& (REX_V
|REX_D
|REX_P
)) != REX_V
)
722 if ((vexm
& 0x1f) != prefix
->vex_m
)
725 switch (vexwlp
& 030) {
727 if (ins
->rex
& REX_W
)
731 if (!(ins
->rex
& REX_W
))
735 break; /* Need to do anything special here? */
738 if ((vexwlp
& 007) != prefix
->vex_lp
)
741 if (prefix
->vex_v
!= 0)
763 if (asize
!= segsize
)
777 if (prefix
->rex
& REX_B
)
782 if (prefix
->rex
& REX_X
)
787 if (prefix
->rex
& REX_R
)
792 if (prefix
->rex
& REX_W
)
811 if (osize
!= (segsize
== 16) ? 16 : 32)
818 ins
->rex
|= REX_W
; /* 64-bit only instruction */
824 if (!(ins
->rex
& (REX_P
|REX_W
)) || osize
!= 64)
835 int t
= *r
++, d
= *data
++;
836 if (d
< t
|| d
> t
+ 15)
839 ins
->condition
= d
- t
;
849 if (prefix
->rep
!= 0xF2)
855 if (prefix
->rep
!= 0xF3)
880 if (prefix
->wait
!= 0x9B)
886 ins
->oprs
[0].basereg
= (*data
++ >> 3) & 7;
890 if (prefix
->osp
|| prefix
->rep
)
895 if (!prefix
->osp
|| prefix
->rep
)
901 if (prefix
->osp
|| prefix
->rep
!= 0xf2)
907 if (prefix
->osp
|| prefix
->rep
!= 0xf3)
935 return false; /* Unknown code */
939 if (!vex_ok
&& (ins
->rex
& REX_V
))
942 /* REX cannot be combined with DREX or VEX */
943 if ((ins
->rex
& (REX_D
|REX_V
)) && (prefix
->rex
& REX_P
))
947 * Check for unused rep or a/o prefixes.
949 for (i
= 0; i
< t
->operands
; i
++) {
950 if (ins
->oprs
[i
].segment
!= SEG_RMREG
)
955 if (ins
->prefixes
[PPS_LREP
])
957 ins
->prefixes
[PPS_LREP
] = P_LOCK
;
960 if (ins
->prefixes
[PPS_LREP
])
962 ins
->prefixes
[PPS_LREP
] = drep
;
964 ins
->prefixes
[PPS_WAIT
] = dwait
;
966 if (osize
!= ((segsize
== 16) ? 16 : 32)) {
967 enum prefixes pfx
= 0;
981 if (ins
->prefixes
[PPS_OSIZE
])
983 ins
->prefixes
[PPS_OSIZE
] = pfx
;
986 if (!a_used
&& asize
!= segsize
) {
987 if (ins
->prefixes
[PPS_ASIZE
])
989 ins
->prefixes
[PPS_ASIZE
] = asize
== 16 ? P_A16
: P_A32
;
992 /* Fix: check for redundant REX prefixes */
994 return data
- origdata
;
997 /* Condition names for disassembly, sorted by x86 code */
998 static const char * const condition_name
[16] = {
999 "o", "no", "c", "nc", "z", "nz", "na", "a",
1000 "s", "ns", "pe", "po", "l", "nl", "ng", "g"
1003 int32_t disasm(uint8_t *data
, char *output
, int outbufsize
, int segsize
,
1004 int32_t offset
, int autosync
, uint32_t prefer
)
1006 const struct itemplate
* const *p
, * const *best_p
;
1007 const struct disasm_index
*ix
;
1009 int length
, best_length
= 0;
1011 int i
, slen
, colon
, n
;
1015 uint32_t goodness
, best
;
1017 struct prefix_info prefix
;
1020 memset(&ins
, 0, sizeof ins
);
1023 * Scan for prefixes.
1025 memset(&prefix
, 0, sizeof prefix
);
1026 prefix
.asize
= segsize
;
1027 prefix
.osize
= (segsize
== 64) ? 32 : segsize
;
1034 while (!end_prefix
) {
1038 prefix
.rep
= *data
++;
1042 prefix
.wait
= *data
++;
1046 prefix
.lock
= *data
++;
1050 segover
= "cs", prefix
.seg
= *data
++;
1053 segover
= "ss", prefix
.seg
= *data
++;
1056 segover
= "ds", prefix
.seg
= *data
++;
1059 segover
= "es", prefix
.seg
= *data
++;
1062 segover
= "fs", prefix
.seg
= *data
++;
1065 segover
= "gs", prefix
.seg
= *data
++;
1069 prefix
.osize
= (segsize
== 16) ? 32 : 16;
1070 prefix
.osp
= *data
++;
1073 prefix
.asize
= (segsize
== 32) ? 16 : 32;
1074 prefix
.asp
= *data
++;
1079 if (segsize
== 64 || (data
[1] & 0xc0) == 0xc0) {
1080 prefix
.vex
[0] = *data
++;
1081 prefix
.vex
[1] = *data
++;
1084 prefix
.vex_c
= RV_VEX
;
1086 if (prefix
.vex
[0] == 0xc4) {
1087 prefix
.vex
[2] = *data
++;
1088 prefix
.rex
|= (~prefix
.vex
[1] >> 5) & 7; /* REX_RXB */
1089 prefix
.rex
|= (prefix
.vex
[2] >> (7-3)) & REX_W
;
1090 prefix
.vex_m
= prefix
.vex
[1] & 0x1f;
1091 prefix
.vex_v
= (~prefix
.vex
[2] >> 3) & 15;
1092 prefix
.vex_lp
= prefix
.vex
[2] & 7;
1094 prefix
.rex
|= (~prefix
.vex
[1] >> (7-2)) & REX_R
;
1096 prefix
.vex_v
= (~prefix
.vex
[1] >> 3) & 15;
1097 prefix
.vex_lp
= prefix
.vex
[1] & 7;
1100 ix
= itable_vex
[RV_VEX
][prefix
.vex_m
][prefix
.vex_lp
];
1106 if ((data
[1] & 030) != 0 &&
1107 (segsize
== 64 || (data
[1] & 0xc0) == 0xc0)) {
1108 prefix
.vex
[0] = *data
++;
1109 prefix
.vex
[1] = *data
++;
1110 prefix
.vex
[2] = *data
++;
1113 prefix
.vex_c
= RV_XOP
;
1115 prefix
.rex
|= (~prefix
.vex
[1] >> 5) & 7; /* REX_RXB */
1116 prefix
.rex
|= (prefix
.vex
[2] >> (7-3)) & REX_W
;
1117 prefix
.vex_m
= prefix
.vex
[1] & 0x1f;
1118 prefix
.vex_v
= (~prefix
.vex
[2] >> 3) & 15;
1119 prefix
.vex_lp
= prefix
.vex
[2] & 7;
1121 ix
= itable_vex
[RV_XOP
][prefix
.vex_m
][prefix
.vex_lp
];
1142 if (segsize
== 64) {
1143 prefix
.rex
= *data
++;
1144 if (prefix
.rex
& REX_W
)
1156 best
= -1; /* Worst possible */
1158 best_pref
= INT_MAX
;
1161 return 0; /* No instruction table at all... */
1165 while (ix
->n
== -1) {
1166 ix
= (const struct disasm_index
*)ix
->p
+ *dp
++;
1169 p
= (const struct itemplate
* const *)ix
->p
;
1170 for (n
= ix
->n
; n
; n
--, p
++) {
1171 if ((length
= matches(*p
, data
, &prefix
, segsize
, &tmp_ins
))) {
1174 * Final check to make sure the types of r/m match up.
1175 * XXX: Need to make sure this is actually correct.
1177 for (i
= 0; i
< (*p
)->operands
; i
++) {
1178 if (!((*p
)->opd
[i
] & SAME_AS
) &&
1180 /* If it's a mem-only EA but we have a
1182 ((tmp_ins
.oprs
[i
].segment
& SEG_RMREG
) &&
1183 !(MEMORY
& ~(*p
)->opd
[i
])) ||
1184 /* If it's a reg-only EA but we have a memory
1186 (!(tmp_ins
.oprs
[i
].segment
& SEG_RMREG
) &&
1187 !(REG_EA
& ~(*p
)->opd
[i
]) &&
1188 !((*p
)->opd
[i
] & REG_SMASK
)) ||
1189 /* Register type mismatch (eg FS vs REG_DESS):
1191 ((((*p
)->opd
[i
] & (REGISTER
| FPUREG
)) ||
1192 (tmp_ins
.oprs
[i
].segment
& SEG_RMREG
)) &&
1193 !whichreg((*p
)->opd
[i
],
1194 tmp_ins
.oprs
[i
].basereg
, tmp_ins
.rex
))
1202 * Note: we always prefer instructions which incorporate
1203 * prefixes in the instructions themselves. This is to allow
1204 * e.g. PAUSE to be preferred to REP NOP, and deal with
1205 * MMX/SSE instructions where prefixes are used to select
1206 * between MMX and SSE register sets or outright opcode
1211 goodness
= ((*p
)->flags
& IF_PFMASK
) ^ prefer
;
1213 for (i
= 0; i
< MAXPREFIX
; i
++)
1214 if (tmp_ins
.prefixes
[i
])
1216 if (nprefix
< best_pref
||
1217 (nprefix
== best_pref
&& goodness
< best
)) {
1218 /* This is the best one found so far */
1221 best_pref
= nprefix
;
1222 best_length
= length
;
1230 return 0; /* no instruction was matched */
1232 /* Pick the best match */
1234 length
= best_length
;
1238 /* TODO: snprintf returns the value that the string would have if
1239 * the buffer were long enough, and not the actual length of
1240 * the returned string, so each instance of using the return
1241 * value of snprintf should actually be checked to assure that
1242 * the return value is "sane." Maybe a macro wrapper could
1243 * be used for that purpose.
1245 for (i
= 0; i
< MAXPREFIX
; i
++) {
1246 const char *prefix
= prefix_name(ins
.prefixes
[i
]);
1248 slen
+= snprintf(output
+slen
, outbufsize
-slen
, "%s ", prefix
);
1252 if (i
>= FIRST_COND_OPCODE
)
1253 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s%s",
1254 nasm_insn_names
[i
], condition_name
[ins
.condition
]);
1256 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1257 nasm_insn_names
[i
]);
1260 length
+= data
- origdata
; /* fix up for prefixes */
1261 for (i
= 0; i
< (*p
)->operands
; i
++) {
1262 opflags_t t
= (*p
)->opd
[i
];
1263 const operand
*o
= &ins
.oprs
[i
];
1267 o
= &ins
.oprs
[t
& ~SAME_AS
];
1268 t
= (*p
)->opd
[t
& ~SAME_AS
];
1271 output
[slen
++] = (colon
? ':' : i
== 0 ? ' ' : ',');
1274 if (o
->segment
& SEG_RELATIVE
) {
1275 offs
+= offset
+ length
;
1277 * sort out wraparound
1279 if (!(o
->segment
& (SEG_32BIT
|SEG_64BIT
)))
1281 else if (segsize
!= 64)
1285 * add sync marker, if autosync is on
1296 if ((t
& (REGISTER
| FPUREG
)) ||
1297 (o
->segment
& SEG_RMREG
)) {
1299 reg
= whichreg(t
, o
->basereg
, ins
.rex
);
1301 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "to ");
1302 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1303 nasm_reg_names
[reg
-EXPR_REG_START
]);
1304 } else if (!(UNITY
& ~t
)) {
1305 output
[slen
++] = '1';
1306 } else if (t
& IMMEDIATE
) {
1309 snprintf(output
+ slen
, outbufsize
- slen
, "byte ");
1310 if (o
->segment
& SEG_SIGNED
) {
1313 output
[slen
++] = '-';
1315 output
[slen
++] = '+';
1317 } else if (t
& BITS16
) {
1319 snprintf(output
+ slen
, outbufsize
- slen
, "word ");
1320 } else if (t
& BITS32
) {
1322 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1323 } else if (t
& BITS64
) {
1325 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1326 } else if (t
& NEAR
) {
1328 snprintf(output
+ slen
, outbufsize
- slen
, "near ");
1329 } else if (t
& SHORT
) {
1331 snprintf(output
+ slen
, outbufsize
- slen
, "short ");
1334 snprintf(output
+ slen
, outbufsize
- slen
, "0x%"PRIx64
"",
1336 } else if (!(MEM_OFFS
& ~t
)) {
1338 snprintf(output
+ slen
, outbufsize
- slen
,
1339 "[%s%s%s0x%"PRIx64
"]",
1340 (segover
? segover
: ""),
1341 (segover
? ":" : ""),
1342 (o
->disp_size
== 64 ? "qword " :
1343 o
->disp_size
== 32 ? "dword " :
1344 o
->disp_size
== 16 ? "word " : ""), offs
);
1346 } else if (!(REGMEM
& ~t
)) {
1347 int started
= false;
1350 snprintf(output
+ slen
, outbufsize
- slen
, "byte ");
1353 snprintf(output
+ slen
, outbufsize
- slen
, "word ");
1356 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1359 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1362 snprintf(output
+ slen
, outbufsize
- slen
, "tword ");
1365 snprintf(output
+ slen
, outbufsize
- slen
, "oword ");
1368 snprintf(output
+ slen
, outbufsize
- slen
, "yword ");
1370 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "far ");
1373 snprintf(output
+ slen
, outbufsize
- slen
, "near ");
1374 output
[slen
++] = '[';
1376 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1377 (o
->disp_size
== 64 ? "qword " :
1378 o
->disp_size
== 32 ? "dword " :
1379 o
->disp_size
== 16 ? "word " :
1381 if (o
->eaflags
& EAF_REL
)
1382 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "rel ");
1385 snprintf(output
+ slen
, outbufsize
- slen
, "%s:",
1389 if (o
->basereg
!= -1) {
1390 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1391 nasm_reg_names
[(o
->basereg
-EXPR_REG_START
)]);
1394 if (o
->indexreg
!= -1) {
1396 output
[slen
++] = '+';
1397 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1398 nasm_reg_names
[(o
->indexreg
-EXPR_REG_START
)]);
1401 snprintf(output
+ slen
, outbufsize
- slen
, "*%d",
1407 if (o
->segment
& SEG_DISP8
) {
1409 uint8_t offset
= offs
;
1410 if ((int8_t)offset
< 0) {
1417 snprintf(output
+ slen
, outbufsize
- slen
, "%s0x%"PRIx8
"",
1419 } else if (o
->segment
& SEG_DISP16
) {
1421 uint16_t offset
= offs
;
1422 if ((int16_t)offset
< 0 && started
) {
1426 prefix
= started
? "+" : "";
1429 snprintf(output
+ slen
, outbufsize
- slen
,
1430 "%s0x%"PRIx16
"", prefix
, offset
);
1431 } else if (o
->segment
& SEG_DISP32
) {
1432 if (prefix
.asize
== 64) {
1434 uint64_t offset
= (int64_t)(int32_t)offs
;
1435 if ((int32_t)offs
< 0 && started
) {
1439 prefix
= started
? "+" : "";
1442 snprintf(output
+ slen
, outbufsize
- slen
,
1443 "%s0x%"PRIx64
"", prefix
, offset
);
1446 uint32_t offset
= offs
;
1447 if ((int32_t) offset
< 0 && started
) {
1451 prefix
= started
? "+" : "";
1454 snprintf(output
+ slen
, outbufsize
- slen
,
1455 "%s0x%"PRIx32
"", prefix
, offset
);
1458 output
[slen
++] = ']';
1461 snprintf(output
+ slen
, outbufsize
- slen
, "<operand%d>",
1465 output
[slen
] = '\0';
1466 if (segover
) { /* unused segment override */
1468 int count
= slen
+ 1;
1470 p
[count
+ 3] = p
[count
];
1471 strncpy(output
, segover
, 2);
1478 * This is called when we don't have a complete instruction. If it
1479 * is a standalone *single-byte* prefix show it as such, otherwise
1480 * print it as a literal.
1482 int32_t eatbyte(uint8_t *data
, char *output
, int outbufsize
, int segsize
)
1484 uint8_t byte
= *data
;
1485 const char *str
= NULL
;
1519 str
= (segsize
== 16) ? "o32" : "o16";
1522 str
= (segsize
== 32) ? "a16" : "a32";
1540 if (segsize
== 64) {
1541 snprintf(output
, outbufsize
, "rex%s%s%s%s%s",
1542 (byte
== REX_P
) ? "" : ".",
1543 (byte
& REX_W
) ? "w" : "",
1544 (byte
& REX_R
) ? "r" : "",
1545 (byte
& REX_X
) ? "x" : "",
1546 (byte
& REX_B
) ? "b" : "");
1549 /* else fall through */
1551 snprintf(output
, outbufsize
, "db 0x%02x", byte
);
1556 strcpy(output
, str
);