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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, Inc.,
10 * 51 Franklin St, Fifth Floor, Boston MA 02110-1301, USA; version 2.1,
11 * or, at your option, any later version, incorporated herein by
14 * Patches submitted to this file are required to be dual licensed
15 * under the LGPL 2.1+ and the 2-clause BSD license:
17 * Copyright 1996-2009 the NASM Authors - All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer in the documentation and/or other materials provided
28 * with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
41 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ----------------------------------------------------------------------- */
47 * disasm.c where all the _work_ gets done in the Netwide Disassembler
65 * Flags that go into the `segment' field of `insn' structures
68 #define SEG_RELATIVE 1
75 #define SEG_SIGNED 128
82 uint8_t osize
; /* Operand size */
83 uint8_t asize
; /* Address size */
84 uint8_t osp
; /* Operand size prefix present */
85 uint8_t asp
; /* Address size prefix present */
86 uint8_t rep
; /* Rep prefix present */
87 uint8_t seg
; /* Segment override prefix present */
88 uint8_t wait
; /* WAIT "prefix" present */
89 uint8_t lock
; /* Lock prefix present */
90 uint8_t vex
[3]; /* VEX prefix present */
91 uint8_t vex_c
; /* VEX "class" (VEX, XOP, ...) */
92 uint8_t vex_m
; /* VEX.M field */
94 uint8_t vex_lp
; /* VEX.LP fields */
95 uint32_t rex
; /* REX prefix present */
98 #define getu8(x) (*(uint8_t *)(x))
100 /* Littleendian CPU which can handle unaligned references */
101 #define getu16(x) (*(uint16_t *)(x))
102 #define getu32(x) (*(uint32_t *)(x))
103 #define getu64(x) (*(uint64_t *)(x))
105 static uint16_t getu16(uint8_t *data
)
107 return (uint16_t)data
[0] + ((uint16_t)data
[1] << 8);
109 static uint32_t getu32(uint8_t *data
)
111 return (uint32_t)getu16(data
) + ((uint32_t)getu16(data
+2) << 16);
113 static uint64_t getu64(uint8_t *data
)
115 return (uint64_t)getu32(data
) + ((uint64_t)getu32(data
+4) << 32);
119 #define gets8(x) ((int8_t)getu8(x))
120 #define gets16(x) ((int16_t)getu16(x))
121 #define gets32(x) ((int32_t)getu32(x))
122 #define gets64(x) ((int64_t)getu64(x))
124 /* Important: regval must already have been adjusted for rex extensions */
125 static enum reg_enum
whichreg(int32_t regflags
, int regval
, int rex
)
127 if (!(regflags
& (REGISTER
|REGMEM
)))
128 return 0; /* Registers not permissible?! */
130 regflags
|= REGISTER
;
132 if (!(REG_AL
& ~regflags
))
134 if (!(REG_AX
& ~regflags
))
136 if (!(REG_EAX
& ~regflags
))
138 if (!(REG_RAX
& ~regflags
))
140 if (!(REG_DL
& ~regflags
))
142 if (!(REG_DX
& ~regflags
))
144 if (!(REG_EDX
& ~regflags
))
146 if (!(REG_RDX
& ~regflags
))
148 if (!(REG_CL
& ~regflags
))
150 if (!(REG_CX
& ~regflags
))
152 if (!(REG_ECX
& ~regflags
))
154 if (!(REG_RCX
& ~regflags
))
156 if (!(FPU0
& ~regflags
))
158 if (!(XMM0
& ~regflags
))
160 if (!(YMM0
& ~regflags
))
162 if (!(REG_CS
& ~regflags
))
163 return (regval
== 1) ? R_CS
: 0;
164 if (!(REG_DESS
& ~regflags
))
165 return (regval
== 0 || regval
== 2
166 || regval
== 3 ? nasm_rd_sreg
[regval
] : 0);
167 if (!(REG_FSGS
& ~regflags
))
168 return (regval
== 4 || regval
== 5 ? nasm_rd_sreg
[regval
] : 0);
169 if (!(REG_SEG67
& ~regflags
))
170 return (regval
== 6 || regval
== 7 ? nasm_rd_sreg
[regval
] : 0);
172 /* All the entries below look up regval in an 16-entry array */
173 if (regval
< 0 || regval
> 15)
176 if (!(REG8
& ~regflags
)) {
177 if (rex
& (REX_P
|REX_NH
))
178 return nasm_rd_reg8_rex
[regval
];
180 return nasm_rd_reg8
[regval
];
182 if (!(REG16
& ~regflags
))
183 return nasm_rd_reg16
[regval
];
184 if (!(REG32
& ~regflags
))
185 return nasm_rd_reg32
[regval
];
186 if (!(REG64
& ~regflags
))
187 return nasm_rd_reg64
[regval
];
188 if (!(REG_SREG
& ~regflags
))
189 return nasm_rd_sreg
[regval
& 7]; /* Ignore REX */
190 if (!(REG_CREG
& ~regflags
))
191 return nasm_rd_creg
[regval
];
192 if (!(REG_DREG
& ~regflags
))
193 return nasm_rd_dreg
[regval
];
194 if (!(REG_TREG
& ~regflags
)) {
196 return 0; /* TR registers are ill-defined with rex */
197 return nasm_rd_treg
[regval
];
199 if (!(FPUREG
& ~regflags
))
200 return nasm_rd_fpureg
[regval
& 7]; /* Ignore REX */
201 if (!(MMXREG
& ~regflags
))
202 return nasm_rd_mmxreg
[regval
& 7]; /* Ignore REX */
203 if (!(XMMREG
& ~regflags
))
204 return nasm_rd_xmmreg
[regval
];
205 if (!(YMMREG
& ~regflags
))
206 return nasm_rd_ymmreg
[regval
];
212 * Process a DREX suffix
214 static uint8_t *do_drex(uint8_t *data
, insn
*ins
)
216 uint8_t drex
= *data
++;
217 operand
*dst
= &ins
->oprs
[ins
->drexdst
];
219 if ((drex
& 8) != ((ins
->rex
& REX_OC
) ? 8 : 0))
220 return NULL
; /* OC0 mismatch */
221 ins
->rex
= (ins
->rex
& ~7) | (drex
& 7);
223 dst
->segment
= SEG_RMREG
;
224 dst
->basereg
= drex
>> 4;
230 * Process an effective address (ModRM) specification.
232 static uint8_t *do_ea(uint8_t *data
, int modrm
, int asize
,
233 int segsize
, operand
* op
, insn
*ins
)
235 int mod
, rm
, scale
, index
, base
;
239 mod
= (modrm
>> 6) & 03;
242 if (mod
!= 3 && rm
== 4 && asize
!= 16)
245 if (ins
->rex
& REX_D
) {
246 data
= do_drex(data
, ins
);
252 if (mod
== 3) { /* pure register version */
253 op
->basereg
= rm
+(rex
& REX_B
? 8 : 0);
254 op
->segment
|= SEG_RMREG
;
263 * <mod> specifies the displacement size (none, byte or
264 * word), and <rm> specifies the register combination.
265 * Exception: mod=0,rm=6 does not specify [BP] as one might
266 * expect, but instead specifies [disp16].
268 op
->indexreg
= op
->basereg
= -1;
269 op
->scale
= 1; /* always, in 16 bits */
300 if (rm
== 6 && mod
== 0) { /* special case */
304 mod
= 2; /* fake disp16 */
308 op
->segment
|= SEG_NODISP
;
311 op
->segment
|= SEG_DISP8
;
312 op
->offset
= (int8_t)*data
++;
315 op
->segment
|= SEG_DISP16
;
316 op
->offset
= *data
++;
317 op
->offset
|= ((unsigned)*data
++) << 8;
323 * Once again, <mod> specifies displacement size (this time
324 * none, byte or *dword*), while <rm> specifies the base
325 * register. Again, [EBP] is missing, replaced by a pure
326 * disp32 (this time that's mod=0,rm=*5*) in 32-bit mode,
327 * and RIP-relative addressing in 64-bit mode.
330 * indicates not a single base register, but instead the
331 * presence of a SIB byte...
333 int a64
= asize
== 64;
338 op
->basereg
= nasm_rd_reg64
[rm
| ((rex
& REX_B
) ? 8 : 0)];
340 op
->basereg
= nasm_rd_reg32
[rm
| ((rex
& REX_B
) ? 8 : 0)];
342 if (rm
== 5 && mod
== 0) {
344 op
->eaflags
|= EAF_REL
;
345 op
->segment
|= SEG_RELATIVE
;
346 mod
= 2; /* fake disp32 */
350 op
->disp_size
= asize
;
353 mod
= 2; /* fake disp32 */
356 if (rm
== 4) { /* process SIB */
357 scale
= (sib
>> 6) & 03;
358 index
= (sib
>> 3) & 07;
361 op
->scale
= 1 << scale
;
363 if (index
== 4 && !(rex
& REX_X
))
364 op
->indexreg
= -1; /* ESP/RSP cannot be an index */
366 op
->indexreg
= nasm_rd_reg64
[index
| ((rex
& REX_X
) ? 8 : 0)];
368 op
->indexreg
= nasm_rd_reg32
[index
| ((rex
& REX_X
) ? 8 : 0)];
370 if (base
== 5 && mod
== 0) {
372 mod
= 2; /* Fake disp32 */
374 op
->basereg
= nasm_rd_reg64
[base
| ((rex
& REX_B
) ? 8 : 0)];
376 op
->basereg
= nasm_rd_reg32
[base
| ((rex
& REX_B
) ? 8 : 0)];
384 op
->segment
|= SEG_NODISP
;
387 op
->segment
|= SEG_DISP8
;
388 op
->offset
= gets8(data
);
392 op
->segment
|= SEG_DISP32
;
393 op
->offset
= gets32(data
);
402 * Determine whether the instruction template in t corresponds to the data
403 * stream in data. Return the number of bytes matched if so.
405 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
407 static int matches(const struct itemplate
*t
, uint8_t *data
,
408 const struct prefix_info
*prefix
, int segsize
, insn
*ins
)
410 uint8_t *r
= (uint8_t *)(t
->code
);
411 uint8_t *origdata
= data
;
412 bool a_used
= false, o_used
= false;
413 enum prefixes drep
= 0;
414 enum prefixes dwait
= 0;
415 uint8_t lock
= prefix
->lock
;
416 int osize
= prefix
->osize
;
417 int asize
= prefix
->asize
;
420 struct operand
*opx
, *opy
;
422 int s_field_for
= -1; /* No 144/154 series code encountered */
424 int regmask
= (segsize
== 64) ? 15 : 7;
426 for (i
= 0; i
< MAX_OPERANDS
; i
++) {
427 ins
->oprs
[i
].segment
= ins
->oprs
[i
].disp_size
=
428 (segsize
== 64 ? SEG_64BIT
: segsize
== 32 ? SEG_32BIT
: 0);
431 ins
->rex
= prefix
->rex
;
432 memset(ins
->prefixes
, 0, sizeof ins
->prefixes
);
434 if (t
->flags
& (segsize
== 64 ? IF_NOLONG
: IF_LONG
))
437 if (prefix
->rep
== 0xF2)
439 else if (prefix
->rep
== 0xF3)
442 dwait
= prefix
->wait
? P_WAIT
: 0;
444 while ((c
= *r
++) != 0) {
445 op1
= (c
& 3) + ((opex
& 1) << 2);
446 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
447 opx
= &ins
->oprs
[op1
];
448 opy
= &ins
->oprs
[op2
];
469 int t
= *r
++, d
= *data
++;
470 if (d
< t
|| d
> t
+ 7)
473 opx
->basereg
= (d
-t
)+
474 (ins
->rex
& REX_B
? 8 : 0);
475 opx
->segment
|= SEG_RMREG
;
482 opx
->offset
= (int8_t)*data
++;
483 opx
->segment
|= SEG_SIGNED
;
487 opx
->offset
= *data
++;
491 opx
->offset
= *data
++;
495 opx
->offset
= getu16(data
);
501 opx
->offset
= getu32(data
);
504 opx
->offset
= getu16(data
);
507 if (segsize
!= asize
)
508 opx
->disp_size
= asize
;
513 opx
->offset
= getu32(data
);
520 opx
->offset
= getu16(data
);
526 opx
->offset
= getu32(data
);
532 opx
->offset
= getu64(data
);
540 opx
->offset
= gets8(data
++);
541 opx
->segment
|= SEG_RELATIVE
;
545 opx
->offset
= getu64(data
);
550 opx
->offset
= gets16(data
);
552 opx
->segment
|= SEG_RELATIVE
;
553 opx
->segment
&= ~SEG_32BIT
;
557 opx
->segment
|= SEG_RELATIVE
;
559 opx
->offset
= gets16(data
);
561 opx
->segment
&= ~(SEG_32BIT
|SEG_64BIT
);
562 } else if (osize
== 32) {
563 opx
->offset
= gets32(data
);
565 opx
->segment
&= ~SEG_64BIT
;
566 opx
->segment
|= SEG_32BIT
;
568 if (segsize
!= osize
) {
570 (opx
->type
& ~SIZE_MASK
)
571 | ((osize
== 16) ? BITS16
: BITS32
);
576 opx
->offset
= gets32(data
);
578 opx
->segment
|= SEG_32BIT
| SEG_RELATIVE
;
587 opx
->segment
|= SEG_RMREG
;
588 data
= do_ea(data
, modrm
, asize
, segsize
, opy
, ins
);
591 opx
->basereg
= ((modrm
>> 3) & 7) + (ins
->rex
& REX_R
? 8 : 0);
596 if (s_field_for
== op1
) {
597 opx
->offset
= gets8(data
);
600 opx
->offset
= getu16(data
);
607 s_field_for
= (*data
& 0x02) ? op1
: -1;
608 if ((*data
++ & ~0x02) != *r
++)
613 if (s_field_for
== op1
) {
614 opx
->offset
= gets8(data
);
617 opx
->offset
= getu32(data
);
628 ins
->rex
|= REX_D
|REX_OC
;
633 data
= do_drex(data
, ins
);
640 uint8_t ximm
= *data
++;
642 ins
->oprs
[c
>> 3].basereg
= (ximm
>> 4) & regmask
;
643 ins
->oprs
[c
>> 3].segment
|= SEG_RMREG
;
644 ins
->oprs
[c
& 7].offset
= ximm
& 15;
650 uint8_t ximm
= *data
++;
656 ins
->oprs
[c
>> 4].basereg
= (ximm
>> 4) & regmask
;
657 ins
->oprs
[c
>> 4].segment
|= SEG_RMREG
;
663 uint8_t ximm
= *data
++;
666 ins
->oprs
[c
].basereg
= (ximm
>> 4) & regmask
;
667 ins
->oprs
[c
].segment
|= SEG_RMREG
;
681 if (((modrm
>> 3) & 07) != (c
& 07))
682 return false; /* spare field doesn't match up */
683 data
= do_ea(data
, modrm
, asize
, segsize
, opy
, ins
);
694 if ((prefix
->rex
& (REX_V
|REX_D
|REX_P
)) != REX_V
)
697 if ((vexm
& 0x1f) != prefix
->vex_m
)
700 switch (vexwlp
& 030) {
702 if (prefix
->rex
& REX_W
)
706 if (!(prefix
->rex
& REX_W
))
710 case 020: /* VEX.W is a don't care */
717 if ((vexwlp
& 007) != prefix
->vex_lp
)
720 opx
->segment
|= SEG_RMREG
;
721 opx
->basereg
= prefix
->vex_v
;
731 if ((prefix
->rex
& (REX_V
|REX_D
|REX_P
)) != REX_V
)
734 if ((vexm
& 0x1f) != prefix
->vex_m
)
737 switch (vexwlp
& 030) {
739 if (ins
->rex
& REX_W
)
743 if (!(ins
->rex
& REX_W
))
747 break; /* Need to do anything special here? */
750 if ((vexwlp
& 007) != prefix
->vex_lp
)
753 if (prefix
->vex_v
!= 0)
775 if (asize
!= segsize
)
789 if (prefix
->rex
& REX_B
)
794 if (prefix
->rex
& REX_X
)
799 if (prefix
->rex
& REX_R
)
804 if (prefix
->rex
& REX_W
)
823 if (osize
!= (segsize
== 16) ? 16 : 32)
830 ins
->rex
|= REX_W
; /* 64-bit only instruction */
836 if (!(ins
->rex
& (REX_P
|REX_W
)) || osize
!= 64)
847 int t
= *r
++, d
= *data
++;
848 if (d
< t
|| d
> t
+ 15)
851 ins
->condition
= d
- t
;
861 if (prefix
->rep
!= 0xF2)
867 if (prefix
->rep
!= 0xF3)
892 if (prefix
->wait
!= 0x9B)
898 ins
->oprs
[0].basereg
= (*data
++ >> 3) & 7;
902 if (prefix
->osp
|| prefix
->rep
)
907 if (!prefix
->osp
|| prefix
->rep
)
913 if (prefix
->osp
|| prefix
->rep
!= 0xf2)
919 if (prefix
->osp
|| prefix
->rep
!= 0xf3)
947 return false; /* Unknown code */
951 if (!vex_ok
&& (ins
->rex
& REX_V
))
954 /* REX cannot be combined with DREX or VEX */
955 if ((ins
->rex
& (REX_D
|REX_V
)) && (prefix
->rex
& REX_P
))
959 * Check for unused rep or a/o prefixes.
961 for (i
= 0; i
< t
->operands
; i
++) {
962 if (ins
->oprs
[i
].segment
!= SEG_RMREG
)
967 if (ins
->prefixes
[PPS_LREP
])
969 ins
->prefixes
[PPS_LREP
] = P_LOCK
;
972 if (ins
->prefixes
[PPS_LREP
])
974 ins
->prefixes
[PPS_LREP
] = drep
;
976 ins
->prefixes
[PPS_WAIT
] = dwait
;
978 if (osize
!= ((segsize
== 16) ? 16 : 32)) {
979 enum prefixes pfx
= 0;
993 if (ins
->prefixes
[PPS_OSIZE
])
995 ins
->prefixes
[PPS_OSIZE
] = pfx
;
998 if (!a_used
&& asize
!= segsize
) {
999 if (ins
->prefixes
[PPS_ASIZE
])
1001 ins
->prefixes
[PPS_ASIZE
] = asize
== 16 ? P_A16
: P_A32
;
1004 /* Fix: check for redundant REX prefixes */
1006 return data
- origdata
;
1009 /* Condition names for disassembly, sorted by x86 code */
1010 static const char * const condition_name
[16] = {
1011 "o", "no", "c", "nc", "z", "nz", "na", "a",
1012 "s", "ns", "pe", "po", "l", "nl", "ng", "g"
1015 int32_t disasm(uint8_t *data
, char *output
, int outbufsize
, int segsize
,
1016 int32_t offset
, int autosync
, uint32_t prefer
)
1018 const struct itemplate
* const *p
, * const *best_p
;
1019 const struct disasm_index
*ix
;
1021 int length
, best_length
= 0;
1023 int i
, slen
, colon
, n
;
1027 uint32_t goodness
, best
;
1029 struct prefix_info prefix
;
1032 memset(&ins
, 0, sizeof ins
);
1035 * Scan for prefixes.
1037 memset(&prefix
, 0, sizeof prefix
);
1038 prefix
.asize
= segsize
;
1039 prefix
.osize
= (segsize
== 64) ? 32 : segsize
;
1046 while (!end_prefix
) {
1050 prefix
.rep
= *data
++;
1054 prefix
.wait
= *data
++;
1058 prefix
.lock
= *data
++;
1062 segover
= "cs", prefix
.seg
= *data
++;
1065 segover
= "ss", prefix
.seg
= *data
++;
1068 segover
= "ds", prefix
.seg
= *data
++;
1071 segover
= "es", prefix
.seg
= *data
++;
1074 segover
= "fs", prefix
.seg
= *data
++;
1077 segover
= "gs", prefix
.seg
= *data
++;
1081 prefix
.osize
= (segsize
== 16) ? 32 : 16;
1082 prefix
.osp
= *data
++;
1085 prefix
.asize
= (segsize
== 32) ? 16 : 32;
1086 prefix
.asp
= *data
++;
1091 if (segsize
== 64 || (data
[1] & 0xc0) == 0xc0) {
1092 prefix
.vex
[0] = *data
++;
1093 prefix
.vex
[1] = *data
++;
1096 prefix
.vex_c
= RV_VEX
;
1098 if (prefix
.vex
[0] == 0xc4) {
1099 prefix
.vex
[2] = *data
++;
1100 prefix
.rex
|= (~prefix
.vex
[1] >> 5) & 7; /* REX_RXB */
1101 prefix
.rex
|= (prefix
.vex
[2] >> (7-3)) & REX_W
;
1102 prefix
.vex_m
= prefix
.vex
[1] & 0x1f;
1103 prefix
.vex_v
= (~prefix
.vex
[2] >> 3) & 15;
1104 prefix
.vex_lp
= prefix
.vex
[2] & 7;
1106 prefix
.rex
|= (~prefix
.vex
[1] >> (7-2)) & REX_R
;
1108 prefix
.vex_v
= (~prefix
.vex
[1] >> 3) & 15;
1109 prefix
.vex_lp
= prefix
.vex
[1] & 7;
1112 ix
= itable_vex
[RV_VEX
][prefix
.vex_m
][prefix
.vex_lp
];
1118 if ((data
[1] & 030) != 0 &&
1119 (segsize
== 64 || (data
[1] & 0xc0) == 0xc0)) {
1120 prefix
.vex
[0] = *data
++;
1121 prefix
.vex
[1] = *data
++;
1122 prefix
.vex
[2] = *data
++;
1125 prefix
.vex_c
= RV_XOP
;
1127 prefix
.rex
|= (~prefix
.vex
[1] >> 5) & 7; /* REX_RXB */
1128 prefix
.rex
|= (prefix
.vex
[2] >> (7-3)) & REX_W
;
1129 prefix
.vex_m
= prefix
.vex
[1] & 0x1f;
1130 prefix
.vex_v
= (~prefix
.vex
[2] >> 3) & 15;
1131 prefix
.vex_lp
= prefix
.vex
[2] & 7;
1133 ix
= itable_vex
[RV_XOP
][prefix
.vex_m
][prefix
.vex_lp
];
1154 if (segsize
== 64) {
1155 prefix
.rex
= *data
++;
1156 if (prefix
.rex
& REX_W
)
1168 best
= -1; /* Worst possible */
1170 best_pref
= INT_MAX
;
1173 return 0; /* No instruction table at all... */
1177 while (ix
->n
== -1) {
1178 ix
= (const struct disasm_index
*)ix
->p
+ *dp
++;
1181 p
= (const struct itemplate
* const *)ix
->p
;
1182 for (n
= ix
->n
; n
; n
--, p
++) {
1183 if ((length
= matches(*p
, data
, &prefix
, segsize
, &tmp_ins
))) {
1186 * Final check to make sure the types of r/m match up.
1187 * XXX: Need to make sure this is actually correct.
1189 for (i
= 0; i
< (*p
)->operands
; i
++) {
1190 if (!((*p
)->opd
[i
] & SAME_AS
) &&
1192 /* If it's a mem-only EA but we have a
1194 ((tmp_ins
.oprs
[i
].segment
& SEG_RMREG
) &&
1195 !(MEMORY
& ~(*p
)->opd
[i
])) ||
1196 /* If it's a reg-only EA but we have a memory
1198 (!(tmp_ins
.oprs
[i
].segment
& SEG_RMREG
) &&
1199 !(REG_EA
& ~(*p
)->opd
[i
]) &&
1200 !((*p
)->opd
[i
] & REG_SMASK
)) ||
1201 /* Register type mismatch (eg FS vs REG_DESS):
1203 ((((*p
)->opd
[i
] & (REGISTER
| FPUREG
)) ||
1204 (tmp_ins
.oprs
[i
].segment
& SEG_RMREG
)) &&
1205 !whichreg((*p
)->opd
[i
],
1206 tmp_ins
.oprs
[i
].basereg
, tmp_ins
.rex
))
1214 * Note: we always prefer instructions which incorporate
1215 * prefixes in the instructions themselves. This is to allow
1216 * e.g. PAUSE to be preferred to REP NOP, and deal with
1217 * MMX/SSE instructions where prefixes are used to select
1218 * between MMX and SSE register sets or outright opcode
1223 goodness
= ((*p
)->flags
& IF_PFMASK
) ^ prefer
;
1225 for (i
= 0; i
< MAXPREFIX
; i
++)
1226 if (tmp_ins
.prefixes
[i
])
1228 if (nprefix
< best_pref
||
1229 (nprefix
== best_pref
&& goodness
< best
)) {
1230 /* This is the best one found so far */
1233 best_pref
= nprefix
;
1234 best_length
= length
;
1242 return 0; /* no instruction was matched */
1244 /* Pick the best match */
1246 length
= best_length
;
1250 /* TODO: snprintf returns the value that the string would have if
1251 * the buffer were long enough, and not the actual length of
1252 * the returned string, so each instance of using the return
1253 * value of snprintf should actually be checked to assure that
1254 * the return value is "sane." Maybe a macro wrapper could
1255 * be used for that purpose.
1257 for (i
= 0; i
< MAXPREFIX
; i
++) {
1258 const char *prefix
= prefix_name(ins
.prefixes
[i
]);
1260 slen
+= snprintf(output
+slen
, outbufsize
-slen
, "%s ", prefix
);
1264 if (i
>= FIRST_COND_OPCODE
)
1265 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s%s",
1266 nasm_insn_names
[i
], condition_name
[ins
.condition
]);
1268 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1269 nasm_insn_names
[i
]);
1272 length
+= data
- origdata
; /* fix up for prefixes */
1273 for (i
= 0; i
< (*p
)->operands
; i
++) {
1274 opflags_t t
= (*p
)->opd
[i
];
1275 const operand
*o
= &ins
.oprs
[i
];
1279 o
= &ins
.oprs
[t
& ~SAME_AS
];
1280 t
= (*p
)->opd
[t
& ~SAME_AS
];
1283 output
[slen
++] = (colon
? ':' : i
== 0 ? ' ' : ',');
1286 if (o
->segment
& SEG_RELATIVE
) {
1287 offs
+= offset
+ length
;
1289 * sort out wraparound
1291 if (!(o
->segment
& (SEG_32BIT
|SEG_64BIT
)))
1293 else if (segsize
!= 64)
1297 * add sync marker, if autosync is on
1308 if ((t
& (REGISTER
| FPUREG
)) ||
1309 (o
->segment
& SEG_RMREG
)) {
1311 reg
= whichreg(t
, o
->basereg
, ins
.rex
);
1313 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "to ");
1314 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1315 nasm_reg_names
[reg
-EXPR_REG_START
]);
1316 } else if (!(UNITY
& ~t
)) {
1317 output
[slen
++] = '1';
1318 } else if (t
& IMMEDIATE
) {
1321 snprintf(output
+ slen
, outbufsize
- slen
, "byte ");
1322 if (o
->segment
& SEG_SIGNED
) {
1325 output
[slen
++] = '-';
1327 output
[slen
++] = '+';
1329 } else if (t
& BITS16
) {
1331 snprintf(output
+ slen
, outbufsize
- slen
, "word ");
1332 } else if (t
& BITS32
) {
1334 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1335 } else if (t
& BITS64
) {
1337 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1338 } else if (t
& NEAR
) {
1340 snprintf(output
+ slen
, outbufsize
- slen
, "near ");
1341 } else if (t
& SHORT
) {
1343 snprintf(output
+ slen
, outbufsize
- slen
, "short ");
1346 snprintf(output
+ slen
, outbufsize
- slen
, "0x%"PRIx64
"",
1348 } else if (!(MEM_OFFS
& ~t
)) {
1350 snprintf(output
+ slen
, outbufsize
- slen
,
1351 "[%s%s%s0x%"PRIx64
"]",
1352 (segover
? segover
: ""),
1353 (segover
? ":" : ""),
1354 (o
->disp_size
== 64 ? "qword " :
1355 o
->disp_size
== 32 ? "dword " :
1356 o
->disp_size
== 16 ? "word " : ""), offs
);
1358 } else if (!(REGMEM
& ~t
)) {
1359 int started
= false;
1362 snprintf(output
+ slen
, outbufsize
- slen
, "byte ");
1365 snprintf(output
+ slen
, outbufsize
- slen
, "word ");
1368 snprintf(output
+ slen
, outbufsize
- slen
, "dword ");
1371 snprintf(output
+ slen
, outbufsize
- slen
, "qword ");
1374 snprintf(output
+ slen
, outbufsize
- slen
, "tword ");
1377 snprintf(output
+ slen
, outbufsize
- slen
, "oword ");
1380 snprintf(output
+ slen
, outbufsize
- slen
, "yword ");
1382 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "far ");
1385 snprintf(output
+ slen
, outbufsize
- slen
, "near ");
1386 output
[slen
++] = '[';
1388 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1389 (o
->disp_size
== 64 ? "qword " :
1390 o
->disp_size
== 32 ? "dword " :
1391 o
->disp_size
== 16 ? "word " :
1393 if (o
->eaflags
& EAF_REL
)
1394 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "rel ");
1397 snprintf(output
+ slen
, outbufsize
- slen
, "%s:",
1401 if (o
->basereg
!= -1) {
1402 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1403 nasm_reg_names
[(o
->basereg
-EXPR_REG_START
)]);
1406 if (o
->indexreg
!= -1) {
1408 output
[slen
++] = '+';
1409 slen
+= snprintf(output
+ slen
, outbufsize
- slen
, "%s",
1410 nasm_reg_names
[(o
->indexreg
-EXPR_REG_START
)]);
1413 snprintf(output
+ slen
, outbufsize
- slen
, "*%d",
1419 if (o
->segment
& SEG_DISP8
) {
1421 uint8_t offset
= offs
;
1422 if ((int8_t)offset
< 0) {
1429 snprintf(output
+ slen
, outbufsize
- slen
, "%s0x%"PRIx8
"",
1431 } else if (o
->segment
& SEG_DISP16
) {
1433 uint16_t offset
= offs
;
1434 if ((int16_t)offset
< 0 && started
) {
1438 prefix
= started
? "+" : "";
1441 snprintf(output
+ slen
, outbufsize
- slen
,
1442 "%s0x%"PRIx16
"", prefix
, offset
);
1443 } else if (o
->segment
& SEG_DISP32
) {
1444 if (prefix
.asize
== 64) {
1446 uint64_t offset
= (int64_t)(int32_t)offs
;
1447 if ((int32_t)offs
< 0 && started
) {
1451 prefix
= started
? "+" : "";
1454 snprintf(output
+ slen
, outbufsize
- slen
,
1455 "%s0x%"PRIx64
"", prefix
, offset
);
1458 uint32_t offset
= offs
;
1459 if ((int32_t) offset
< 0 && started
) {
1463 prefix
= started
? "+" : "";
1466 snprintf(output
+ slen
, outbufsize
- slen
,
1467 "%s0x%"PRIx32
"", prefix
, offset
);
1470 output
[slen
++] = ']';
1473 snprintf(output
+ slen
, outbufsize
- slen
, "<operand%d>",
1477 output
[slen
] = '\0';
1478 if (segover
) { /* unused segment override */
1480 int count
= slen
+ 1;
1482 p
[count
+ 3] = p
[count
];
1483 strncpy(output
, segover
, 2);
1490 * This is called when we don't have a complete instruction. If it
1491 * is a standalone *single-byte* prefix show it as such, otherwise
1492 * print it as a literal.
1494 int32_t eatbyte(uint8_t *data
, char *output
, int outbufsize
, int segsize
)
1496 uint8_t byte
= *data
;
1497 const char *str
= NULL
;
1531 str
= (segsize
== 16) ? "o32" : "o16";
1534 str
= (segsize
== 32) ? "a16" : "a32";
1552 if (segsize
== 64) {
1553 snprintf(output
, outbufsize
, "rex%s%s%s%s%s",
1554 (byte
== REX_P
) ? "" : ".",
1555 (byte
& REX_W
) ? "w" : "",
1556 (byte
& REX_R
) ? "r" : "",
1557 (byte
& REX_X
) ? "x" : "",
1558 (byte
& REX_B
) ? "b" : "");
1561 /* else fall through */
1563 snprintf(output
, outbufsize
, "db 0x%02x", byte
);
1568 strcpy(output
, str
);