1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2012 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
54 * Flags that go into the `segment' field of `insn' structures
57 #define SEG_RELATIVE 1
64 #define SEG_SIGNED 128
71 uint8_t osize
; /* Operand size */
72 uint8_t asize
; /* Address size */
73 uint8_t osp
; /* Operand size prefix present */
74 uint8_t asp
; /* Address size prefix present */
75 uint8_t rep
; /* Rep prefix present */
76 uint8_t seg
; /* Segment override prefix present */
77 uint8_t wait
; /* WAIT "prefix" present */
78 uint8_t lock
; /* Lock prefix present */
79 uint8_t vex
[3]; /* VEX prefix present */
80 uint8_t vex_c
; /* VEX "class" (VEX, XOP, ...) */
81 uint8_t vex_m
; /* VEX.M field */
83 uint8_t vex_lp
; /* VEX.LP fields */
84 uint32_t rex
; /* REX prefix present */
85 uint8_t evex
[3]; /* EVEX prefix present */
88 #define getu8(x) (*(uint8_t *)(x))
90 /* Littleendian CPU which can handle unaligned references */
91 #define getu16(x) (*(uint16_t *)(x))
92 #define getu32(x) (*(uint32_t *)(x))
93 #define getu64(x) (*(uint64_t *)(x))
95 static uint16_t getu16(uint8_t *data
)
97 return (uint16_t)data
[0] + ((uint16_t)data
[1] << 8);
99 static uint32_t getu32(uint8_t *data
)
101 return (uint32_t)getu16(data
) + ((uint32_t)getu16(data
+2) << 16);
103 static uint64_t getu64(uint8_t *data
)
105 return (uint64_t)getu32(data
) + ((uint64_t)getu32(data
+4) << 32);
109 #define gets8(x) ((int8_t)getu8(x))
110 #define gets16(x) ((int16_t)getu16(x))
111 #define gets32(x) ((int32_t)getu32(x))
112 #define gets64(x) ((int64_t)getu64(x))
114 /* Important: regval must already have been adjusted for rex extensions */
115 static enum reg_enum
whichreg(opflags_t regflags
, int regval
, int rex
)
119 static const struct {
122 } specific_registers
[] = {
148 if (!(regflags
& (REGISTER
|REGMEM
)))
149 return 0; /* Registers not permissible?! */
151 regflags
|= REGISTER
;
153 for (i
= 0; i
< ARRAY_SIZE(specific_registers
); i
++)
154 if (!(specific_registers
[i
].flags
& ~regflags
))
155 return specific_registers
[i
].reg
;
157 /* All the entries below look up regval in an 16-entry array */
158 if (regval
< 0 || regval
> (rex
& REX_EV
? 31 : 15))
161 #define GET_REGISTER(__array, __index) \
162 ((size_t)(__index) < (size_t)ARRAY_SIZE(__array) ? __array[(__index)] : 0)
164 if (!(REG8
& ~regflags
)) {
165 if (rex
& (REX_P
|REX_NH
))
166 return GET_REGISTER(nasm_rd_reg8_rex
, regval
);
168 return GET_REGISTER(nasm_rd_reg8
, regval
);
170 if (!(REG16
& ~regflags
))
171 return GET_REGISTER(nasm_rd_reg16
, regval
);
172 if (!(REG32
& ~regflags
))
173 return GET_REGISTER(nasm_rd_reg32
, regval
);
174 if (!(REG64
& ~regflags
))
175 return GET_REGISTER(nasm_rd_reg64
, regval
);
176 if (!(REG_SREG
& ~regflags
))
177 return GET_REGISTER(nasm_rd_sreg
, regval
& 7); /* Ignore REX */
178 if (!(REG_CREG
& ~regflags
))
179 return GET_REGISTER(nasm_rd_creg
, regval
);
180 if (!(REG_DREG
& ~regflags
))
181 return GET_REGISTER(nasm_rd_dreg
, regval
);
182 if (!(REG_TREG
& ~regflags
)) {
184 return 0; /* TR registers are ill-defined with rex */
185 return GET_REGISTER(nasm_rd_treg
, regval
);
187 if (!(FPUREG
& ~regflags
))
188 return GET_REGISTER(nasm_rd_fpureg
, regval
& 7); /* Ignore REX */
189 if (!(MMXREG
& ~regflags
))
190 return GET_REGISTER(nasm_rd_mmxreg
, regval
& 7); /* Ignore REX */
191 if (!(XMMREG
& ~regflags
))
192 return GET_REGISTER(nasm_rd_xmmreg
, regval
);
193 if (!(YMMREG
& ~regflags
))
194 return GET_REGISTER(nasm_rd_ymmreg
, regval
);
195 if (!(ZMMREG
& ~regflags
))
196 return GET_REGISTER(nasm_rd_zmmreg
, regval
);
197 if (!(OPMASKREG
& ~regflags
))
198 return GET_REGISTER(nasm_rd_opmaskreg
, regval
);
199 if (!(BNDREG
& ~regflags
))
200 return GET_REGISTER(nasm_rd_bndreg
, regval
);
206 static uint32_t append_evex_reg_deco(char *buf
, uint32_t num
,
207 decoflags_t deco
, uint8_t *evex
)
209 const char * const er_names
[] = {"rn-sae", "rd-sae", "ru-sae", "rz-sae"};
210 uint32_t num_chars
= 0;
212 if ((deco
& MASK
) && (evex
[2] & EVEX_P2AAA
)) {
213 enum reg_enum opmasknum
= nasm_rd_opmaskreg
[evex
[2] & EVEX_P2AAA
];
214 const char * regname
= nasm_reg_names
[opmasknum
- EXPR_REG_START
];
216 num_chars
+= snprintf(buf
+ num_chars
, num
- num_chars
,
219 if ((deco
& Z
) && (evex
[2] & EVEX_P2Z
)) {
220 num_chars
+= snprintf(buf
+ num_chars
, num
- num_chars
,
225 if (evex
[2] & EVEX_P2B
) {
227 uint8_t er_type
= (evex
[2] & EVEX_P2LL
) >> 5;
228 num_chars
+= snprintf(buf
+ num_chars
, num
- num_chars
,
229 ",{%s}", er_names
[er_type
]);
230 } else if (deco
& SAE
) {
231 num_chars
+= snprintf(buf
+ num_chars
, num
- num_chars
,
239 static uint32_t append_evex_mem_deco(char *buf
, uint32_t num
, opflags_t type
,
240 decoflags_t deco
, uint8_t *evex
)
242 uint32_t num_chars
= 0;
244 if ((evex
[2] & EVEX_P2B
) && (deco
& BRDCAST_MASK
)) {
245 decoflags_t deco_brsize
= deco
& BRSIZE_MASK
;
246 opflags_t template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
247 uint8_t br_num
= (type
& SIZE_MASK
) / BITS128
*
248 BITS64
/ template_opsize
* 2;
250 num_chars
+= snprintf(buf
+ num_chars
, num
- num_chars
,
254 if ((deco
& MASK
) && (evex
[2] & EVEX_P2AAA
)) {
255 enum reg_enum opmasknum
= nasm_rd_opmaskreg
[evex
[2] & EVEX_P2AAA
];
256 const char * regname
= nasm_reg_names
[opmasknum
- EXPR_REG_START
];
258 num_chars
+= snprintf(buf
+ num_chars
, num
- num_chars
,
261 if ((deco
& Z
) && (evex
[2] & EVEX_P2Z
)) {
262 num_chars
+= snprintf(buf
+ num_chars
, num
- num_chars
,
272 * Process an effective address (ModRM) specification.
274 static uint8_t *do_ea(uint8_t *data
, int modrm
, int asize
,
275 int segsize
, enum ea_type type
,
276 operand
*op
, insn
*ins
)
278 int mod
, rm
, scale
, index
, base
;
282 bool is_evex
= !!(ins
->rex
& REX_EV
);
284 mod
= (modrm
>> 6) & 03;
287 if (mod
!= 3 && asize
!= 16 && rm
== 4)
293 if (mod
== 3) { /* pure register version */
294 op
->basereg
= rm
+(rex
& REX_B
? 8 : 0);
295 op
->segment
|= SEG_RMREG
;
296 if (is_evex
&& segsize
== 64) {
297 op
->basereg
+= (evex
[0] & EVEX_P0X
? 0 : 16);
307 * <mod> specifies the displacement size (none, byte or
308 * word), and <rm> specifies the register combination.
309 * Exception: mod=0,rm=6 does not specify [BP] as one might
310 * expect, but instead specifies [disp16].
313 if (type
!= EA_SCALAR
)
316 op
->indexreg
= op
->basereg
= -1;
317 op
->scale
= 1; /* always, in 16 bits */
348 if (rm
== 6 && mod
== 0) { /* special case */
352 mod
= 2; /* fake disp16 */
356 op
->segment
|= SEG_NODISP
;
359 op
->segment
|= SEG_DISP8
;
360 if (ins
->evex_tuple
!= 0) {
361 op
->offset
= gets8(data
) * get_disp8N(ins
);
363 op
->offset
= gets8(data
);
368 op
->segment
|= SEG_DISP16
;
369 op
->offset
= *data
++;
370 op
->offset
|= ((unsigned)*data
++) << 8;
376 * Once again, <mod> specifies displacement size (this time
377 * none, byte or *dword*), while <rm> specifies the base
378 * register. Again, [EBP] is missing, replaced by a pure
379 * disp32 (this time that's mod=0,rm=*5*) in 32-bit mode,
380 * and RIP-relative addressing in 64-bit mode.
383 * indicates not a single base register, but instead the
384 * presence of a SIB byte...
386 int a64
= asize
== 64;
391 op
->basereg
= nasm_rd_reg64
[rm
| ((rex
& REX_B
) ? 8 : 0)];
393 op
->basereg
= nasm_rd_reg32
[rm
| ((rex
& REX_B
) ? 8 : 0)];
395 if (rm
== 5 && mod
== 0) {
397 op
->eaflags
|= EAF_REL
;
398 op
->segment
|= SEG_RELATIVE
;
399 mod
= 2; /* fake disp32 */
403 op
->disp_size
= asize
;
406 mod
= 2; /* fake disp32 */
410 if (rm
== 4) { /* process SIB */
412 scale
= (sib
>> 6) & 03;
413 index
= (sib
>> 3) & 07;
416 op
->scale
= 1 << scale
;
419 vsib_hi
= (rex
& REX_X
? 8 : 0) |
420 (evex
[2] & EVEX_P2VP
? 0 : 16);
423 if (type
== EA_XMMVSIB
)
424 op
->indexreg
= nasm_rd_xmmreg
[index
| vsib_hi
];
425 else if (type
== EA_YMMVSIB
)
426 op
->indexreg
= nasm_rd_ymmreg
[index
| vsib_hi
];
427 else if (type
== EA_ZMMVSIB
)
428 op
->indexreg
= nasm_rd_zmmreg
[index
| vsib_hi
];
429 else if (index
== 4 && !(rex
& REX_X
))
430 op
->indexreg
= -1; /* ESP/RSP cannot be an index */
432 op
->indexreg
= nasm_rd_reg64
[index
| ((rex
& REX_X
) ? 8 : 0)];
434 op
->indexreg
= nasm_rd_reg32
[index
| ((rex
& REX_X
) ? 8 : 0)];
436 if (base
== 5 && mod
== 0) {
438 mod
= 2; /* Fake disp32 */
440 op
->basereg
= nasm_rd_reg64
[base
| ((rex
& REX_B
) ? 8 : 0)];
442 op
->basereg
= nasm_rd_reg32
[base
| ((rex
& REX_B
) ? 8 : 0)];
446 } else if (type
!= EA_SCALAR
) {
447 /* Can't have VSIB without SIB */
453 op
->segment
|= SEG_NODISP
;
456 op
->segment
|= SEG_DISP8
;
457 if (ins
->evex_tuple
!= 0) {
458 op
->offset
= gets8(data
) * get_disp8N(ins
);
460 op
->offset
= gets8(data
);
465 op
->segment
|= SEG_DISP32
;
466 op
->offset
= gets32(data
);
475 * Determine whether the instruction template in t corresponds to the data
476 * stream in data. Return the number of bytes matched if so.
478 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
480 static int matches(const struct itemplate
*t
, uint8_t *data
,
481 const struct prefix_info
*prefix
, int segsize
, insn
*ins
)
483 uint8_t *r
= (uint8_t *)(t
->code
);
484 uint8_t *origdata
= data
;
485 bool a_used
= false, o_used
= false;
486 enum prefixes drep
= 0;
487 enum prefixes dwait
= 0;
488 uint8_t lock
= prefix
->lock
;
489 int osize
= prefix
->osize
;
490 int asize
= prefix
->asize
;
493 struct operand
*opx
, *opy
;
496 int regmask
= (segsize
== 64) ? 15 : 7;
497 enum ea_type eat
= EA_SCALAR
;
499 for (i
= 0; i
< MAX_OPERANDS
; i
++) {
500 ins
->oprs
[i
].segment
= ins
->oprs
[i
].disp_size
=
501 (segsize
== 64 ? SEG_64BIT
: segsize
== 32 ? SEG_32BIT
: 0);
505 ins
->rex
= prefix
->rex
;
506 memset(ins
->prefixes
, 0, sizeof ins
->prefixes
);
508 if (itemp_has(t
, (segsize
== 64 ? IF_NOLONG
: IF_LONG
)))
511 if (prefix
->rep
== 0xF2)
512 drep
= (itemp_has(t
, IF_BND
) ? P_BND
: P_REPNE
);
513 else if (prefix
->rep
== 0xF3)
516 dwait
= prefix
->wait
? P_WAIT
: 0;
518 while ((c
= *r
++) != 0) {
519 op1
= (c
& 3) + ((opex
& 1) << 2);
520 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
521 opx
= &ins
->oprs
[op1
];
522 opy
= &ins
->oprs
[op2
];
543 int t
= *r
++, d
= *data
++;
544 if (d
< t
|| d
> t
+ 7)
547 opx
->basereg
= (d
-t
)+
548 (ins
->rex
& REX_B
? 8 : 0);
549 opx
->segment
|= SEG_RMREG
;
555 /* this is an separate index reg position of MIB operand (ICC) */
556 /* Disassembler uses NASM's split EA form only */
560 opx
->offset
= (int8_t)*data
++;
561 opx
->segment
|= SEG_SIGNED
;
565 opx
->offset
= *data
++;
569 opx
->offset
= *data
++;
573 opx
->offset
= getu16(data
);
579 opx
->offset
= getu32(data
);
582 opx
->offset
= getu16(data
);
585 if (segsize
!= asize
)
586 opx
->disp_size
= asize
;
590 opx
->offset
= getu32(data
);
595 opx
->offset
= gets32(data
);
602 opx
->offset
= getu16(data
);
608 opx
->offset
= getu32(data
);
614 opx
->offset
= getu64(data
);
622 opx
->offset
= gets8(data
++);
623 opx
->segment
|= SEG_RELATIVE
;
627 opx
->offset
= getu64(data
);
632 opx
->offset
= gets16(data
);
634 opx
->segment
|= SEG_RELATIVE
;
635 opx
->segment
&= ~SEG_32BIT
;
638 case4(064): /* rel */
639 opx
->segment
|= SEG_RELATIVE
;
640 /* In long mode rel is always 32 bits, sign extended. */
641 if (segsize
== 64 || osize
== 32) {
642 opx
->offset
= gets32(data
);
645 opx
->segment
|= SEG_32BIT
;
646 opx
->type
= (opx
->type
& ~SIZE_MASK
)
647 | (segsize
== 64 ? BITS64
: BITS32
);
649 opx
->offset
= gets16(data
);
651 opx
->segment
&= ~SEG_32BIT
;
652 opx
->type
= (opx
->type
& ~SIZE_MASK
) | BITS16
;
657 opx
->offset
= gets32(data
);
659 opx
->segment
|= SEG_32BIT
| SEG_RELATIVE
;
668 opx
->segment
|= SEG_RMREG
;
669 data
= do_ea(data
, modrm
, asize
, segsize
, eat
, opy
, ins
);
672 opx
->basereg
= ((modrm
>> 3) & 7) + (ins
->rex
& REX_R
? 8 : 0);
673 if ((ins
->rex
& REX_EV
) && (segsize
== 64))
674 opx
->basereg
+= (ins
->evex_p
[0] & EVEX_P0RP
? 0 : 16);
680 uint8_t ximm
= *data
++;
682 ins
->oprs
[c
>> 3].basereg
= (ximm
>> 4) & regmask
;
683 ins
->oprs
[c
>> 3].segment
|= SEG_RMREG
;
684 ins
->oprs
[c
& 7].offset
= ximm
& 15;
690 uint8_t ximm
= *data
++;
696 ins
->oprs
[c
>> 4].basereg
= (ximm
>> 4) & regmask
;
697 ins
->oprs
[c
>> 4].segment
|= SEG_RMREG
;
703 uint8_t ximm
= *data
++;
705 opx
->basereg
= (ximm
>> 4) & regmask
;
706 opx
->segment
|= SEG_RMREG
;
720 if (((modrm
>> 3) & 07) != (c
& 07))
721 return 0; /* spare field doesn't match up */
722 data
= do_ea(data
, modrm
, asize
, segsize
, eat
, opy
, ins
);
731 uint8_t evexm
= *r
++;
732 uint8_t evexwlp
= *r
++;
733 uint8_t modrm
, valid_mask
;
734 ins
->evex_tuple
= *r
++ - 0300;
735 modrm
= *(origdata
+ 1);
738 if ((prefix
->rex
& (REX_EV
|REX_V
|REX_P
)) != REX_EV
)
741 if ((evexm
& 0x1f) != prefix
->vex_m
)
744 switch (evexwlp
& 060) {
746 if (prefix
->rex
& REX_W
)
750 if (!(prefix
->rex
& REX_W
))
754 case 040: /* VEX.W is a don't care */
761 /* If EVEX.b is set with reg-reg op,
762 * EVEX.L'L contains embedded rounding control info
764 if ((prefix
->evex
[2] & EVEX_P2B
) && ((modrm
>> 6) == 3)) {
765 valid_mask
= 0x3; /* prefix only */
767 valid_mask
= 0xf; /* vector length and prefix */
769 if ((evexwlp
^ prefix
->vex_lp
) & valid_mask
)
773 if ((prefix
->vex_v
!= 0) ||
774 (!(prefix
->evex
[2] & EVEX_P2VP
) &&
775 ((eat
< EA_XMMVSIB
) || (eat
> EA_ZMMVSIB
))))
778 opx
->segment
|= SEG_RMREG
;
779 opx
->basereg
= ((~prefix
->evex
[2] & EVEX_P2VP
) << (4 - 3) ) |
783 memcpy(ins
->evex_p
, prefix
->evex
, 3);
794 if ((prefix
->rex
& (REX_V
|REX_P
)) != REX_V
)
797 if ((vexm
& 0x1f) != prefix
->vex_m
)
800 switch (vexwlp
& 060) {
802 if (prefix
->rex
& REX_W
)
806 if (!(prefix
->rex
& REX_W
))
810 case 040: /* VEX.W is a don't care */
817 /* The 010 bit of vexwlp is set if VEX.L is ignored */
818 if ((vexwlp
^ prefix
->vex_lp
) & ((vexwlp
& 010) ? 03 : 07))
822 if (prefix
->vex_v
!= 0)
825 opx
->segment
|= SEG_RMREG
;
826 opx
->basereg
= prefix
->vex_v
;
833 if (prefix
->rep
== 0xF3)
838 if (prefix
->rep
== 0xF2)
840 else if (prefix
->rep
== 0xF3)
845 if (prefix
->lock
== 0xF0) {
846 if (prefix
->rep
== 0xF2)
848 else if (prefix
->rep
== 0xF3)
868 if (asize
!= segsize
)
882 if (prefix
->rex
& REX_B
)
887 if (prefix
->rex
& REX_X
)
892 if (prefix
->rex
& REX_R
)
897 if (prefix
->rex
& REX_W
)
916 if (osize
!= (segsize
== 16) ? 16 : 32)
923 ins
->rex
|= REX_W
; /* 64-bit only instruction */
940 int t
= *r
++, d
= *data
++;
941 if (d
< t
|| d
> t
+ 15)
944 ins
->condition
= d
- t
;
949 if (prefix
->rep
== 0xF3)
959 if (prefix
->rep
!= 0xF2)
965 if (prefix
->rep
!= 0xF3)
990 if (prefix
->wait
!= 0x9B)
996 if (prefix
->osp
|| prefix
->rep
)
1001 if (!prefix
->osp
|| prefix
->rep
)
1045 return 0; /* Unknown code */
1049 if (!vex_ok
&& (ins
->rex
& (REX_V
| REX_EV
)))
1052 /* REX cannot be combined with VEX */
1053 if ((ins
->rex
& REX_V
) && (prefix
->rex
& REX_P
))
1057 * Check for unused rep or a/o prefixes.
1059 for (i
= 0; i
< t
->operands
; i
++) {
1060 if (ins
->oprs
[i
].segment
!= SEG_RMREG
)
1065 if (ins
->prefixes
[PPS_LOCK
])
1067 ins
->prefixes
[PPS_LOCK
] = P_LOCK
;
1070 if (ins
->prefixes
[PPS_REP
])
1072 ins
->prefixes
[PPS_REP
] = drep
;
1074 ins
->prefixes
[PPS_WAIT
] = dwait
;
1076 if (osize
!= ((segsize
== 16) ? 16 : 32)) {
1077 enum prefixes pfx
= 0;
1091 if (ins
->prefixes
[PPS_OSIZE
])
1093 ins
->prefixes
[PPS_OSIZE
] = pfx
;
1096 if (!a_used
&& asize
!= segsize
) {
1097 if (ins
->prefixes
[PPS_ASIZE
])
1099 ins
->prefixes
[PPS_ASIZE
] = asize
== 16 ? P_A16
: P_A32
;
1102 /* Fix: check for redundant REX prefixes */
1104 return data
- origdata
;
1107 /* Condition names for disassembly, sorted by x86 code */
1108 static const char * const condition_name
[16] = {
1109 "o", "no", "c", "nc", "z", "nz", "na", "a",
1110 "s", "ns", "pe", "po", "l", "nl", "ng", "g"
1113 int32_t disasm(uint8_t *data
, char *output
, int outbufsize
, int segsize
,
1114 int32_t offset
, int autosync
, iflag_t
*prefer
)
1116 const struct itemplate
* const *p
, * const *best_p
;
1117 const struct disasm_index
*ix
;
1119 int length
, best_length
= 0;
1121 int i
, slen
, colon
, n
;
1125 iflag_t goodness
, best
;
1127 struct prefix_info prefix
;
1131 memset(&ins
, 0, sizeof ins
);
1134 * Scan for prefixes.
1136 memset(&prefix
, 0, sizeof prefix
);
1137 prefix
.asize
= segsize
;
1138 prefix
.osize
= (segsize
== 64) ? 32 : segsize
;
1145 while (!end_prefix
) {
1149 prefix
.rep
= *data
++;
1153 prefix
.wait
= *data
++;
1157 prefix
.lock
= *data
++;
1161 segover
= "cs", prefix
.seg
= *data
++;
1164 segover
= "ss", prefix
.seg
= *data
++;
1167 segover
= "ds", prefix
.seg
= *data
++;
1170 segover
= "es", prefix
.seg
= *data
++;
1173 segover
= "fs", prefix
.seg
= *data
++;
1176 segover
= "gs", prefix
.seg
= *data
++;
1180 prefix
.osize
= (segsize
== 16) ? 32 : 16;
1181 prefix
.osp
= *data
++;
1184 prefix
.asize
= (segsize
== 32) ? 16 : 32;
1185 prefix
.asp
= *data
++;
1190 if (segsize
== 64 || (data
[1] & 0xc0) == 0xc0) {
1191 prefix
.vex
[0] = *data
++;
1192 prefix
.vex
[1] = *data
++;
1195 prefix
.vex_c
= RV_VEX
;
1197 if (prefix
.vex
[0] == 0xc4) {
1198 prefix
.vex
[2] = *data
++;
1199 prefix
.rex
|= (~prefix
.vex
[1] >> 5) & 7; /* REX_RXB */
1200 prefix
.rex
|= (prefix
.vex
[2] >> (7-3)) & REX_W
;
1201 prefix
.vex_m
= prefix
.vex
[1] & 0x1f;
1202 prefix
.vex_v
= (~prefix
.vex
[2] >> 3) & 15;
1203 prefix
.vex_lp
= prefix
.vex
[2] & 7;
1205 prefix
.rex
|= (~prefix
.vex
[1] >> (7-2)) & REX_R
;
1207 prefix
.vex_v
= (~prefix
.vex
[1] >> 3) & 15;
1208 prefix
.vex_lp
= prefix
.vex
[1] & 7;
1211 ix
= itable_vex
[RV_VEX
][prefix
.vex_m
][prefix
.vex_lp
& 3];
1218 uint8_t evex_p0
= data
[1] & 0x0f;
1219 if (segsize
== 64 ||
1220 ((evex_p0
>= 0x01) && (evex_p0
<= 0x03))) {
1221 data
++; /* 62h EVEX prefix */
1222 prefix
.evex
[0] = *data
++;
1223 prefix
.evex
[1] = *data
++;
1224 prefix
.evex
[2] = *data
++;
1226 prefix
.rex
= REX_EV
;
1227 prefix
.vex_c
= RV_EVEX
;
1228 prefix
.rex
|= (~prefix
.evex
[0] >> 5) & 7; /* REX_RXB */
1229 prefix
.rex
|= (prefix
.evex
[1] >> (7-3)) & REX_W
;
1230 prefix
.vex_m
= prefix
.evex
[0] & EVEX_P0MM
;
1231 prefix
.vex_v
= (~prefix
.evex
[1] & EVEX_P1VVVV
) >> 3;
1232 prefix
.vex_lp
= ((prefix
.evex
[2] & EVEX_P2LL
) >> (5-2)) |
1233 (prefix
.evex
[1] & EVEX_P1PP
);
1235 ix
= itable_vex
[prefix
.vex_c
][prefix
.vex_m
][prefix
.vex_lp
& 3];
1242 if ((data
[1] & 030) != 0 &&
1243 (segsize
== 64 || (data
[1] & 0xc0) == 0xc0)) {
1244 prefix
.vex
[0] = *data
++;
1245 prefix
.vex
[1] = *data
++;
1246 prefix
.vex
[2] = *data
++;
1249 prefix
.vex_c
= RV_XOP
;
1251 prefix
.rex
|= (~prefix
.vex
[1] >> 5) & 7; /* REX_RXB */
1252 prefix
.rex
|= (prefix
.vex
[2] >> (7-3)) & REX_W
;
1253 prefix
.vex_m
= prefix
.vex
[1] & 0x1f;
1254 prefix
.vex_v
= (~prefix
.vex
[2] >> 3) & 15;
1255 prefix
.vex_lp
= prefix
.vex
[2] & 7;
1257 ix
= itable_vex
[RV_XOP
][prefix
.vex_m
][prefix
.vex_lp
& 3];
1278 if (segsize
== 64) {
1279 prefix
.rex
= *data
++;
1280 if (prefix
.rex
& REX_W
)
1292 iflag_set_all(&best
); /* Worst possible */
1294 best_pref
= INT_MAX
;
1297 return 0; /* No instruction table at all... */
1301 while (ix
->n
== -1) {
1302 ix
= (const struct disasm_index
*)ix
->p
+ *dp
++;
1305 p
= (const struct itemplate
* const *)ix
->p
;
1306 for (n
= ix
->n
; n
; n
--, p
++) {
1307 if ((length
= matches(*p
, data
, &prefix
, segsize
, &tmp_ins
))) {
1310 * Final check to make sure the types of r/m match up.
1311 * XXX: Need to make sure this is actually correct.
1313 for (i
= 0; i
< (*p
)->operands
; i
++) {
1315 /* If it's a mem-only EA but we have a
1317 ((tmp_ins
.oprs
[i
].segment
& SEG_RMREG
) &&
1318 is_class(MEMORY
, (*p
)->opd
[i
])) ||
1319 /* If it's a reg-only EA but we have a memory
1321 (!(tmp_ins
.oprs
[i
].segment
& SEG_RMREG
) &&
1322 !(REG_EA
& ~(*p
)->opd
[i
]) &&
1323 !((*p
)->opd
[i
] & REG_SMASK
)) ||
1324 /* Register type mismatch (eg FS vs REG_DESS):
1326 ((((*p
)->opd
[i
] & (REGISTER
| FPUREG
)) ||
1327 (tmp_ins
.oprs
[i
].segment
& SEG_RMREG
)) &&
1328 !whichreg((*p
)->opd
[i
],
1329 tmp_ins
.oprs
[i
].basereg
, tmp_ins
.rex
))
1337 * Note: we always prefer instructions which incorporate
1338 * prefixes in the instructions themselves. This is to allow
1339 * e.g. PAUSE to be preferred to REP NOP, and deal with
1340 * MMX/SSE instructions where prefixes are used to select
1341 * between MMX and SSE register sets or outright opcode
1346 goodness
= iflag_pfmask(*p
);
1347 goodness
= iflag_xor(&goodness
, prefer
);
1349 for (i
= 0; i
< MAXPREFIX
; i
++)
1350 if (tmp_ins
.prefixes
[i
])
1352 if (nprefix
< best_pref
||
1353 (nprefix
== best_pref
&&
1354 iflag_cmp(&goodness
, &best
) < 0)) {
1355 /* This is the best one found so far */
1358 best_pref
= nprefix
;
1359 best_length
= length
;
1367 return 0; /* no instruction was matched */
1369 /* Pick the best match */
1371 length
= best_length
;
1375 /* TODO: snprintf returns the value that the string would have if
1376 * the buffer were long enough, and not the actual length of
1377 * the returned string, so each instance of using the return
1378 * value of snprintf should actually be checked to assure that
1379 * the return value is "sane." Maybe a macro wrapper could
1380 * be used for that purpose.
1382 for (i
= 0; i
< MAXPREFIX
; i
++) {
1383 const char *prefix
= prefix_name(ins
.prefixes
[i
]);
1385 slen
+= snprintf(output
+slen
, outbufsize
-slen
, "%s ", prefix
);
1389 if (i
>= FIRST_COND_OPCODE
)
1390 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s%s",
1391 nasm_insn_names
[i
], condition_name
[ins
.condition
]);
1393 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1394 nasm_insn_names
[i
]);
1397 is_evex
= !!(ins
.rex
& REX_EV
);
1398 length
+= data
- origdata
; /* fix up for prefixes */
1399 for (i
= 0; i
< (*p
)->operands
; i
++) {
1400 opflags_t t
= (*p
)->opd
[i
];
1401 decoflags_t deco
= (*p
)->deco
[i
];
1402 const operand
*o
= &ins
.oprs
[i
];
1405 output
[slen
++] = (colon
? ':' : i
== 0 ? ' ' : ',');
1408 if (o
->segment
& SEG_RELATIVE
) {
1409 offs
+= offset
+ length
;
1411 * sort out wraparound
1413 if (!(o
->segment
& (SEG_32BIT
|SEG_64BIT
)))
1415 else if (segsize
!= 64)
1419 * add sync marker, if autosync is on
1430 if ((t
& (REGISTER
| FPUREG
)) ||
1431 (o
->segment
& SEG_RMREG
)) {
1433 reg
= whichreg(t
, o
->basereg
, ins
.rex
);
1435 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "to ");
1436 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1437 nasm_reg_names
[reg
-EXPR_REG_START
]);
1438 if (is_evex
&& deco
)
1439 slen
+= append_evex_reg_deco(output
+ slen
, outbufsize
- slen
,
1441 } else if (!(UNITY
& ~t
)) {
1442 output
[slen
++] = '1';
1443 } else if (t
& IMMEDIATE
) {
1446 snprintf(output
+ slen
, outbufsize
- slen
, "byte ");
1447 if (o
->segment
& SEG_SIGNED
) {
1450 output
[slen
++] = '-';
1452 output
[slen
++] = '+';
1454 } else if (t
& BITS16
) {
1456 snprintf(output
+ slen
, outbufsize
- slen
, "word ");
1457 } else if (t
& BITS32
) {
1459 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1460 } else if (t
& BITS64
) {
1462 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1463 } else if (t
& NEAR
) {
1465 snprintf(output
+ slen
, outbufsize
- slen
, "near ");
1466 } else if (t
& SHORT
) {
1468 snprintf(output
+ slen
, outbufsize
- slen
, "short ");
1471 snprintf(output
+ slen
, outbufsize
- slen
, "0x%"PRIx64
"",
1473 } else if (!(MEM_OFFS
& ~t
)) {
1475 snprintf(output
+ slen
, outbufsize
- slen
,
1476 "[%s%s%s0x%"PRIx64
"]",
1477 (segover
? segover
: ""),
1478 (segover
? ":" : ""),
1479 (o
->disp_size
== 64 ? "qword " :
1480 o
->disp_size
== 32 ? "dword " :
1481 o
->disp_size
== 16 ? "word " : ""), offs
);
1483 } else if (is_class(REGMEM
, t
)) {
1484 int started
= false;
1487 snprintf(output
+ slen
, outbufsize
- slen
, "byte ");
1490 snprintf(output
+ slen
, outbufsize
- slen
, "word ");
1493 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1496 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1499 snprintf(output
+ slen
, outbufsize
- slen
, "tword ");
1500 if ((ins
.evex_p
[2] & EVEX_P2B
) && (deco
& BRDCAST_MASK
)) {
1501 /* when broadcasting, each element size should be used */
1502 if (deco
& BR_BITS32
)
1504 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1505 else if (deco
& BR_BITS64
)
1507 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1511 snprintf(output
+ slen
, outbufsize
- slen
, "oword ");
1514 snprintf(output
+ slen
, outbufsize
- slen
, "yword ");
1517 snprintf(output
+ slen
, outbufsize
- slen
, "zword ");
1520 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "far ");
1523 snprintf(output
+ slen
, outbufsize
- slen
, "near ");
1524 output
[slen
++] = '[';
1526 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1527 (o
->disp_size
== 64 ? "qword " :
1528 o
->disp_size
== 32 ? "dword " :
1529 o
->disp_size
== 16 ? "word " :
1531 if (o
->eaflags
& EAF_REL
)
1532 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "rel ");
1535 snprintf(output
+ slen
, outbufsize
- slen
, "%s:",
1539 if (o
->basereg
!= -1) {
1540 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1541 nasm_reg_names
[(o
->basereg
-EXPR_REG_START
)]);
1544 if (o
->indexreg
!= -1 && !itemp_has(*best_p
, IF_MIB
)) {
1546 output
[slen
++] = '+';
1547 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1548 nasm_reg_names
[(o
->indexreg
-EXPR_REG_START
)]);
1551 snprintf(output
+ slen
, outbufsize
- slen
, "*%d",
1557 if (o
->segment
& SEG_DISP8
) {
1560 uint32_t offset
= offs
;
1561 if ((int32_t)offset
< 0) {
1568 snprintf(output
+ slen
, outbufsize
- slen
, "%s0x%"PRIx32
"",
1572 uint8_t offset
= offs
;
1573 if ((int8_t)offset
< 0) {
1580 snprintf(output
+ slen
, outbufsize
- slen
, "%s0x%"PRIx8
"",
1583 } else if (o
->segment
& SEG_DISP16
) {
1585 uint16_t offset
= offs
;
1586 if ((int16_t)offset
< 0 && started
) {
1590 prefix
= started
? "+" : "";
1593 snprintf(output
+ slen
, outbufsize
- slen
,
1594 "%s0x%"PRIx16
"", prefix
, offset
);
1595 } else if (o
->segment
& SEG_DISP32
) {
1596 if (prefix
.asize
== 64) {
1598 uint64_t offset
= (int64_t)(int32_t)offs
;
1599 if ((int32_t)offs
< 0 && started
) {
1603 prefix
= started
? "+" : "";
1606 snprintf(output
+ slen
, outbufsize
- slen
,
1607 "%s0x%"PRIx64
"", prefix
, offset
);
1610 uint32_t offset
= offs
;
1611 if ((int32_t) offset
< 0 && started
) {
1615 prefix
= started
? "+" : "";
1618 snprintf(output
+ slen
, outbufsize
- slen
,
1619 "%s0x%"PRIx32
"", prefix
, offset
);
1623 if (o
->indexreg
!= -1 && itemp_has(*best_p
, IF_MIB
)) {
1624 output
[slen
++] = ',';
1625 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1626 nasm_reg_names
[(o
->indexreg
-EXPR_REG_START
)]);
1629 snprintf(output
+ slen
, outbufsize
- slen
, "*%d",
1634 output
[slen
++] = ']';
1636 if (is_evex
&& deco
)
1637 slen
+= append_evex_mem_deco(output
+ slen
, outbufsize
- slen
,
1638 t
, deco
, ins
.evex_p
);
1641 snprintf(output
+ slen
, outbufsize
- slen
, "<operand%d>",
1645 output
[slen
] = '\0';
1646 if (segover
) { /* unused segment override */
1648 int count
= slen
+ 1;
1650 p
[count
+ 3] = p
[count
];
1651 strncpy(output
, segover
, 2);
1658 * This is called when we don't have a complete instruction. If it
1659 * is a standalone *single-byte* prefix show it as such, otherwise
1660 * print it as a literal.
1662 int32_t eatbyte(uint8_t *data
, char *output
, int outbufsize
, int segsize
)
1664 uint8_t byte
= *data
;
1665 const char *str
= NULL
;
1699 str
= (segsize
== 16) ? "o32" : "o16";
1702 str
= (segsize
== 32) ? "a16" : "a32";
1720 if (segsize
== 64) {
1721 snprintf(output
, outbufsize
, "rex%s%s%s%s%s",
1722 (byte
== REX_P
) ? "" : ".",
1723 (byte
& REX_W
) ? "w" : "",
1724 (byte
& REX_R
) ? "r" : "",
1725 (byte
& REX_X
) ? "x" : "",
1726 (byte
& REX_B
) ? "b" : "");
1729 /* else fall through */
1731 snprintf(output
, outbufsize
, "db 0x%02x", byte
);
1736 snprintf(output
, outbufsize
, "%s", str
);