nasm.c: Tabs to spaces in nasm_verror_common
[nasm.git] / disasm.c
blob46cec8a686f119d4c93dbedc3ba4d9918bbfb761
1 /* ----------------------------------------------------------------------- *
2 *
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
9 * conditions are met:
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 * ----------------------------------------------------------------------- */
34 /*
35 * disasm.c where all the _work_ gets done in the Netwide Disassembler
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include <limits.h>
43 #include <inttypes.h>
45 #include "nasm.h"
46 #include "disasm.h"
47 #include "sync.h"
48 #include "insns.h"
49 #include "tables.h"
50 #include "regdis.h"
53 * Flags that go into the `segment' field of `insn' structures
54 * during disassembly.
56 #define SEG_RELATIVE 1
57 #define SEG_32BIT 2
58 #define SEG_RMREG 4
59 #define SEG_DISP8 8
60 #define SEG_DISP16 16
61 #define SEG_DISP32 32
62 #define SEG_NODISP 64
63 #define SEG_SIGNED 128
64 #define SEG_64BIT 256
67 * Prefix information
69 struct prefix_info {
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 */
81 uint8_t vex_v;
82 uint8_t vex_lp; /* VEX.LP fields */
83 uint32_t rex; /* REX prefix present */
86 #define getu8(x) (*(uint8_t *)(x))
87 #if X86_MEMORY
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))
92 #else
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);
105 #endif
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(opflags_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))
121 return R_AL;
122 if (!(REG_AX & ~regflags))
123 return R_AX;
124 if (!(REG_EAX & ~regflags))
125 return R_EAX;
126 if (!(REG_RAX & ~regflags))
127 return R_RAX;
128 if (!(REG_DL & ~regflags))
129 return R_DL;
130 if (!(REG_DX & ~regflags))
131 return R_DX;
132 if (!(REG_EDX & ~regflags))
133 return R_EDX;
134 if (!(REG_RDX & ~regflags))
135 return R_RDX;
136 if (!(REG_CL & ~regflags))
137 return R_CL;
138 if (!(REG_CX & ~regflags))
139 return R_CX;
140 if (!(REG_ECX & ~regflags))
141 return R_ECX;
142 if (!(REG_RCX & ~regflags))
143 return R_RCX;
144 if (!(FPU0 & ~regflags))
145 return R_ST0;
146 if (!(XMM0 & ~regflags))
147 return R_XMM0;
148 if (!(YMM0 & ~regflags))
149 return R_YMM0;
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)
162 return 0;
164 if (!(REG8 & ~regflags)) {
165 if (rex & (REX_P|REX_NH))
166 return nasm_rd_reg8_rex[regval];
167 else
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)) {
183 if (regval > 7)
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];
196 return 0;
200 * Process an effective address (ModRM) specification.
202 static uint8_t *do_ea(uint8_t *data, int modrm, int asize,
203 int segsize, enum ea_type type,
204 operand *op, insn *ins)
206 int mod, rm, scale, index, base;
207 int rex;
208 uint8_t sib = 0;
210 mod = (modrm >> 6) & 03;
211 rm = modrm & 07;
213 if (mod != 3 && asize != 16 && rm == 4)
214 sib = *data++;
216 rex = ins->rex;
218 if (mod == 3) { /* pure register version */
219 op->basereg = rm+(rex & REX_B ? 8 : 0);
220 op->segment |= SEG_RMREG;
221 return data;
224 op->disp_size = 0;
225 op->eaflags = 0;
227 if (asize == 16) {
229 * <mod> specifies the displacement size (none, byte or
230 * word), and <rm> specifies the register combination.
231 * Exception: mod=0,rm=6 does not specify [BP] as one might
232 * expect, but instead specifies [disp16].
235 if (type != EA_SCALAR)
236 return NULL;
238 op->indexreg = op->basereg = -1;
239 op->scale = 1; /* always, in 16 bits */
240 switch (rm) {
241 case 0:
242 op->basereg = R_BX;
243 op->indexreg = R_SI;
244 break;
245 case 1:
246 op->basereg = R_BX;
247 op->indexreg = R_DI;
248 break;
249 case 2:
250 op->basereg = R_BP;
251 op->indexreg = R_SI;
252 break;
253 case 3:
254 op->basereg = R_BP;
255 op->indexreg = R_DI;
256 break;
257 case 4:
258 op->basereg = R_SI;
259 break;
260 case 5:
261 op->basereg = R_DI;
262 break;
263 case 6:
264 op->basereg = R_BP;
265 break;
266 case 7:
267 op->basereg = R_BX;
268 break;
270 if (rm == 6 && mod == 0) { /* special case */
271 op->basereg = -1;
272 if (segsize != 16)
273 op->disp_size = 16;
274 mod = 2; /* fake disp16 */
276 switch (mod) {
277 case 0:
278 op->segment |= SEG_NODISP;
279 break;
280 case 1:
281 op->segment |= SEG_DISP8;
282 op->offset = (int8_t)*data++;
283 break;
284 case 2:
285 op->segment |= SEG_DISP16;
286 op->offset = *data++;
287 op->offset |= ((unsigned)*data++) << 8;
288 break;
290 return data;
291 } else {
293 * Once again, <mod> specifies displacement size (this time
294 * none, byte or *dword*), while <rm> specifies the base
295 * register. Again, [EBP] is missing, replaced by a pure
296 * disp32 (this time that's mod=0,rm=*5*) in 32-bit mode,
297 * and RIP-relative addressing in 64-bit mode.
299 * However, rm=4
300 * indicates not a single base register, but instead the
301 * presence of a SIB byte...
303 int a64 = asize == 64;
305 op->indexreg = -1;
307 if (a64)
308 op->basereg = nasm_rd_reg64[rm | ((rex & REX_B) ? 8 : 0)];
309 else
310 op->basereg = nasm_rd_reg32[rm | ((rex & REX_B) ? 8 : 0)];
312 if (rm == 5 && mod == 0) {
313 if (segsize == 64) {
314 op->eaflags |= EAF_REL;
315 op->segment |= SEG_RELATIVE;
316 mod = 2; /* fake disp32 */
319 if (asize != 64)
320 op->disp_size = asize;
322 op->basereg = -1;
323 mod = 2; /* fake disp32 */
327 if (rm == 4) { /* process SIB */
328 scale = (sib >> 6) & 03;
329 index = (sib >> 3) & 07;
330 base = sib & 07;
332 op->scale = 1 << scale;
334 if (type == EA_XMMVSIB)
335 op->indexreg = nasm_rd_xmmreg[index | ((rex & REX_X) ? 8 : 0)];
336 else if (type == EA_YMMVSIB)
337 op->indexreg = nasm_rd_ymmreg[index | ((rex & REX_X) ? 8 : 0)];
338 else if (index == 4 && !(rex & REX_X))
339 op->indexreg = -1; /* ESP/RSP cannot be an index */
340 else if (a64)
341 op->indexreg = nasm_rd_reg64[index | ((rex & REX_X) ? 8 : 0)];
342 else
343 op->indexreg = nasm_rd_reg32[index | ((rex & REX_X) ? 8 : 0)];
345 if (base == 5 && mod == 0) {
346 op->basereg = -1;
347 mod = 2; /* Fake disp32 */
348 } else if (a64)
349 op->basereg = nasm_rd_reg64[base | ((rex & REX_B) ? 8 : 0)];
350 else
351 op->basereg = nasm_rd_reg32[base | ((rex & REX_B) ? 8 : 0)];
353 if (segsize == 16)
354 op->disp_size = 32;
355 } else if (type != EA_SCALAR) {
356 /* Can't have VSIB without SIB */
357 return NULL;
360 switch (mod) {
361 case 0:
362 op->segment |= SEG_NODISP;
363 break;
364 case 1:
365 op->segment |= SEG_DISP8;
366 op->offset = gets8(data);
367 data++;
368 break;
369 case 2:
370 op->segment |= SEG_DISP32;
371 op->offset = gets32(data);
372 data += 4;
373 break;
375 return data;
380 * Determine whether the instruction template in t corresponds to the data
381 * stream in data. Return the number of bytes matched if so.
383 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
385 static int matches(const struct itemplate *t, uint8_t *data,
386 const struct prefix_info *prefix, int segsize, insn *ins)
388 uint8_t *r = (uint8_t *)(t->code);
389 uint8_t *origdata = data;
390 bool a_used = false, o_used = false;
391 enum prefixes drep = 0;
392 enum prefixes dwait = 0;
393 uint8_t lock = prefix->lock;
394 int osize = prefix->osize;
395 int asize = prefix->asize;
396 int i, c;
397 int op1, op2;
398 struct operand *opx, *opy;
399 uint8_t opex = 0;
400 int s_field_for = -1; /* No 144/154 series code encountered */
401 bool vex_ok = false;
402 int regmask = (segsize == 64) ? 15 : 7;
403 enum ea_type eat = EA_SCALAR;
405 for (i = 0; i < MAX_OPERANDS; i++) {
406 ins->oprs[i].segment = ins->oprs[i].disp_size =
407 (segsize == 64 ? SEG_64BIT : segsize == 32 ? SEG_32BIT : 0);
409 ins->condition = -1;
410 ins->rex = prefix->rex;
411 memset(ins->prefixes, 0, sizeof ins->prefixes);
413 if (t->flags & (segsize == 64 ? IF_NOLONG : IF_LONG))
414 return false;
416 if (prefix->rep == 0xF2)
417 drep = P_REPNE;
418 else if (prefix->rep == 0xF3)
419 drep = P_REP;
421 dwait = prefix->wait ? P_WAIT : 0;
423 while ((c = *r++) != 0) {
424 op1 = (c & 3) + ((opex & 1) << 2);
425 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
426 opx = &ins->oprs[op1];
427 opy = &ins->oprs[op2];
428 opex = 0;
430 switch (c) {
431 case 01:
432 case 02:
433 case 03:
434 case 04:
435 while (c--)
436 if (*r++ != *data++)
437 return false;
438 break;
440 case 05:
441 case 06:
442 case 07:
443 opex = c;
444 break;
446 case4(010):
448 int t = *r++, d = *data++;
449 if (d < t || d > t + 7)
450 return false;
451 else {
452 opx->basereg = (d-t)+
453 (ins->rex & REX_B ? 8 : 0);
454 opx->segment |= SEG_RMREG;
456 break;
459 case4(014):
460 case4(0274):
461 opx->offset = (int8_t)*data++;
462 opx->segment |= SEG_SIGNED;
463 break;
465 case4(020):
466 opx->offset = *data++;
467 break;
469 case4(024):
470 opx->offset = *data++;
471 break;
473 case4(030):
474 opx->offset = getu16(data);
475 data += 2;
476 break;
478 case4(034):
479 if (osize == 32) {
480 opx->offset = getu32(data);
481 data += 4;
482 } else {
483 opx->offset = getu16(data);
484 data += 2;
486 if (segsize != asize)
487 opx->disp_size = asize;
488 break;
490 case4(040):
491 case4(0254):
492 opx->offset = getu32(data);
493 data += 4;
494 break;
496 case4(044):
497 switch (asize) {
498 case 16:
499 opx->offset = getu16(data);
500 data += 2;
501 if (segsize != 16)
502 opx->disp_size = 16;
503 break;
504 case 32:
505 opx->offset = getu32(data);
506 data += 4;
507 if (segsize == 16)
508 opx->disp_size = 32;
509 break;
510 case 64:
511 opx->offset = getu64(data);
512 opx->disp_size = 64;
513 data += 8;
514 break;
516 break;
518 case4(050):
519 opx->offset = gets8(data++);
520 opx->segment |= SEG_RELATIVE;
521 break;
523 case4(054):
524 opx->offset = getu64(data);
525 data += 8;
526 break;
528 case4(060):
529 opx->offset = gets16(data);
530 data += 2;
531 opx->segment |= SEG_RELATIVE;
532 opx->segment &= ~SEG_32BIT;
533 break;
535 case4(064):
536 opx->segment |= SEG_RELATIVE;
537 if (osize == 16) {
538 opx->offset = gets16(data);
539 data += 2;
540 opx->segment &= ~(SEG_32BIT|SEG_64BIT);
541 } else if (osize == 32) {
542 opx->offset = gets32(data);
543 data += 4;
544 opx->segment &= ~SEG_64BIT;
545 opx->segment |= SEG_32BIT;
547 if (segsize != osize) {
548 opx->type =
549 (opx->type & ~SIZE_MASK)
550 | ((osize == 16) ? BITS16 : BITS32);
552 break;
554 case4(070):
555 opx->offset = gets32(data);
556 data += 4;
557 opx->segment |= SEG_32BIT | SEG_RELATIVE;
558 break;
560 case4(0100):
561 case4(0110):
562 case4(0120):
563 case4(0130):
565 int modrm = *data++;
566 opx->segment |= SEG_RMREG;
567 data = do_ea(data, modrm, asize, segsize, eat, opy, ins);
568 if (!data)
569 return false;
570 opx->basereg = ((modrm >> 3) & 7) + (ins->rex & REX_R ? 8 : 0);
571 break;
574 case4(0140):
575 if (s_field_for == op1) {
576 opx->offset = gets8(data);
577 data++;
578 } else {
579 opx->offset = getu16(data);
580 data += 2;
582 break;
584 case4(0144):
585 case4(0154):
586 s_field_for = (*data & 0x02) ? op1 : -1;
587 if ((*data++ & ~0x02) != *r++)
588 return false;
589 break;
591 case4(0150):
592 if (s_field_for == op1) {
593 opx->offset = gets8(data);
594 data++;
595 } else {
596 opx->offset = getu32(data);
597 data += 4;
599 break;
601 case 0172:
603 uint8_t ximm = *data++;
604 c = *r++;
605 ins->oprs[c >> 3].basereg = (ximm >> 4) & regmask;
606 ins->oprs[c >> 3].segment |= SEG_RMREG;
607 ins->oprs[c & 7].offset = ximm & 15;
609 break;
611 case 0173:
613 uint8_t ximm = *data++;
614 c = *r++;
616 if ((c ^ ximm) & 15)
617 return false;
619 ins->oprs[c >> 4].basereg = (ximm >> 4) & regmask;
620 ins->oprs[c >> 4].segment |= SEG_RMREG;
622 break;
624 case4(0174):
626 uint8_t ximm = *data++;
628 opx->basereg = (ximm >> 4) & regmask;
629 opx->segment |= SEG_RMREG;
631 break;
633 case4(0200):
634 case4(0204):
635 case4(0210):
636 case4(0214):
637 case4(0220):
638 case4(0224):
639 case4(0230):
640 case4(0234):
642 int modrm = *data++;
643 if (((modrm >> 3) & 07) != (c & 07))
644 return false; /* spare field doesn't match up */
645 data = do_ea(data, modrm, asize, segsize, eat, opy, ins);
646 if (!data)
647 return false;
648 break;
651 case4(0250):
652 if (s_field_for == op1) {
653 opx->offset = gets8(data);
654 data++;
655 } else {
656 opx->offset = gets32(data);
657 data += 4;
659 break;
661 case4(0260):
662 case 0270:
664 int vexm = *r++;
665 int vexwlp = *r++;
667 ins->rex |= REX_V;
668 if ((prefix->rex & (REX_V|REX_P)) != REX_V)
669 return false;
671 if ((vexm & 0x1f) != prefix->vex_m)
672 return false;
674 switch (vexwlp & 060) {
675 case 000:
676 if (prefix->rex & REX_W)
677 return false;
678 break;
679 case 020:
680 if (!(prefix->rex & REX_W))
681 return false;
682 ins->rex &= ~REX_W;
683 break;
684 case 040: /* VEX.W is a don't care */
685 ins->rex &= ~REX_W;
686 break;
687 case 060:
688 break;
691 /* The 010 bit of vexwlp is set if VEX.L is ignored */
692 if ((vexwlp ^ prefix->vex_lp) & ((vexwlp & 010) ? 03 : 07))
693 return false;
695 if (c == 0270) {
696 if (prefix->vex_v != 0)
697 return false;
698 } else {
699 opx->segment |= SEG_RMREG;
700 opx->basereg = prefix->vex_v;
702 vex_ok = true;
703 break;
706 case 0271:
707 if (prefix->rep == 0xF3)
708 drep = P_XRELEASE;
709 break;
711 case 0272:
712 if (prefix->rep == 0xF2)
713 drep = P_XACQUIRE;
714 else if (prefix->rep == 0xF3)
715 drep = P_XRELEASE;
716 break;
718 case 0273:
719 if (prefix->lock == 0xF0) {
720 if (prefix->rep == 0xF2)
721 drep = P_XACQUIRE;
722 else if (prefix->rep == 0xF3)
723 drep = P_XRELEASE;
725 break;
727 case 0310:
728 if (asize != 16)
729 return false;
730 else
731 a_used = true;
732 break;
734 case 0311:
735 if (asize != 32)
736 return false;
737 else
738 a_used = true;
739 break;
741 case 0312:
742 if (asize != segsize)
743 return false;
744 else
745 a_used = true;
746 break;
748 case 0313:
749 if (asize != 64)
750 return false;
751 else
752 a_used = true;
753 break;
755 case 0314:
756 if (prefix->rex & REX_B)
757 return false;
758 break;
760 case 0315:
761 if (prefix->rex & REX_X)
762 return false;
763 break;
765 case 0316:
766 if (prefix->rex & REX_R)
767 return false;
768 break;
770 case 0317:
771 if (prefix->rex & REX_W)
772 return false;
773 break;
775 case 0320:
776 if (osize != 16)
777 return false;
778 else
779 o_used = true;
780 break;
782 case 0321:
783 if (osize != 32)
784 return false;
785 else
786 o_used = true;
787 break;
789 case 0322:
790 if (osize != (segsize == 16) ? 16 : 32)
791 return false;
792 else
793 o_used = true;
794 break;
796 case 0323:
797 ins->rex |= REX_W; /* 64-bit only instruction */
798 osize = 64;
799 o_used = true;
800 break;
802 case 0324:
803 if (osize != 64)
804 return false;
805 o_used = true;
806 break;
808 case 0325:
809 ins->rex |= REX_NH;
810 break;
812 case 0330:
814 int t = *r++, d = *data++;
815 if (d < t || d > t + 15)
816 return false;
817 else
818 ins->condition = d - t;
819 break;
822 case 0331:
823 if (prefix->rep)
824 return false;
825 break;
827 case 0332:
828 if (prefix->rep != 0xF2)
829 return false;
830 drep = 0;
831 break;
833 case 0333:
834 if (prefix->rep != 0xF3)
835 return false;
836 drep = 0;
837 break;
839 case 0334:
840 if (lock) {
841 ins->rex |= REX_R;
842 lock = 0;
844 break;
846 case 0335:
847 if (drep == P_REP)
848 drep = P_REPE;
849 break;
851 case 0336:
852 case 0337:
853 break;
855 case 0340:
856 return false;
858 case 0341:
859 if (prefix->wait != 0x9B)
860 return false;
861 dwait = 0;
862 break;
864 case4(0344):
865 ins->oprs[0].basereg = (*data++ >> 3) & 7;
866 break;
868 case 0360:
869 if (prefix->osp || prefix->rep)
870 return false;
871 break;
873 case 0361:
874 if (!prefix->osp || prefix->rep)
875 return false;
876 o_used = true;
877 break;
879 case 0362:
880 if (prefix->osp || prefix->rep != 0xf2)
881 return false;
882 drep = 0;
883 break;
885 case 0363:
886 if (prefix->osp || prefix->rep != 0xf3)
887 return false;
888 drep = 0;
889 break;
891 case 0364:
892 if (prefix->osp)
893 return false;
894 break;
896 case 0365:
897 if (prefix->asp)
898 return false;
899 break;
901 case 0366:
902 if (!prefix->osp)
903 return false;
904 o_used = true;
905 break;
907 case 0367:
908 if (!prefix->asp)
909 return false;
910 a_used = true;
911 break;
913 case 0370:
914 case 0371:
915 break;
917 case 0374:
918 eat = EA_XMMVSIB;
919 break;
921 case 0375:
922 eat = EA_YMMVSIB;
923 break;
925 default:
926 return false; /* Unknown code */
930 if (!vex_ok && (ins->rex & REX_V))
931 return false;
933 /* REX cannot be combined with VEX */
934 if ((ins->rex & REX_V) && (prefix->rex & REX_P))
935 return false;
938 * Check for unused rep or a/o prefixes.
940 for (i = 0; i < t->operands; i++) {
941 if (ins->oprs[i].segment != SEG_RMREG)
942 a_used = true;
945 if (lock) {
946 if (ins->prefixes[PPS_LOCK])
947 return false;
948 ins->prefixes[PPS_LOCK] = P_LOCK;
950 if (drep) {
951 if (ins->prefixes[PPS_REP])
952 return false;
953 ins->prefixes[PPS_REP] = drep;
955 ins->prefixes[PPS_WAIT] = dwait;
956 if (!o_used) {
957 if (osize != ((segsize == 16) ? 16 : 32)) {
958 enum prefixes pfx = 0;
960 switch (osize) {
961 case 16:
962 pfx = P_O16;
963 break;
964 case 32:
965 pfx = P_O32;
966 break;
967 case 64:
968 pfx = P_O64;
969 break;
972 if (ins->prefixes[PPS_OSIZE])
973 return false;
974 ins->prefixes[PPS_OSIZE] = pfx;
977 if (!a_used && asize != segsize) {
978 if (ins->prefixes[PPS_ASIZE])
979 return false;
980 ins->prefixes[PPS_ASIZE] = asize == 16 ? P_A16 : P_A32;
983 /* Fix: check for redundant REX prefixes */
985 return data - origdata;
988 /* Condition names for disassembly, sorted by x86 code */
989 static const char * const condition_name[16] = {
990 "o", "no", "c", "nc", "z", "nz", "na", "a",
991 "s", "ns", "pe", "po", "l", "nl", "ng", "g"
994 int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize,
995 int32_t offset, int autosync, uint32_t prefer)
997 const struct itemplate * const *p, * const *best_p;
998 const struct disasm_index *ix;
999 uint8_t *dp;
1000 int length, best_length = 0;
1001 char *segover;
1002 int i, slen, colon, n;
1003 uint8_t *origdata;
1004 int works;
1005 insn tmp_ins, ins;
1006 uint32_t goodness, best;
1007 int best_pref;
1008 struct prefix_info prefix;
1009 bool end_prefix;
1011 memset(&ins, 0, sizeof ins);
1014 * Scan for prefixes.
1016 memset(&prefix, 0, sizeof prefix);
1017 prefix.asize = segsize;
1018 prefix.osize = (segsize == 64) ? 32 : segsize;
1019 segover = NULL;
1020 origdata = data;
1022 ix = itable;
1024 end_prefix = false;
1025 while (!end_prefix) {
1026 switch (*data) {
1027 case 0xF2:
1028 case 0xF3:
1029 prefix.rep = *data++;
1030 break;
1032 case 0x9B:
1033 prefix.wait = *data++;
1034 break;
1036 case 0xF0:
1037 prefix.lock = *data++;
1038 break;
1040 case 0x2E:
1041 segover = "cs", prefix.seg = *data++;
1042 break;
1043 case 0x36:
1044 segover = "ss", prefix.seg = *data++;
1045 break;
1046 case 0x3E:
1047 segover = "ds", prefix.seg = *data++;
1048 break;
1049 case 0x26:
1050 segover = "es", prefix.seg = *data++;
1051 break;
1052 case 0x64:
1053 segover = "fs", prefix.seg = *data++;
1054 break;
1055 case 0x65:
1056 segover = "gs", prefix.seg = *data++;
1057 break;
1059 case 0x66:
1060 prefix.osize = (segsize == 16) ? 32 : 16;
1061 prefix.osp = *data++;
1062 break;
1063 case 0x67:
1064 prefix.asize = (segsize == 32) ? 16 : 32;
1065 prefix.asp = *data++;
1066 break;
1068 case 0xC4:
1069 case 0xC5:
1070 if (segsize == 64 || (data[1] & 0xc0) == 0xc0) {
1071 prefix.vex[0] = *data++;
1072 prefix.vex[1] = *data++;
1074 prefix.rex = REX_V;
1075 prefix.vex_c = RV_VEX;
1077 if (prefix.vex[0] == 0xc4) {
1078 prefix.vex[2] = *data++;
1079 prefix.rex |= (~prefix.vex[1] >> 5) & 7; /* REX_RXB */
1080 prefix.rex |= (prefix.vex[2] >> (7-3)) & REX_W;
1081 prefix.vex_m = prefix.vex[1] & 0x1f;
1082 prefix.vex_v = (~prefix.vex[2] >> 3) & 15;
1083 prefix.vex_lp = prefix.vex[2] & 7;
1084 } else {
1085 prefix.rex |= (~prefix.vex[1] >> (7-2)) & REX_R;
1086 prefix.vex_m = 1;
1087 prefix.vex_v = (~prefix.vex[1] >> 3) & 15;
1088 prefix.vex_lp = prefix.vex[1] & 7;
1091 ix = itable_vex[RV_VEX][prefix.vex_m][prefix.vex_lp & 3];
1093 end_prefix = true;
1094 break;
1096 case 0x8F:
1097 if ((data[1] & 030) != 0 &&
1098 (segsize == 64 || (data[1] & 0xc0) == 0xc0)) {
1099 prefix.vex[0] = *data++;
1100 prefix.vex[1] = *data++;
1101 prefix.vex[2] = *data++;
1103 prefix.rex = REX_V;
1104 prefix.vex_c = RV_XOP;
1106 prefix.rex |= (~prefix.vex[1] >> 5) & 7; /* REX_RXB */
1107 prefix.rex |= (prefix.vex[2] >> (7-3)) & REX_W;
1108 prefix.vex_m = prefix.vex[1] & 0x1f;
1109 prefix.vex_v = (~prefix.vex[2] >> 3) & 15;
1110 prefix.vex_lp = prefix.vex[2] & 7;
1112 ix = itable_vex[RV_XOP][prefix.vex_m][prefix.vex_lp & 3];
1114 end_prefix = true;
1115 break;
1117 case REX_P + 0x0:
1118 case REX_P + 0x1:
1119 case REX_P + 0x2:
1120 case REX_P + 0x3:
1121 case REX_P + 0x4:
1122 case REX_P + 0x5:
1123 case REX_P + 0x6:
1124 case REX_P + 0x7:
1125 case REX_P + 0x8:
1126 case REX_P + 0x9:
1127 case REX_P + 0xA:
1128 case REX_P + 0xB:
1129 case REX_P + 0xC:
1130 case REX_P + 0xD:
1131 case REX_P + 0xE:
1132 case REX_P + 0xF:
1133 if (segsize == 64) {
1134 prefix.rex = *data++;
1135 if (prefix.rex & REX_W)
1136 prefix.osize = 64;
1138 end_prefix = true;
1139 break;
1141 default:
1142 end_prefix = true;
1143 break;
1147 best = -1; /* Worst possible */
1148 best_p = NULL;
1149 best_pref = INT_MAX;
1151 if (!ix)
1152 return 0; /* No instruction table at all... */
1154 dp = data;
1155 ix += *dp++;
1156 while (ix->n == -1) {
1157 ix = (const struct disasm_index *)ix->p + *dp++;
1160 p = (const struct itemplate * const *)ix->p;
1161 for (n = ix->n; n; n--, p++) {
1162 if ((length = matches(*p, data, &prefix, segsize, &tmp_ins))) {
1163 works = true;
1165 * Final check to make sure the types of r/m match up.
1166 * XXX: Need to make sure this is actually correct.
1168 for (i = 0; i < (*p)->operands; i++) {
1169 if (!((*p)->opd[i] & SAME_AS) &&
1171 /* If it's a mem-only EA but we have a
1172 register, die. */
1173 ((tmp_ins.oprs[i].segment & SEG_RMREG) &&
1174 is_class(MEMORY, (*p)->opd[i])) ||
1175 /* If it's a reg-only EA but we have a memory
1176 ref, die. */
1177 (!(tmp_ins.oprs[i].segment & SEG_RMREG) &&
1178 !(REG_EA & ~(*p)->opd[i]) &&
1179 !((*p)->opd[i] & REG_SMASK)) ||
1180 /* Register type mismatch (eg FS vs REG_DESS):
1181 die. */
1182 ((((*p)->opd[i] & (REGISTER | FPUREG)) ||
1183 (tmp_ins.oprs[i].segment & SEG_RMREG)) &&
1184 !whichreg((*p)->opd[i],
1185 tmp_ins.oprs[i].basereg, tmp_ins.rex))
1186 )) {
1187 works = false;
1188 break;
1193 * Note: we always prefer instructions which incorporate
1194 * prefixes in the instructions themselves. This is to allow
1195 * e.g. PAUSE to be preferred to REP NOP, and deal with
1196 * MMX/SSE instructions where prefixes are used to select
1197 * between MMX and SSE register sets or outright opcode
1198 * selection.
1200 if (works) {
1201 int i, nprefix;
1202 goodness = ((*p)->flags & IF_PFMASK) ^ prefer;
1203 nprefix = 0;
1204 for (i = 0; i < MAXPREFIX; i++)
1205 if (tmp_ins.prefixes[i])
1206 nprefix++;
1207 if (nprefix < best_pref ||
1208 (nprefix == best_pref && goodness < best)) {
1209 /* This is the best one found so far */
1210 best = goodness;
1211 best_p = p;
1212 best_pref = nprefix;
1213 best_length = length;
1214 ins = tmp_ins;
1220 if (!best_p)
1221 return 0; /* no instruction was matched */
1223 /* Pick the best match */
1224 p = best_p;
1225 length = best_length;
1227 slen = 0;
1229 /* TODO: snprintf returns the value that the string would have if
1230 * the buffer were long enough, and not the actual length of
1231 * the returned string, so each instance of using the return
1232 * value of snprintf should actually be checked to assure that
1233 * the return value is "sane." Maybe a macro wrapper could
1234 * be used for that purpose.
1236 for (i = 0; i < MAXPREFIX; i++) {
1237 const char *prefix = prefix_name(ins.prefixes[i]);
1238 if (prefix)
1239 slen += snprintf(output+slen, outbufsize-slen, "%s ", prefix);
1242 i = (*p)->opcode;
1243 if (i >= FIRST_COND_OPCODE)
1244 slen += snprintf(output + slen, outbufsize - slen, "%s%s",
1245 nasm_insn_names[i], condition_name[ins.condition]);
1246 else
1247 slen += snprintf(output + slen, outbufsize - slen, "%s",
1248 nasm_insn_names[i]);
1250 colon = false;
1251 length += data - origdata; /* fix up for prefixes */
1252 for (i = 0; i < (*p)->operands; i++) {
1253 opflags_t t = (*p)->opd[i];
1254 const operand *o = &ins.oprs[i];
1255 int64_t offs;
1257 if (t & SAME_AS) {
1258 o = &ins.oprs[t & ~SAME_AS];
1259 t = (*p)->opd[t & ~SAME_AS];
1262 output[slen++] = (colon ? ':' : i == 0 ? ' ' : ',');
1264 offs = o->offset;
1265 if (o->segment & SEG_RELATIVE) {
1266 offs += offset + length;
1268 * sort out wraparound
1270 if (!(o->segment & (SEG_32BIT|SEG_64BIT)))
1271 offs &= 0xffff;
1272 else if (segsize != 64)
1273 offs &= 0xffffffff;
1276 * add sync marker, if autosync is on
1278 if (autosync)
1279 add_sync(offs, 0L);
1282 if (t & COLON)
1283 colon = true;
1284 else
1285 colon = false;
1287 if ((t & (REGISTER | FPUREG)) ||
1288 (o->segment & SEG_RMREG)) {
1289 enum reg_enum reg;
1290 reg = whichreg(t, o->basereg, ins.rex);
1291 if (t & TO)
1292 slen += snprintf(output + slen, outbufsize - slen, "to ");
1293 slen += snprintf(output + slen, outbufsize - slen, "%s",
1294 nasm_reg_names[reg-EXPR_REG_START]);
1295 } else if (!(UNITY & ~t)) {
1296 output[slen++] = '1';
1297 } else if (t & IMMEDIATE) {
1298 if (t & BITS8) {
1299 slen +=
1300 snprintf(output + slen, outbufsize - slen, "byte ");
1301 if (o->segment & SEG_SIGNED) {
1302 if (offs < 0) {
1303 offs *= -1;
1304 output[slen++] = '-';
1305 } else
1306 output[slen++] = '+';
1308 } else if (t & BITS16) {
1309 slen +=
1310 snprintf(output + slen, outbufsize - slen, "word ");
1311 } else if (t & BITS32) {
1312 slen +=
1313 snprintf(output + slen, outbufsize - slen, "dword ");
1314 } else if (t & BITS64) {
1315 slen +=
1316 snprintf(output + slen, outbufsize - slen, "qword ");
1317 } else if (t & NEAR) {
1318 slen +=
1319 snprintf(output + slen, outbufsize - slen, "near ");
1320 } else if (t & SHORT) {
1321 slen +=
1322 snprintf(output + slen, outbufsize - slen, "short ");
1324 slen +=
1325 snprintf(output + slen, outbufsize - slen, "0x%"PRIx64"",
1326 offs);
1327 } else if (!(MEM_OFFS & ~t)) {
1328 slen +=
1329 snprintf(output + slen, outbufsize - slen,
1330 "[%s%s%s0x%"PRIx64"]",
1331 (segover ? segover : ""),
1332 (segover ? ":" : ""),
1333 (o->disp_size == 64 ? "qword " :
1334 o->disp_size == 32 ? "dword " :
1335 o->disp_size == 16 ? "word " : ""), offs);
1336 segover = NULL;
1337 } else if (is_class(REGMEM, t)) {
1338 int started = false;
1339 if (t & BITS8)
1340 slen +=
1341 snprintf(output + slen, outbufsize - slen, "byte ");
1342 if (t & BITS16)
1343 slen +=
1344 snprintf(output + slen, outbufsize - slen, "word ");
1345 if (t & BITS32)
1346 slen +=
1347 snprintf(output + slen, outbufsize - slen, "dword ");
1348 if (t & BITS64)
1349 slen +=
1350 snprintf(output + slen, outbufsize - slen, "qword ");
1351 if (t & BITS80)
1352 slen +=
1353 snprintf(output + slen, outbufsize - slen, "tword ");
1354 if (t & BITS128)
1355 slen +=
1356 snprintf(output + slen, outbufsize - slen, "oword ");
1357 if (t & BITS256)
1358 slen +=
1359 snprintf(output + slen, outbufsize - slen, "yword ");
1360 if (t & FAR)
1361 slen += snprintf(output + slen, outbufsize - slen, "far ");
1362 if (t & NEAR)
1363 slen +=
1364 snprintf(output + slen, outbufsize - slen, "near ");
1365 output[slen++] = '[';
1366 if (o->disp_size)
1367 slen += snprintf(output + slen, outbufsize - slen, "%s",
1368 (o->disp_size == 64 ? "qword " :
1369 o->disp_size == 32 ? "dword " :
1370 o->disp_size == 16 ? "word " :
1371 ""));
1372 if (o->eaflags & EAF_REL)
1373 slen += snprintf(output + slen, outbufsize - slen, "rel ");
1374 if (segover) {
1375 slen +=
1376 snprintf(output + slen, outbufsize - slen, "%s:",
1377 segover);
1378 segover = NULL;
1380 if (o->basereg != -1) {
1381 slen += snprintf(output + slen, outbufsize - slen, "%s",
1382 nasm_reg_names[(o->basereg-EXPR_REG_START)]);
1383 started = true;
1385 if (o->indexreg != -1) {
1386 if (started)
1387 output[slen++] = '+';
1388 slen += snprintf(output + slen, outbufsize - slen, "%s",
1389 nasm_reg_names[(o->indexreg-EXPR_REG_START)]);
1390 if (o->scale > 1)
1391 slen +=
1392 snprintf(output + slen, outbufsize - slen, "*%d",
1393 o->scale);
1394 started = true;
1398 if (o->segment & SEG_DISP8) {
1399 const char *prefix;
1400 uint8_t offset = offs;
1401 if ((int8_t)offset < 0) {
1402 prefix = "-";
1403 offset = -offset;
1404 } else {
1405 prefix = "+";
1407 slen +=
1408 snprintf(output + slen, outbufsize - slen, "%s0x%"PRIx8"",
1409 prefix, offset);
1410 } else if (o->segment & SEG_DISP16) {
1411 const char *prefix;
1412 uint16_t offset = offs;
1413 if ((int16_t)offset < 0 && started) {
1414 offset = -offset;
1415 prefix = "-";
1416 } else {
1417 prefix = started ? "+" : "";
1419 slen +=
1420 snprintf(output + slen, outbufsize - slen,
1421 "%s0x%"PRIx16"", prefix, offset);
1422 } else if (o->segment & SEG_DISP32) {
1423 if (prefix.asize == 64) {
1424 const char *prefix;
1425 uint64_t offset = (int64_t)(int32_t)offs;
1426 if ((int32_t)offs < 0 && started) {
1427 offset = -offset;
1428 prefix = "-";
1429 } else {
1430 prefix = started ? "+" : "";
1432 slen +=
1433 snprintf(output + slen, outbufsize - slen,
1434 "%s0x%"PRIx64"", prefix, offset);
1435 } else {
1436 const char *prefix;
1437 uint32_t offset = offs;
1438 if ((int32_t) offset < 0 && started) {
1439 offset = -offset;
1440 prefix = "-";
1441 } else {
1442 prefix = started ? "+" : "";
1444 slen +=
1445 snprintf(output + slen, outbufsize - slen,
1446 "%s0x%"PRIx32"", prefix, offset);
1449 output[slen++] = ']';
1450 } else {
1451 slen +=
1452 snprintf(output + slen, outbufsize - slen, "<operand%d>",
1456 output[slen] = '\0';
1457 if (segover) { /* unused segment override */
1458 char *p = output;
1459 int count = slen + 1;
1460 while (count--)
1461 p[count + 3] = p[count];
1462 strncpy(output, segover, 2);
1463 output[2] = ' ';
1465 return length;
1469 * This is called when we don't have a complete instruction. If it
1470 * is a standalone *single-byte* prefix show it as such, otherwise
1471 * print it as a literal.
1473 int32_t eatbyte(uint8_t *data, char *output, int outbufsize, int segsize)
1475 uint8_t byte = *data;
1476 const char *str = NULL;
1478 switch (byte) {
1479 case 0xF2:
1480 str = "repne";
1481 break;
1482 case 0xF3:
1483 str = "rep";
1484 break;
1485 case 0x9B:
1486 str = "wait";
1487 break;
1488 case 0xF0:
1489 str = "lock";
1490 break;
1491 case 0x2E:
1492 str = "cs";
1493 break;
1494 case 0x36:
1495 str = "ss";
1496 break;
1497 case 0x3E:
1498 str = "ss";
1499 break;
1500 case 0x26:
1501 str = "es";
1502 break;
1503 case 0x64:
1504 str = "fs";
1505 break;
1506 case 0x65:
1507 str = "gs";
1508 break;
1509 case 0x66:
1510 str = (segsize == 16) ? "o32" : "o16";
1511 break;
1512 case 0x67:
1513 str = (segsize == 32) ? "a16" : "a32";
1514 break;
1515 case REX_P + 0x0:
1516 case REX_P + 0x1:
1517 case REX_P + 0x2:
1518 case REX_P + 0x3:
1519 case REX_P + 0x4:
1520 case REX_P + 0x5:
1521 case REX_P + 0x6:
1522 case REX_P + 0x7:
1523 case REX_P + 0x8:
1524 case REX_P + 0x9:
1525 case REX_P + 0xA:
1526 case REX_P + 0xB:
1527 case REX_P + 0xC:
1528 case REX_P + 0xD:
1529 case REX_P + 0xE:
1530 case REX_P + 0xF:
1531 if (segsize == 64) {
1532 snprintf(output, outbufsize, "rex%s%s%s%s%s",
1533 (byte == REX_P) ? "" : ".",
1534 (byte & REX_W) ? "w" : "",
1535 (byte & REX_R) ? "r" : "",
1536 (byte & REX_X) ? "x" : "",
1537 (byte & REX_B) ? "b" : "");
1538 break;
1540 /* else fall through */
1541 default:
1542 snprintf(output, outbufsize, "db 0x%02x", byte);
1543 break;
1546 if (str)
1547 snprintf(output, outbufsize, "%s", str);
1549 return 1;