Macro file copyright header
[nasm.git] / disasm.c
bloba83c8068aac4a2bc8d27d529a7cc43240a1826ac
1 /* ----------------------------------------------------------------------- *
2 *
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
12 * reference.
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
21 * conditions are met:
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 * ----------------------------------------------------------------------- */
46 /*
47 * disasm.c where all the _work_ gets done in the Netwide Disassembler
50 #include "compiler.h"
52 #include <stdio.h>
53 #include <string.h>
54 #include <limits.h>
55 #include <inttypes.h>
57 #include "nasm.h"
58 #include "disasm.h"
59 #include "sync.h"
60 #include "insns.h"
61 #include "tables.h"
62 #include "regdis.h"
65 * Flags that go into the `segment' field of `insn' structures
66 * during disassembly.
68 #define SEG_RELATIVE 1
69 #define SEG_32BIT 2
70 #define SEG_RMREG 4
71 #define SEG_DISP8 8
72 #define SEG_DISP16 16
73 #define SEG_DISP32 32
74 #define SEG_NODISP 64
75 #define SEG_SIGNED 128
76 #define SEG_64BIT 256
79 * Prefix information
81 struct prefix_info {
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 */
93 uint8_t vex_v;
94 uint8_t vex_lp; /* VEX.LP fields */
95 uint32_t rex; /* REX prefix present */
98 #define getu8(x) (*(uint8_t *)(x))
99 #if X86_MEMORY
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))
104 #else
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);
117 #endif
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))
133 return R_AL;
134 if (!(REG_AX & ~regflags))
135 return R_AX;
136 if (!(REG_EAX & ~regflags))
137 return R_EAX;
138 if (!(REG_RAX & ~regflags))
139 return R_RAX;
140 if (!(REG_DL & ~regflags))
141 return R_DL;
142 if (!(REG_DX & ~regflags))
143 return R_DX;
144 if (!(REG_EDX & ~regflags))
145 return R_EDX;
146 if (!(REG_RDX & ~regflags))
147 return R_RDX;
148 if (!(REG_CL & ~regflags))
149 return R_CL;
150 if (!(REG_CX & ~regflags))
151 return R_CX;
152 if (!(REG_ECX & ~regflags))
153 return R_ECX;
154 if (!(REG_RCX & ~regflags))
155 return R_RCX;
156 if (!(FPU0 & ~regflags))
157 return R_ST0;
158 if (!(XMM0 & ~regflags))
159 return R_XMM0;
160 if (!(YMM0 & ~regflags))
161 return R_YMM0;
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)
174 return 0;
176 if (!(REG8 & ~regflags)) {
177 if (rex & (REX_P|REX_NH))
178 return nasm_rd_reg8_rex[regval];
179 else
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)) {
195 if (regval > 7)
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];
208 return 0;
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;
225 return data;
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;
236 int rex;
237 uint8_t sib = 0;
239 mod = (modrm >> 6) & 03;
240 rm = modrm & 07;
242 if (mod != 3 && rm == 4 && asize != 16)
243 sib = *data++;
245 if (ins->rex & REX_D) {
246 data = do_drex(data, ins);
247 if (!data)
248 return NULL;
250 rex = ins->rex;
252 if (mod == 3) { /* pure register version */
253 op->basereg = rm+(rex & REX_B ? 8 : 0);
254 op->segment |= SEG_RMREG;
255 return data;
258 op->disp_size = 0;
259 op->eaflags = 0;
261 if (asize == 16) {
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 */
270 switch (rm) {
271 case 0:
272 op->basereg = R_BX;
273 op->indexreg = R_SI;
274 break;
275 case 1:
276 op->basereg = R_BX;
277 op->indexreg = R_DI;
278 break;
279 case 2:
280 op->basereg = R_BP;
281 op->indexreg = R_SI;
282 break;
283 case 3:
284 op->basereg = R_BP;
285 op->indexreg = R_DI;
286 break;
287 case 4:
288 op->basereg = R_SI;
289 break;
290 case 5:
291 op->basereg = R_DI;
292 break;
293 case 6:
294 op->basereg = R_BP;
295 break;
296 case 7:
297 op->basereg = R_BX;
298 break;
300 if (rm == 6 && mod == 0) { /* special case */
301 op->basereg = -1;
302 if (segsize != 16)
303 op->disp_size = 16;
304 mod = 2; /* fake disp16 */
306 switch (mod) {
307 case 0:
308 op->segment |= SEG_NODISP;
309 break;
310 case 1:
311 op->segment |= SEG_DISP8;
312 op->offset = (int8_t)*data++;
313 break;
314 case 2:
315 op->segment |= SEG_DISP16;
316 op->offset = *data++;
317 op->offset |= ((unsigned)*data++) << 8;
318 break;
320 return data;
321 } else {
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.
329 * However, rm=4
330 * indicates not a single base register, but instead the
331 * presence of a SIB byte...
333 int a64 = asize == 64;
335 op->indexreg = -1;
337 if (a64)
338 op->basereg = nasm_rd_reg64[rm | ((rex & REX_B) ? 8 : 0)];
339 else
340 op->basereg = nasm_rd_reg32[rm | ((rex & REX_B) ? 8 : 0)];
342 if (rm == 5 && mod == 0) {
343 if (segsize == 64) {
344 op->eaflags |= EAF_REL;
345 op->segment |= SEG_RELATIVE;
346 mod = 2; /* fake disp32 */
349 if (asize != 64)
350 op->disp_size = asize;
352 op->basereg = -1;
353 mod = 2; /* fake disp32 */
356 if (rm == 4) { /* process SIB */
357 scale = (sib >> 6) & 03;
358 index = (sib >> 3) & 07;
359 base = sib & 07;
361 op->scale = 1 << scale;
363 if (index == 4 && !(rex & REX_X))
364 op->indexreg = -1; /* ESP/RSP cannot be an index */
365 else if (a64)
366 op->indexreg = nasm_rd_reg64[index | ((rex & REX_X) ? 8 : 0)];
367 else
368 op->indexreg = nasm_rd_reg32[index | ((rex & REX_X) ? 8 : 0)];
370 if (base == 5 && mod == 0) {
371 op->basereg = -1;
372 mod = 2; /* Fake disp32 */
373 } else if (a64)
374 op->basereg = nasm_rd_reg64[base | ((rex & REX_B) ? 8 : 0)];
375 else
376 op->basereg = nasm_rd_reg32[base | ((rex & REX_B) ? 8 : 0)];
378 if (segsize == 16)
379 op->disp_size = 32;
382 switch (mod) {
383 case 0:
384 op->segment |= SEG_NODISP;
385 break;
386 case 1:
387 op->segment |= SEG_DISP8;
388 op->offset = gets8(data);
389 data++;
390 break;
391 case 2:
392 op->segment |= SEG_DISP32;
393 op->offset = gets32(data);
394 data += 4;
395 break;
397 return 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;
418 int i, c;
419 int op1, op2;
420 struct operand *opx, *opy;
421 uint8_t opex = 0;
422 int s_field_for = -1; /* No 144/154 series code encountered */
423 bool vex_ok = false;
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);
430 ins->condition = -1;
431 ins->rex = prefix->rex;
432 memset(ins->prefixes, 0, sizeof ins->prefixes);
434 if (t->flags & (segsize == 64 ? IF_NOLONG : IF_LONG))
435 return false;
437 if (prefix->rep == 0xF2)
438 drep = P_REPNE;
439 else if (prefix->rep == 0xF3)
440 drep = P_REP;
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];
449 opex = 0;
451 switch (c) {
452 case 01:
453 case 02:
454 case 03:
455 case 04:
456 while (c--)
457 if (*r++ != *data++)
458 return false;
459 break;
461 case 05:
462 case 06:
463 case 07:
464 opex = c;
465 break;
467 case4(010):
469 int t = *r++, d = *data++;
470 if (d < t || d > t + 7)
471 return false;
472 else {
473 opx->basereg = (d-t)+
474 (ins->rex & REX_B ? 8 : 0);
475 opx->segment |= SEG_RMREG;
477 break;
480 case4(014):
481 case4(0274):
482 opx->offset = (int8_t)*data++;
483 opx->segment |= SEG_SIGNED;
484 break;
486 case4(020):
487 opx->offset = *data++;
488 break;
490 case4(024):
491 opx->offset = *data++;
492 break;
494 case4(030):
495 opx->offset = getu16(data);
496 data += 2;
497 break;
499 case4(034):
500 if (osize == 32) {
501 opx->offset = getu32(data);
502 data += 4;
503 } else {
504 opx->offset = getu16(data);
505 data += 2;
507 if (segsize != asize)
508 opx->disp_size = asize;
509 break;
511 case4(040):
512 case4(0254):
513 opx->offset = getu32(data);
514 data += 4;
515 break;
517 case4(044):
518 switch (asize) {
519 case 16:
520 opx->offset = getu16(data);
521 data += 2;
522 if (segsize != 16)
523 opx->disp_size = 16;
524 break;
525 case 32:
526 opx->offset = getu32(data);
527 data += 4;
528 if (segsize == 16)
529 opx->disp_size = 32;
530 break;
531 case 64:
532 opx->offset = getu64(data);
533 opx->disp_size = 64;
534 data += 8;
535 break;
537 break;
539 case4(050):
540 opx->offset = gets8(data++);
541 opx->segment |= SEG_RELATIVE;
542 break;
544 case4(054):
545 opx->offset = getu64(data);
546 data += 8;
547 break;
549 case4(060):
550 opx->offset = gets16(data);
551 data += 2;
552 opx->segment |= SEG_RELATIVE;
553 opx->segment &= ~SEG_32BIT;
554 break;
556 case4(064):
557 opx->segment |= SEG_RELATIVE;
558 if (osize == 16) {
559 opx->offset = gets16(data);
560 data += 2;
561 opx->segment &= ~(SEG_32BIT|SEG_64BIT);
562 } else if (osize == 32) {
563 opx->offset = gets32(data);
564 data += 4;
565 opx->segment &= ~SEG_64BIT;
566 opx->segment |= SEG_32BIT;
568 if (segsize != osize) {
569 opx->type =
570 (opx->type & ~SIZE_MASK)
571 | ((osize == 16) ? BITS16 : BITS32);
573 break;
575 case4(070):
576 opx->offset = gets32(data);
577 data += 4;
578 opx->segment |= SEG_32BIT | SEG_RELATIVE;
579 break;
581 case4(0100):
582 case4(0110):
583 case4(0120):
584 case4(0130):
586 int modrm = *data++;
587 opx->segment |= SEG_RMREG;
588 data = do_ea(data, modrm, asize, segsize, opy, ins);
589 if (!data)
590 return false;
591 opx->basereg = ((modrm >> 3) & 7) + (ins->rex & REX_R ? 8 : 0);
592 break;
595 case4(0140):
596 if (s_field_for == op1) {
597 opx->offset = gets8(data);
598 data++;
599 } else {
600 opx->offset = getu16(data);
601 data += 2;
603 break;
605 case4(0144):
606 case4(0154):
607 s_field_for = (*data & 0x02) ? op1 : -1;
608 if ((*data++ & ~0x02) != *r++)
609 return false;
610 break;
612 case4(0150):
613 if (s_field_for == op1) {
614 opx->offset = gets8(data);
615 data++;
616 } else {
617 opx->offset = getu32(data);
618 data += 4;
620 break;
622 case4(0160):
623 ins->rex |= REX_D;
624 ins->drexdst = op1;
625 break;
627 case4(0164):
628 ins->rex |= REX_D|REX_OC;
629 ins->drexdst = op1;
630 break;
632 case 0171:
633 data = do_drex(data, ins);
634 if (!data)
635 return false;
636 break;
638 case 0172:
640 uint8_t ximm = *data++;
641 c = *r++;
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;
646 break;
648 case 0173:
650 uint8_t ximm = *data++;
651 c = *r++;
653 if ((c ^ ximm) & 15)
654 return false;
656 ins->oprs[c >> 4].basereg = (ximm >> 4) & regmask;
657 ins->oprs[c >> 4].segment |= SEG_RMREG;
659 break;
661 case 0174:
663 uint8_t ximm = *data++;
664 c = *r++;
666 ins->oprs[c].basereg = (ximm >> 4) & regmask;
667 ins->oprs[c].segment |= SEG_RMREG;
669 break;
671 case4(0200):
672 case4(0204):
673 case4(0210):
674 case4(0214):
675 case4(0220):
676 case4(0224):
677 case4(0230):
678 case4(0234):
680 int modrm = *data++;
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);
684 if (!data)
685 return false;
686 break;
689 case4(0260):
691 int vexm = *r++;
692 int vexwlp = *r++;
693 ins->rex |= REX_V;
694 if ((prefix->rex & (REX_V|REX_D|REX_P)) != REX_V)
695 return false;
697 if ((vexm & 0x1f) != prefix->vex_m)
698 return false;
700 switch (vexwlp & 030) {
701 case 000:
702 if (prefix->rex & REX_W)
703 return false;
704 break;
705 case 010:
706 if (!(prefix->rex & REX_W))
707 return false;
708 ins->rex &= ~REX_W;
709 break;
710 case 020: /* VEX.W is a don't care */
711 ins->rex &= ~REX_W;
712 break;
713 case 030:
714 break;
717 if ((vexwlp & 007) != prefix->vex_lp)
718 return false;
720 opx->segment |= SEG_RMREG;
721 opx->basereg = prefix->vex_v;
722 vex_ok = true;
723 break;
726 case 0270:
728 int vexm = *r++;
729 int vexwlp = *r++;
730 ins->rex |= REX_V;
731 if ((prefix->rex & (REX_V|REX_D|REX_P)) != REX_V)
732 return false;
734 if ((vexm & 0x1f) != prefix->vex_m)
735 return false;
737 switch (vexwlp & 030) {
738 case 000:
739 if (ins->rex & REX_W)
740 return false;
741 break;
742 case 010:
743 if (!(ins->rex & REX_W))
744 return false;
745 break;
746 default:
747 break; /* Need to do anything special here? */
750 if ((vexwlp & 007) != prefix->vex_lp)
751 return false;
753 if (prefix->vex_v != 0)
754 return false;
756 vex_ok = true;
757 break;
760 case 0310:
761 if (asize != 16)
762 return false;
763 else
764 a_used = true;
765 break;
767 case 0311:
768 if (asize != 32)
769 return false;
770 else
771 a_used = true;
772 break;
774 case 0312:
775 if (asize != segsize)
776 return false;
777 else
778 a_used = true;
779 break;
781 case 0313:
782 if (asize != 64)
783 return false;
784 else
785 a_used = true;
786 break;
788 case 0314:
789 if (prefix->rex & REX_B)
790 return false;
791 break;
793 case 0315:
794 if (prefix->rex & REX_X)
795 return false;
796 break;
798 case 0316:
799 if (prefix->rex & REX_R)
800 return false;
801 break;
803 case 0317:
804 if (prefix->rex & REX_W)
805 return false;
806 break;
808 case 0320:
809 if (osize != 16)
810 return false;
811 else
812 o_used = true;
813 break;
815 case 0321:
816 if (osize != 32)
817 return false;
818 else
819 o_used = true;
820 break;
822 case 0322:
823 if (osize != (segsize == 16) ? 16 : 32)
824 return false;
825 else
826 o_used = true;
827 break;
829 case 0323:
830 ins->rex |= REX_W; /* 64-bit only instruction */
831 osize = 64;
832 o_used = true;
833 break;
835 case 0324:
836 if (!(ins->rex & (REX_P|REX_W)) || osize != 64)
837 return false;
838 o_used = true;
839 break;
841 case 0325:
842 ins->rex |= REX_NH;
843 break;
845 case 0330:
847 int t = *r++, d = *data++;
848 if (d < t || d > t + 15)
849 return false;
850 else
851 ins->condition = d - t;
852 break;
855 case 0331:
856 if (prefix->rep)
857 return false;
858 break;
860 case 0332:
861 if (prefix->rep != 0xF2)
862 return false;
863 drep = 0;
864 break;
866 case 0333:
867 if (prefix->rep != 0xF3)
868 return false;
869 drep = 0;
870 break;
872 case 0334:
873 if (lock) {
874 ins->rex |= REX_R;
875 lock = 0;
877 break;
879 case 0335:
880 if (drep == P_REP)
881 drep = P_REPE;
882 break;
884 case 0336:
885 case 0337:
886 break;
888 case 0340:
889 return false;
891 case 0341:
892 if (prefix->wait != 0x9B)
893 return false;
894 dwait = 0;
895 break;
897 case4(0344):
898 ins->oprs[0].basereg = (*data++ >> 3) & 7;
899 break;
901 case 0360:
902 if (prefix->osp || prefix->rep)
903 return false;
904 break;
906 case 0361:
907 if (!prefix->osp || prefix->rep)
908 return false;
909 o_used = true;
910 break;
912 case 0362:
913 if (prefix->osp || prefix->rep != 0xf2)
914 return false;
915 drep = 0;
916 break;
918 case 0363:
919 if (prefix->osp || prefix->rep != 0xf3)
920 return false;
921 drep = 0;
922 break;
924 case 0364:
925 if (prefix->osp)
926 return false;
927 break;
929 case 0365:
930 if (prefix->asp)
931 return false;
932 break;
934 case 0366:
935 if (!prefix->osp)
936 return false;
937 o_used = true;
938 break;
940 case 0367:
941 if (!prefix->asp)
942 return false;
943 a_used = true;
944 break;
946 default:
947 return false; /* Unknown code */
951 if (!vex_ok && (ins->rex & REX_V))
952 return false;
954 /* REX cannot be combined with DREX or VEX */
955 if ((ins->rex & (REX_D|REX_V)) && (prefix->rex & REX_P))
956 return false;
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)
963 a_used = true;
966 if (lock) {
967 if (ins->prefixes[PPS_LREP])
968 return false;
969 ins->prefixes[PPS_LREP] = P_LOCK;
971 if (drep) {
972 if (ins->prefixes[PPS_LREP])
973 return false;
974 ins->prefixes[PPS_LREP] = drep;
976 ins->prefixes[PPS_WAIT] = dwait;
977 if (!o_used) {
978 if (osize != ((segsize == 16) ? 16 : 32)) {
979 enum prefixes pfx = 0;
981 switch (osize) {
982 case 16:
983 pfx = P_O16;
984 break;
985 case 32:
986 pfx = P_O32;
987 break;
988 case 64:
989 pfx = P_O64;
990 break;
993 if (ins->prefixes[PPS_OSIZE])
994 return false;
995 ins->prefixes[PPS_OSIZE] = pfx;
998 if (!a_used && asize != segsize) {
999 if (ins->prefixes[PPS_ASIZE])
1000 return false;
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;
1020 uint8_t *dp;
1021 int length, best_length = 0;
1022 char *segover;
1023 int i, slen, colon, n;
1024 uint8_t *origdata;
1025 int works;
1026 insn tmp_ins, ins;
1027 uint32_t goodness, best;
1028 int best_pref;
1029 struct prefix_info prefix;
1030 bool end_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;
1040 segover = NULL;
1041 origdata = data;
1043 ix = itable;
1045 end_prefix = false;
1046 while (!end_prefix) {
1047 switch (*data) {
1048 case 0xF2:
1049 case 0xF3:
1050 prefix.rep = *data++;
1051 break;
1053 case 0x9B:
1054 prefix.wait = *data++;
1055 break;
1057 case 0xF0:
1058 prefix.lock = *data++;
1059 break;
1061 case 0x2E:
1062 segover = "cs", prefix.seg = *data++;
1063 break;
1064 case 0x36:
1065 segover = "ss", prefix.seg = *data++;
1066 break;
1067 case 0x3E:
1068 segover = "ds", prefix.seg = *data++;
1069 break;
1070 case 0x26:
1071 segover = "es", prefix.seg = *data++;
1072 break;
1073 case 0x64:
1074 segover = "fs", prefix.seg = *data++;
1075 break;
1076 case 0x65:
1077 segover = "gs", prefix.seg = *data++;
1078 break;
1080 case 0x66:
1081 prefix.osize = (segsize == 16) ? 32 : 16;
1082 prefix.osp = *data++;
1083 break;
1084 case 0x67:
1085 prefix.asize = (segsize == 32) ? 16 : 32;
1086 prefix.asp = *data++;
1087 break;
1089 case 0xC4:
1090 case 0xC5:
1091 if (segsize == 64 || (data[1] & 0xc0) == 0xc0) {
1092 prefix.vex[0] = *data++;
1093 prefix.vex[1] = *data++;
1095 prefix.rex = REX_V;
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;
1105 } else {
1106 prefix.rex |= (~prefix.vex[1] >> (7-2)) & REX_R;
1107 prefix.vex_m = 1;
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];
1114 end_prefix = true;
1115 break;
1117 case 0x8F:
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++;
1124 prefix.rex = REX_V;
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];
1135 end_prefix = true;
1136 break;
1138 case REX_P + 0x0:
1139 case REX_P + 0x1:
1140 case REX_P + 0x2:
1141 case REX_P + 0x3:
1142 case REX_P + 0x4:
1143 case REX_P + 0x5:
1144 case REX_P + 0x6:
1145 case REX_P + 0x7:
1146 case REX_P + 0x8:
1147 case REX_P + 0x9:
1148 case REX_P + 0xA:
1149 case REX_P + 0xB:
1150 case REX_P + 0xC:
1151 case REX_P + 0xD:
1152 case REX_P + 0xE:
1153 case REX_P + 0xF:
1154 if (segsize == 64) {
1155 prefix.rex = *data++;
1156 if (prefix.rex & REX_W)
1157 prefix.osize = 64;
1159 end_prefix = true;
1160 break;
1162 default:
1163 end_prefix = true;
1164 break;
1168 best = -1; /* Worst possible */
1169 best_p = NULL;
1170 best_pref = INT_MAX;
1172 if (!ix)
1173 return 0; /* No instruction table at all... */
1175 dp = data;
1176 ix += *dp++;
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))) {
1184 works = true;
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
1193 register, die. */
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
1197 ref, die. */
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):
1202 die. */
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))
1207 )) {
1208 works = false;
1209 break;
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
1219 * selection.
1221 if (works) {
1222 int i, nprefix;
1223 goodness = ((*p)->flags & IF_PFMASK) ^ prefer;
1224 nprefix = 0;
1225 for (i = 0; i < MAXPREFIX; i++)
1226 if (tmp_ins.prefixes[i])
1227 nprefix++;
1228 if (nprefix < best_pref ||
1229 (nprefix == best_pref && goodness < best)) {
1230 /* This is the best one found so far */
1231 best = goodness;
1232 best_p = p;
1233 best_pref = nprefix;
1234 best_length = length;
1235 ins = tmp_ins;
1241 if (!best_p)
1242 return 0; /* no instruction was matched */
1244 /* Pick the best match */
1245 p = best_p;
1246 length = best_length;
1248 slen = 0;
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]);
1259 if (prefix)
1260 slen += snprintf(output+slen, outbufsize-slen, "%s ", prefix);
1263 i = (*p)->opcode;
1264 if (i >= FIRST_COND_OPCODE)
1265 slen += snprintf(output + slen, outbufsize - slen, "%s%s",
1266 nasm_insn_names[i], condition_name[ins.condition]);
1267 else
1268 slen += snprintf(output + slen, outbufsize - slen, "%s",
1269 nasm_insn_names[i]);
1271 colon = false;
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];
1276 int64_t offs;
1278 if (t & SAME_AS) {
1279 o = &ins.oprs[t & ~SAME_AS];
1280 t = (*p)->opd[t & ~SAME_AS];
1283 output[slen++] = (colon ? ':' : i == 0 ? ' ' : ',');
1285 offs = o->offset;
1286 if (o->segment & SEG_RELATIVE) {
1287 offs += offset + length;
1289 * sort out wraparound
1291 if (!(o->segment & (SEG_32BIT|SEG_64BIT)))
1292 offs &= 0xffff;
1293 else if (segsize != 64)
1294 offs &= 0xffffffff;
1297 * add sync marker, if autosync is on
1299 if (autosync)
1300 add_sync(offs, 0L);
1303 if (t & COLON)
1304 colon = true;
1305 else
1306 colon = false;
1308 if ((t & (REGISTER | FPUREG)) ||
1309 (o->segment & SEG_RMREG)) {
1310 enum reg_enum reg;
1311 reg = whichreg(t, o->basereg, ins.rex);
1312 if (t & TO)
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) {
1319 if (t & BITS8) {
1320 slen +=
1321 snprintf(output + slen, outbufsize - slen, "byte ");
1322 if (o->segment & SEG_SIGNED) {
1323 if (offs < 0) {
1324 offs *= -1;
1325 output[slen++] = '-';
1326 } else
1327 output[slen++] = '+';
1329 } else if (t & BITS16) {
1330 slen +=
1331 snprintf(output + slen, outbufsize - slen, "word ");
1332 } else if (t & BITS32) {
1333 slen +=
1334 snprintf(output + slen, outbufsize - slen, "dword ");
1335 } else if (t & BITS64) {
1336 slen +=
1337 snprintf(output + slen, outbufsize - slen, "qword ");
1338 } else if (t & NEAR) {
1339 slen +=
1340 snprintf(output + slen, outbufsize - slen, "near ");
1341 } else if (t & SHORT) {
1342 slen +=
1343 snprintf(output + slen, outbufsize - slen, "short ");
1345 slen +=
1346 snprintf(output + slen, outbufsize - slen, "0x%"PRIx64"",
1347 offs);
1348 } else if (!(MEM_OFFS & ~t)) {
1349 slen +=
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);
1357 segover = NULL;
1358 } else if (!(REGMEM & ~t)) {
1359 int started = false;
1360 if (t & BITS8)
1361 slen +=
1362 snprintf(output + slen, outbufsize - slen, "byte ");
1363 if (t & BITS16)
1364 slen +=
1365 snprintf(output + slen, outbufsize - slen, "word ");
1366 if (t & BITS32)
1367 slen +=
1368 snprintf(output + slen, outbufsize - slen, "dword ");
1369 if (t & BITS64)
1370 slen +=
1371 snprintf(output + slen, outbufsize - slen, "qword ");
1372 if (t & BITS80)
1373 slen +=
1374 snprintf(output + slen, outbufsize - slen, "tword ");
1375 if (t & BITS128)
1376 slen +=
1377 snprintf(output + slen, outbufsize - slen, "oword ");
1378 if (t & BITS256)
1379 slen +=
1380 snprintf(output + slen, outbufsize - slen, "yword ");
1381 if (t & FAR)
1382 slen += snprintf(output + slen, outbufsize - slen, "far ");
1383 if (t & NEAR)
1384 slen +=
1385 snprintf(output + slen, outbufsize - slen, "near ");
1386 output[slen++] = '[';
1387 if (o->disp_size)
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 " :
1392 ""));
1393 if (o->eaflags & EAF_REL)
1394 slen += snprintf(output + slen, outbufsize - slen, "rel ");
1395 if (segover) {
1396 slen +=
1397 snprintf(output + slen, outbufsize - slen, "%s:",
1398 segover);
1399 segover = NULL;
1401 if (o->basereg != -1) {
1402 slen += snprintf(output + slen, outbufsize - slen, "%s",
1403 nasm_reg_names[(o->basereg-EXPR_REG_START)]);
1404 started = true;
1406 if (o->indexreg != -1) {
1407 if (started)
1408 output[slen++] = '+';
1409 slen += snprintf(output + slen, outbufsize - slen, "%s",
1410 nasm_reg_names[(o->indexreg-EXPR_REG_START)]);
1411 if (o->scale > 1)
1412 slen +=
1413 snprintf(output + slen, outbufsize - slen, "*%d",
1414 o->scale);
1415 started = true;
1419 if (o->segment & SEG_DISP8) {
1420 const char *prefix;
1421 uint8_t offset = offs;
1422 if ((int8_t)offset < 0) {
1423 prefix = "-";
1424 offset = -offset;
1425 } else {
1426 prefix = "+";
1428 slen +=
1429 snprintf(output + slen, outbufsize - slen, "%s0x%"PRIx8"",
1430 prefix, offset);
1431 } else if (o->segment & SEG_DISP16) {
1432 const char *prefix;
1433 uint16_t offset = offs;
1434 if ((int16_t)offset < 0 && started) {
1435 offset = -offset;
1436 prefix = "-";
1437 } else {
1438 prefix = started ? "+" : "";
1440 slen +=
1441 snprintf(output + slen, outbufsize - slen,
1442 "%s0x%"PRIx16"", prefix, offset);
1443 } else if (o->segment & SEG_DISP32) {
1444 if (prefix.asize == 64) {
1445 const char *prefix;
1446 uint64_t offset = (int64_t)(int32_t)offs;
1447 if ((int32_t)offs < 0 && started) {
1448 offset = -offset;
1449 prefix = "-";
1450 } else {
1451 prefix = started ? "+" : "";
1453 slen +=
1454 snprintf(output + slen, outbufsize - slen,
1455 "%s0x%"PRIx64"", prefix, offset);
1456 } else {
1457 const char *prefix;
1458 uint32_t offset = offs;
1459 if ((int32_t) offset < 0 && started) {
1460 offset = -offset;
1461 prefix = "-";
1462 } else {
1463 prefix = started ? "+" : "";
1465 slen +=
1466 snprintf(output + slen, outbufsize - slen,
1467 "%s0x%"PRIx32"", prefix, offset);
1470 output[slen++] = ']';
1471 } else {
1472 slen +=
1473 snprintf(output + slen, outbufsize - slen, "<operand%d>",
1477 output[slen] = '\0';
1478 if (segover) { /* unused segment override */
1479 char *p = output;
1480 int count = slen + 1;
1481 while (count--)
1482 p[count + 3] = p[count];
1483 strncpy(output, segover, 2);
1484 output[2] = ' ';
1486 return length;
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;
1499 switch (byte) {
1500 case 0xF2:
1501 str = "repne";
1502 break;
1503 case 0xF3:
1504 str = "rep";
1505 break;
1506 case 0x9B:
1507 str = "wait";
1508 break;
1509 case 0xF0:
1510 str = "lock";
1511 break;
1512 case 0x2E:
1513 str = "cs";
1514 break;
1515 case 0x36:
1516 str = "ss";
1517 break;
1518 case 0x3E:
1519 str = "ss";
1520 break;
1521 case 0x26:
1522 str = "es";
1523 break;
1524 case 0x64:
1525 str = "fs";
1526 break;
1527 case 0x65:
1528 str = "gs";
1529 break;
1530 case 0x66:
1531 str = (segsize == 16) ? "o32" : "o16";
1532 break;
1533 case 0x67:
1534 str = (segsize == 32) ? "a16" : "a32";
1535 break;
1536 case REX_P + 0x0:
1537 case REX_P + 0x1:
1538 case REX_P + 0x2:
1539 case REX_P + 0x3:
1540 case REX_P + 0x4:
1541 case REX_P + 0x5:
1542 case REX_P + 0x6:
1543 case REX_P + 0x7:
1544 case REX_P + 0x8:
1545 case REX_P + 0x9:
1546 case REX_P + 0xA:
1547 case REX_P + 0xB:
1548 case REX_P + 0xC:
1549 case REX_P + 0xD:
1550 case REX_P + 0xE:
1551 case REX_P + 0xF:
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" : "");
1559 break;
1561 /* else fall through */
1562 default:
1563 snprintf(output, outbufsize, "db 0x%02x", byte);
1564 break;
1567 if (str)
1568 strcpy(output, str);
1570 return 1;