NASM 2.09rc7
[nasm/avx512.git] / disasm.c
blob588c8328cd2a349c5c993097e35ec2eef84dc206
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 * 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 a DREX suffix
202 static uint8_t *do_drex(uint8_t *data, insn *ins)
204 uint8_t drex = *data++;
205 operand *dst = &ins->oprs[ins->drexdst];
207 if ((drex & 8) != ((ins->rex & REX_OC) ? 8 : 0))
208 return NULL; /* OC0 mismatch */
209 ins->rex = (ins->rex & ~7) | (drex & 7);
211 dst->segment = SEG_RMREG;
212 dst->basereg = drex >> 4;
213 return data;
218 * Process an effective address (ModRM) specification.
220 static uint8_t *do_ea(uint8_t *data, int modrm, int asize,
221 int segsize, operand * op, insn *ins)
223 int mod, rm, scale, index, base;
224 int rex;
225 uint8_t sib = 0;
227 mod = (modrm >> 6) & 03;
228 rm = modrm & 07;
230 if (mod != 3 && rm == 4 && asize != 16)
231 sib = *data++;
233 if (ins->rex & REX_D) {
234 data = do_drex(data, ins);
235 if (!data)
236 return NULL;
238 rex = ins->rex;
240 if (mod == 3) { /* pure register version */
241 op->basereg = rm+(rex & REX_B ? 8 : 0);
242 op->segment |= SEG_RMREG;
243 return data;
246 op->disp_size = 0;
247 op->eaflags = 0;
249 if (asize == 16) {
251 * <mod> specifies the displacement size (none, byte or
252 * word), and <rm> specifies the register combination.
253 * Exception: mod=0,rm=6 does not specify [BP] as one might
254 * expect, but instead specifies [disp16].
256 op->indexreg = op->basereg = -1;
257 op->scale = 1; /* always, in 16 bits */
258 switch (rm) {
259 case 0:
260 op->basereg = R_BX;
261 op->indexreg = R_SI;
262 break;
263 case 1:
264 op->basereg = R_BX;
265 op->indexreg = R_DI;
266 break;
267 case 2:
268 op->basereg = R_BP;
269 op->indexreg = R_SI;
270 break;
271 case 3:
272 op->basereg = R_BP;
273 op->indexreg = R_DI;
274 break;
275 case 4:
276 op->basereg = R_SI;
277 break;
278 case 5:
279 op->basereg = R_DI;
280 break;
281 case 6:
282 op->basereg = R_BP;
283 break;
284 case 7:
285 op->basereg = R_BX;
286 break;
288 if (rm == 6 && mod == 0) { /* special case */
289 op->basereg = -1;
290 if (segsize != 16)
291 op->disp_size = 16;
292 mod = 2; /* fake disp16 */
294 switch (mod) {
295 case 0:
296 op->segment |= SEG_NODISP;
297 break;
298 case 1:
299 op->segment |= SEG_DISP8;
300 op->offset = (int8_t)*data++;
301 break;
302 case 2:
303 op->segment |= SEG_DISP16;
304 op->offset = *data++;
305 op->offset |= ((unsigned)*data++) << 8;
306 break;
308 return data;
309 } else {
311 * Once again, <mod> specifies displacement size (this time
312 * none, byte or *dword*), while <rm> specifies the base
313 * register. Again, [EBP] is missing, replaced by a pure
314 * disp32 (this time that's mod=0,rm=*5*) in 32-bit mode,
315 * and RIP-relative addressing in 64-bit mode.
317 * However, rm=4
318 * indicates not a single base register, but instead the
319 * presence of a SIB byte...
321 int a64 = asize == 64;
323 op->indexreg = -1;
325 if (a64)
326 op->basereg = nasm_rd_reg64[rm | ((rex & REX_B) ? 8 : 0)];
327 else
328 op->basereg = nasm_rd_reg32[rm | ((rex & REX_B) ? 8 : 0)];
330 if (rm == 5 && mod == 0) {
331 if (segsize == 64) {
332 op->eaflags |= EAF_REL;
333 op->segment |= SEG_RELATIVE;
334 mod = 2; /* fake disp32 */
337 if (asize != 64)
338 op->disp_size = asize;
340 op->basereg = -1;
341 mod = 2; /* fake disp32 */
344 if (rm == 4) { /* process SIB */
345 scale = (sib >> 6) & 03;
346 index = (sib >> 3) & 07;
347 base = sib & 07;
349 op->scale = 1 << scale;
351 if (index == 4 && !(rex & REX_X))
352 op->indexreg = -1; /* ESP/RSP cannot be an index */
353 else if (a64)
354 op->indexreg = nasm_rd_reg64[index | ((rex & REX_X) ? 8 : 0)];
355 else
356 op->indexreg = nasm_rd_reg32[index | ((rex & REX_X) ? 8 : 0)];
358 if (base == 5 && mod == 0) {
359 op->basereg = -1;
360 mod = 2; /* Fake disp32 */
361 } else if (a64)
362 op->basereg = nasm_rd_reg64[base | ((rex & REX_B) ? 8 : 0)];
363 else
364 op->basereg = nasm_rd_reg32[base | ((rex & REX_B) ? 8 : 0)];
366 if (segsize == 16)
367 op->disp_size = 32;
370 switch (mod) {
371 case 0:
372 op->segment |= SEG_NODISP;
373 break;
374 case 1:
375 op->segment |= SEG_DISP8;
376 op->offset = gets8(data);
377 data++;
378 break;
379 case 2:
380 op->segment |= SEG_DISP32;
381 op->offset = gets32(data);
382 data += 4;
383 break;
385 return data;
390 * Determine whether the instruction template in t corresponds to the data
391 * stream in data. Return the number of bytes matched if so.
393 #define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
395 static int matches(const struct itemplate *t, uint8_t *data,
396 const struct prefix_info *prefix, int segsize, insn *ins)
398 uint8_t *r = (uint8_t *)(t->code);
399 uint8_t *origdata = data;
400 bool a_used = false, o_used = false;
401 enum prefixes drep = 0;
402 enum prefixes dwait = 0;
403 uint8_t lock = prefix->lock;
404 int osize = prefix->osize;
405 int asize = prefix->asize;
406 int i, c;
407 int op1, op2;
408 struct operand *opx, *opy;
409 uint8_t opex = 0;
410 int s_field_for = -1; /* No 144/154 series code encountered */
411 bool vex_ok = false;
412 int regmask = (segsize == 64) ? 15 : 7;
414 for (i = 0; i < MAX_OPERANDS; i++) {
415 ins->oprs[i].segment = ins->oprs[i].disp_size =
416 (segsize == 64 ? SEG_64BIT : segsize == 32 ? SEG_32BIT : 0);
418 ins->condition = -1;
419 ins->rex = prefix->rex;
420 memset(ins->prefixes, 0, sizeof ins->prefixes);
422 if (t->flags & (segsize == 64 ? IF_NOLONG : IF_LONG))
423 return false;
425 if (prefix->rep == 0xF2)
426 drep = P_REPNE;
427 else if (prefix->rep == 0xF3)
428 drep = P_REP;
430 dwait = prefix->wait ? P_WAIT : 0;
432 while ((c = *r++) != 0) {
433 op1 = (c & 3) + ((opex & 1) << 2);
434 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
435 opx = &ins->oprs[op1];
436 opy = &ins->oprs[op2];
437 opex = 0;
439 switch (c) {
440 case 01:
441 case 02:
442 case 03:
443 case 04:
444 while (c--)
445 if (*r++ != *data++)
446 return false;
447 break;
449 case 05:
450 case 06:
451 case 07:
452 opex = c;
453 break;
455 case4(010):
457 int t = *r++, d = *data++;
458 if (d < t || d > t + 7)
459 return false;
460 else {
461 opx->basereg = (d-t)+
462 (ins->rex & REX_B ? 8 : 0);
463 opx->segment |= SEG_RMREG;
465 break;
468 case4(014):
469 case4(0274):
470 opx->offset = (int8_t)*data++;
471 opx->segment |= SEG_SIGNED;
472 break;
474 case4(020):
475 opx->offset = *data++;
476 break;
478 case4(024):
479 opx->offset = *data++;
480 break;
482 case4(030):
483 opx->offset = getu16(data);
484 data += 2;
485 break;
487 case4(034):
488 if (osize == 32) {
489 opx->offset = getu32(data);
490 data += 4;
491 } else {
492 opx->offset = getu16(data);
493 data += 2;
495 if (segsize != asize)
496 opx->disp_size = asize;
497 break;
499 case4(040):
500 case4(0254):
501 opx->offset = getu32(data);
502 data += 4;
503 break;
505 case4(044):
506 switch (asize) {
507 case 16:
508 opx->offset = getu16(data);
509 data += 2;
510 if (segsize != 16)
511 opx->disp_size = 16;
512 break;
513 case 32:
514 opx->offset = getu32(data);
515 data += 4;
516 if (segsize == 16)
517 opx->disp_size = 32;
518 break;
519 case 64:
520 opx->offset = getu64(data);
521 opx->disp_size = 64;
522 data += 8;
523 break;
525 break;
527 case4(050):
528 opx->offset = gets8(data++);
529 opx->segment |= SEG_RELATIVE;
530 break;
532 case4(054):
533 opx->offset = getu64(data);
534 data += 8;
535 break;
537 case4(060):
538 opx->offset = gets16(data);
539 data += 2;
540 opx->segment |= SEG_RELATIVE;
541 opx->segment &= ~SEG_32BIT;
542 break;
544 case4(064):
545 opx->segment |= SEG_RELATIVE;
546 if (osize == 16) {
547 opx->offset = gets16(data);
548 data += 2;
549 opx->segment &= ~(SEG_32BIT|SEG_64BIT);
550 } else if (osize == 32) {
551 opx->offset = gets32(data);
552 data += 4;
553 opx->segment &= ~SEG_64BIT;
554 opx->segment |= SEG_32BIT;
556 if (segsize != osize) {
557 opx->type =
558 (opx->type & ~SIZE_MASK)
559 | ((osize == 16) ? BITS16 : BITS32);
561 break;
563 case4(070):
564 opx->offset = gets32(data);
565 data += 4;
566 opx->segment |= SEG_32BIT | SEG_RELATIVE;
567 break;
569 case4(0100):
570 case4(0110):
571 case4(0120):
572 case4(0130):
574 int modrm = *data++;
575 opx->segment |= SEG_RMREG;
576 data = do_ea(data, modrm, asize, segsize, opy, ins);
577 if (!data)
578 return false;
579 opx->basereg = ((modrm >> 3) & 7) + (ins->rex & REX_R ? 8 : 0);
580 break;
583 case4(0140):
584 if (s_field_for == op1) {
585 opx->offset = gets8(data);
586 data++;
587 } else {
588 opx->offset = getu16(data);
589 data += 2;
591 break;
593 case4(0144):
594 case4(0154):
595 s_field_for = (*data & 0x02) ? op1 : -1;
596 if ((*data++ & ~0x02) != *r++)
597 return false;
598 break;
600 case4(0150):
601 if (s_field_for == op1) {
602 opx->offset = gets8(data);
603 data++;
604 } else {
605 opx->offset = getu32(data);
606 data += 4;
608 break;
610 case4(0160):
611 ins->rex |= REX_D;
612 ins->drexdst = op1;
613 break;
615 case4(0164):
616 ins->rex |= REX_D|REX_OC;
617 ins->drexdst = op1;
618 break;
620 case 0171:
621 data = do_drex(data, ins);
622 if (!data)
623 return false;
624 break;
626 case 0172:
628 uint8_t ximm = *data++;
629 c = *r++;
630 ins->oprs[c >> 3].basereg = (ximm >> 4) & regmask;
631 ins->oprs[c >> 3].segment |= SEG_RMREG;
632 ins->oprs[c & 7].offset = ximm & 15;
634 break;
636 case 0173:
638 uint8_t ximm = *data++;
639 c = *r++;
641 if ((c ^ ximm) & 15)
642 return false;
644 ins->oprs[c >> 4].basereg = (ximm >> 4) & regmask;
645 ins->oprs[c >> 4].segment |= SEG_RMREG;
647 break;
649 case 0174:
651 uint8_t ximm = *data++;
652 c = *r++;
654 ins->oprs[c].basereg = (ximm >> 4) & regmask;
655 ins->oprs[c].segment |= SEG_RMREG;
657 break;
659 case4(0200):
660 case4(0204):
661 case4(0210):
662 case4(0214):
663 case4(0220):
664 case4(0224):
665 case4(0230):
666 case4(0234):
668 int modrm = *data++;
669 if (((modrm >> 3) & 07) != (c & 07))
670 return false; /* spare field doesn't match up */
671 data = do_ea(data, modrm, asize, segsize, opy, ins);
672 if (!data)
673 return false;
674 break;
677 case4(0260):
679 int vexm = *r++;
680 int vexwlp = *r++;
682 ins->rex |= REX_V;
683 if ((prefix->rex & (REX_V|REX_D|REX_P)) != REX_V)
684 return false;
686 if ((vexm & 0x1f) != prefix->vex_m)
687 return false;
689 switch (vexwlp & 060) {
690 case 000:
691 if (prefix->rex & REX_W)
692 return false;
693 break;
694 case 020:
695 if (!(prefix->rex & REX_W))
696 return false;
697 ins->rex &= ~REX_W;
698 break;
699 case 040: /* VEX.W is a don't care */
700 ins->rex &= ~REX_W;
701 break;
702 case 060:
703 break;
706 /* The 010 bit of vexwlp is set if VEX.L is ignored */
707 if ((vexwlp ^ prefix->vex_lp) & ((vexwlp & 010) ? 03 : 07))
708 return false;
710 opx->segment |= SEG_RMREG;
711 opx->basereg = prefix->vex_v;
712 vex_ok = true;
713 break;
716 case 0270:
718 int vexm = *r++;
719 int vexwlp = *r++;
720 ins->rex |= REX_V;
721 if ((prefix->rex & (REX_V|REX_D|REX_P)) != REX_V)
722 return false;
724 if ((vexm & 0x1f) != prefix->vex_m)
725 return false;
727 switch (vexwlp & 030) {
728 case 000:
729 if (ins->rex & REX_W)
730 return false;
731 break;
732 case 010:
733 if (!(ins->rex & REX_W))
734 return false;
735 break;
736 default:
737 break; /* Need to do anything special here? */
740 if ((vexwlp & 007) != prefix->vex_lp)
741 return false;
743 if (prefix->vex_v != 0)
744 return false;
746 vex_ok = true;
747 break;
750 case 0310:
751 if (asize != 16)
752 return false;
753 else
754 a_used = true;
755 break;
757 case 0311:
758 if (asize != 32)
759 return false;
760 else
761 a_used = true;
762 break;
764 case 0312:
765 if (asize != segsize)
766 return false;
767 else
768 a_used = true;
769 break;
771 case 0313:
772 if (asize != 64)
773 return false;
774 else
775 a_used = true;
776 break;
778 case 0314:
779 if (prefix->rex & REX_B)
780 return false;
781 break;
783 case 0315:
784 if (prefix->rex & REX_X)
785 return false;
786 break;
788 case 0316:
789 if (prefix->rex & REX_R)
790 return false;
791 break;
793 case 0317:
794 if (prefix->rex & REX_W)
795 return false;
796 break;
798 case 0320:
799 if (osize != 16)
800 return false;
801 else
802 o_used = true;
803 break;
805 case 0321:
806 if (osize != 32)
807 return false;
808 else
809 o_used = true;
810 break;
812 case 0322:
813 if (osize != (segsize == 16) ? 16 : 32)
814 return false;
815 else
816 o_used = true;
817 break;
819 case 0323:
820 ins->rex |= REX_W; /* 64-bit only instruction */
821 osize = 64;
822 o_used = true;
823 break;
825 case 0324:
826 if (!(ins->rex & (REX_P|REX_W)) || osize != 64)
827 return false;
828 o_used = true;
829 break;
831 case 0325:
832 ins->rex |= REX_NH;
833 break;
835 case 0330:
837 int t = *r++, d = *data++;
838 if (d < t || d > t + 15)
839 return false;
840 else
841 ins->condition = d - t;
842 break;
845 case 0331:
846 if (prefix->rep)
847 return false;
848 break;
850 case 0332:
851 if (prefix->rep != 0xF2)
852 return false;
853 drep = 0;
854 break;
856 case 0333:
857 if (prefix->rep != 0xF3)
858 return false;
859 drep = 0;
860 break;
862 case 0334:
863 if (lock) {
864 ins->rex |= REX_R;
865 lock = 0;
867 break;
869 case 0335:
870 if (drep == P_REP)
871 drep = P_REPE;
872 break;
874 case 0336:
875 case 0337:
876 break;
878 case 0340:
879 return false;
881 case 0341:
882 if (prefix->wait != 0x9B)
883 return false;
884 dwait = 0;
885 break;
887 case4(0344):
888 ins->oprs[0].basereg = (*data++ >> 3) & 7;
889 break;
891 case 0360:
892 if (prefix->osp || prefix->rep)
893 return false;
894 break;
896 case 0361:
897 if (!prefix->osp || prefix->rep)
898 return false;
899 o_used = true;
900 break;
902 case 0362:
903 if (prefix->osp || prefix->rep != 0xf2)
904 return false;
905 drep = 0;
906 break;
908 case 0363:
909 if (prefix->osp || prefix->rep != 0xf3)
910 return false;
911 drep = 0;
912 break;
914 case 0364:
915 if (prefix->osp)
916 return false;
917 break;
919 case 0365:
920 if (prefix->asp)
921 return false;
922 break;
924 case 0366:
925 if (!prefix->osp)
926 return false;
927 o_used = true;
928 break;
930 case 0367:
931 if (!prefix->asp)
932 return false;
933 a_used = true;
934 break;
936 default:
937 return false; /* Unknown code */
941 if (!vex_ok && (ins->rex & REX_V))
942 return false;
944 /* REX cannot be combined with DREX or VEX */
945 if ((ins->rex & (REX_D|REX_V)) && (prefix->rex & REX_P))
946 return false;
949 * Check for unused rep or a/o prefixes.
951 for (i = 0; i < t->operands; i++) {
952 if (ins->oprs[i].segment != SEG_RMREG)
953 a_used = true;
956 if (lock) {
957 if (ins->prefixes[PPS_LREP])
958 return false;
959 ins->prefixes[PPS_LREP] = P_LOCK;
961 if (drep) {
962 if (ins->prefixes[PPS_LREP])
963 return false;
964 ins->prefixes[PPS_LREP] = drep;
966 ins->prefixes[PPS_WAIT] = dwait;
967 if (!o_used) {
968 if (osize != ((segsize == 16) ? 16 : 32)) {
969 enum prefixes pfx = 0;
971 switch (osize) {
972 case 16:
973 pfx = P_O16;
974 break;
975 case 32:
976 pfx = P_O32;
977 break;
978 case 64:
979 pfx = P_O64;
980 break;
983 if (ins->prefixes[PPS_OSIZE])
984 return false;
985 ins->prefixes[PPS_OSIZE] = pfx;
988 if (!a_used && asize != segsize) {
989 if (ins->prefixes[PPS_ASIZE])
990 return false;
991 ins->prefixes[PPS_ASIZE] = asize == 16 ? P_A16 : P_A32;
994 /* Fix: check for redundant REX prefixes */
996 return data - origdata;
999 /* Condition names for disassembly, sorted by x86 code */
1000 static const char * const condition_name[16] = {
1001 "o", "no", "c", "nc", "z", "nz", "na", "a",
1002 "s", "ns", "pe", "po", "l", "nl", "ng", "g"
1005 int32_t disasm(uint8_t *data, char *output, int outbufsize, int segsize,
1006 int32_t offset, int autosync, uint32_t prefer)
1008 const struct itemplate * const *p, * const *best_p;
1009 const struct disasm_index *ix;
1010 uint8_t *dp;
1011 int length, best_length = 0;
1012 char *segover;
1013 int i, slen, colon, n;
1014 uint8_t *origdata;
1015 int works;
1016 insn tmp_ins, ins;
1017 uint32_t goodness, best;
1018 int best_pref;
1019 struct prefix_info prefix;
1020 bool end_prefix;
1022 memset(&ins, 0, sizeof ins);
1025 * Scan for prefixes.
1027 memset(&prefix, 0, sizeof prefix);
1028 prefix.asize = segsize;
1029 prefix.osize = (segsize == 64) ? 32 : segsize;
1030 segover = NULL;
1031 origdata = data;
1033 ix = itable;
1035 end_prefix = false;
1036 while (!end_prefix) {
1037 switch (*data) {
1038 case 0xF2:
1039 case 0xF3:
1040 prefix.rep = *data++;
1041 break;
1043 case 0x9B:
1044 prefix.wait = *data++;
1045 break;
1047 case 0xF0:
1048 prefix.lock = *data++;
1049 break;
1051 case 0x2E:
1052 segover = "cs", prefix.seg = *data++;
1053 break;
1054 case 0x36:
1055 segover = "ss", prefix.seg = *data++;
1056 break;
1057 case 0x3E:
1058 segover = "ds", prefix.seg = *data++;
1059 break;
1060 case 0x26:
1061 segover = "es", prefix.seg = *data++;
1062 break;
1063 case 0x64:
1064 segover = "fs", prefix.seg = *data++;
1065 break;
1066 case 0x65:
1067 segover = "gs", prefix.seg = *data++;
1068 break;
1070 case 0x66:
1071 prefix.osize = (segsize == 16) ? 32 : 16;
1072 prefix.osp = *data++;
1073 break;
1074 case 0x67:
1075 prefix.asize = (segsize == 32) ? 16 : 32;
1076 prefix.asp = *data++;
1077 break;
1079 case 0xC4:
1080 case 0xC5:
1081 if (segsize == 64 || (data[1] & 0xc0) == 0xc0) {
1082 prefix.vex[0] = *data++;
1083 prefix.vex[1] = *data++;
1085 prefix.rex = REX_V;
1086 prefix.vex_c = RV_VEX;
1088 if (prefix.vex[0] == 0xc4) {
1089 prefix.vex[2] = *data++;
1090 prefix.rex |= (~prefix.vex[1] >> 5) & 7; /* REX_RXB */
1091 prefix.rex |= (prefix.vex[2] >> (7-3)) & REX_W;
1092 prefix.vex_m = prefix.vex[1] & 0x1f;
1093 prefix.vex_v = (~prefix.vex[2] >> 3) & 15;
1094 prefix.vex_lp = prefix.vex[2] & 7;
1095 } else {
1096 prefix.rex |= (~prefix.vex[1] >> (7-2)) & REX_R;
1097 prefix.vex_m = 1;
1098 prefix.vex_v = (~prefix.vex[1] >> 3) & 15;
1099 prefix.vex_lp = prefix.vex[1] & 7;
1102 ix = itable_vex[RV_VEX][prefix.vex_m][prefix.vex_lp];
1104 end_prefix = true;
1105 break;
1107 case 0x8F:
1108 if ((data[1] & 030) != 0 &&
1109 (segsize == 64 || (data[1] & 0xc0) == 0xc0)) {
1110 prefix.vex[0] = *data++;
1111 prefix.vex[1] = *data++;
1112 prefix.vex[2] = *data++;
1114 prefix.rex = REX_V;
1115 prefix.vex_c = RV_XOP;
1117 prefix.rex |= (~prefix.vex[1] >> 5) & 7; /* REX_RXB */
1118 prefix.rex |= (prefix.vex[2] >> (7-3)) & REX_W;
1119 prefix.vex_m = prefix.vex[1] & 0x1f;
1120 prefix.vex_v = (~prefix.vex[2] >> 3) & 15;
1121 prefix.vex_lp = prefix.vex[2] & 7;
1123 ix = itable_vex[RV_XOP][prefix.vex_m][prefix.vex_lp];
1125 end_prefix = true;
1126 break;
1128 case REX_P + 0x0:
1129 case REX_P + 0x1:
1130 case REX_P + 0x2:
1131 case REX_P + 0x3:
1132 case REX_P + 0x4:
1133 case REX_P + 0x5:
1134 case REX_P + 0x6:
1135 case REX_P + 0x7:
1136 case REX_P + 0x8:
1137 case REX_P + 0x9:
1138 case REX_P + 0xA:
1139 case REX_P + 0xB:
1140 case REX_P + 0xC:
1141 case REX_P + 0xD:
1142 case REX_P + 0xE:
1143 case REX_P + 0xF:
1144 if (segsize == 64) {
1145 prefix.rex = *data++;
1146 if (prefix.rex & REX_W)
1147 prefix.osize = 64;
1149 end_prefix = true;
1150 break;
1152 default:
1153 end_prefix = true;
1154 break;
1158 best = -1; /* Worst possible */
1159 best_p = NULL;
1160 best_pref = INT_MAX;
1162 if (!ix)
1163 return 0; /* No instruction table at all... */
1165 dp = data;
1166 ix += *dp++;
1167 while (ix->n == -1) {
1168 ix = (const struct disasm_index *)ix->p + *dp++;
1171 p = (const struct itemplate * const *)ix->p;
1172 for (n = ix->n; n; n--, p++) {
1173 if ((length = matches(*p, data, &prefix, segsize, &tmp_ins))) {
1174 works = true;
1176 * Final check to make sure the types of r/m match up.
1177 * XXX: Need to make sure this is actually correct.
1179 for (i = 0; i < (*p)->operands; i++) {
1180 if (!((*p)->opd[i] & SAME_AS) &&
1182 /* If it's a mem-only EA but we have a
1183 register, die. */
1184 ((tmp_ins.oprs[i].segment & SEG_RMREG) &&
1185 is_class(MEMORY, (*p)->opd[i])) ||
1186 /* If it's a reg-only EA but we have a memory
1187 ref, die. */
1188 (!(tmp_ins.oprs[i].segment & SEG_RMREG) &&
1189 !(REG_EA & ~(*p)->opd[i]) &&
1190 !((*p)->opd[i] & REG_SMASK)) ||
1191 /* Register type mismatch (eg FS vs REG_DESS):
1192 die. */
1193 ((((*p)->opd[i] & (REGISTER | FPUREG)) ||
1194 (tmp_ins.oprs[i].segment & SEG_RMREG)) &&
1195 !whichreg((*p)->opd[i],
1196 tmp_ins.oprs[i].basereg, tmp_ins.rex))
1197 )) {
1198 works = false;
1199 break;
1204 * Note: we always prefer instructions which incorporate
1205 * prefixes in the instructions themselves. This is to allow
1206 * e.g. PAUSE to be preferred to REP NOP, and deal with
1207 * MMX/SSE instructions where prefixes are used to select
1208 * between MMX and SSE register sets or outright opcode
1209 * selection.
1211 if (works) {
1212 int i, nprefix;
1213 goodness = ((*p)->flags & IF_PFMASK) ^ prefer;
1214 nprefix = 0;
1215 for (i = 0; i < MAXPREFIX; i++)
1216 if (tmp_ins.prefixes[i])
1217 nprefix++;
1218 if (nprefix < best_pref ||
1219 (nprefix == best_pref && goodness < best)) {
1220 /* This is the best one found so far */
1221 best = goodness;
1222 best_p = p;
1223 best_pref = nprefix;
1224 best_length = length;
1225 ins = tmp_ins;
1231 if (!best_p)
1232 return 0; /* no instruction was matched */
1234 /* Pick the best match */
1235 p = best_p;
1236 length = best_length;
1238 slen = 0;
1240 /* TODO: snprintf returns the value that the string would have if
1241 * the buffer were long enough, and not the actual length of
1242 * the returned string, so each instance of using the return
1243 * value of snprintf should actually be checked to assure that
1244 * the return value is "sane." Maybe a macro wrapper could
1245 * be used for that purpose.
1247 for (i = 0; i < MAXPREFIX; i++) {
1248 const char *prefix = prefix_name(ins.prefixes[i]);
1249 if (prefix)
1250 slen += snprintf(output+slen, outbufsize-slen, "%s ", prefix);
1253 i = (*p)->opcode;
1254 if (i >= FIRST_COND_OPCODE)
1255 slen += snprintf(output + slen, outbufsize - slen, "%s%s",
1256 nasm_insn_names[i], condition_name[ins.condition]);
1257 else
1258 slen += snprintf(output + slen, outbufsize - slen, "%s",
1259 nasm_insn_names[i]);
1261 colon = false;
1262 length += data - origdata; /* fix up for prefixes */
1263 for (i = 0; i < (*p)->operands; i++) {
1264 opflags_t t = (*p)->opd[i];
1265 const operand *o = &ins.oprs[i];
1266 int64_t offs;
1268 if (t & SAME_AS) {
1269 o = &ins.oprs[t & ~SAME_AS];
1270 t = (*p)->opd[t & ~SAME_AS];
1273 output[slen++] = (colon ? ':' : i == 0 ? ' ' : ',');
1275 offs = o->offset;
1276 if (o->segment & SEG_RELATIVE) {
1277 offs += offset + length;
1279 * sort out wraparound
1281 if (!(o->segment & (SEG_32BIT|SEG_64BIT)))
1282 offs &= 0xffff;
1283 else if (segsize != 64)
1284 offs &= 0xffffffff;
1287 * add sync marker, if autosync is on
1289 if (autosync)
1290 add_sync(offs, 0L);
1293 if (t & COLON)
1294 colon = true;
1295 else
1296 colon = false;
1298 if ((t & (REGISTER | FPUREG)) ||
1299 (o->segment & SEG_RMREG)) {
1300 enum reg_enum reg;
1301 reg = whichreg(t, o->basereg, ins.rex);
1302 if (t & TO)
1303 slen += snprintf(output + slen, outbufsize - slen, "to ");
1304 slen += snprintf(output + slen, outbufsize - slen, "%s",
1305 nasm_reg_names[reg-EXPR_REG_START]);
1306 } else if (!(UNITY & ~t)) {
1307 output[slen++] = '1';
1308 } else if (t & IMMEDIATE) {
1309 if (t & BITS8) {
1310 slen +=
1311 snprintf(output + slen, outbufsize - slen, "byte ");
1312 if (o->segment & SEG_SIGNED) {
1313 if (offs < 0) {
1314 offs *= -1;
1315 output[slen++] = '-';
1316 } else
1317 output[slen++] = '+';
1319 } else if (t & BITS16) {
1320 slen +=
1321 snprintf(output + slen, outbufsize - slen, "word ");
1322 } else if (t & BITS32) {
1323 slen +=
1324 snprintf(output + slen, outbufsize - slen, "dword ");
1325 } else if (t & BITS64) {
1326 slen +=
1327 snprintf(output + slen, outbufsize - slen, "qword ");
1328 } else if (t & NEAR) {
1329 slen +=
1330 snprintf(output + slen, outbufsize - slen, "near ");
1331 } else if (t & SHORT) {
1332 slen +=
1333 snprintf(output + slen, outbufsize - slen, "short ");
1335 slen +=
1336 snprintf(output + slen, outbufsize - slen, "0x%"PRIx64"",
1337 offs);
1338 } else if (!(MEM_OFFS & ~t)) {
1339 slen +=
1340 snprintf(output + slen, outbufsize - slen,
1341 "[%s%s%s0x%"PRIx64"]",
1342 (segover ? segover : ""),
1343 (segover ? ":" : ""),
1344 (o->disp_size == 64 ? "qword " :
1345 o->disp_size == 32 ? "dword " :
1346 o->disp_size == 16 ? "word " : ""), offs);
1347 segover = NULL;
1348 } else if (is_class(REGMEM, t)) {
1349 int started = false;
1350 if (t & BITS8)
1351 slen +=
1352 snprintf(output + slen, outbufsize - slen, "byte ");
1353 if (t & BITS16)
1354 slen +=
1355 snprintf(output + slen, outbufsize - slen, "word ");
1356 if (t & BITS32)
1357 slen +=
1358 snprintf(output + slen, outbufsize - slen, "dword ");
1359 if (t & BITS64)
1360 slen +=
1361 snprintf(output + slen, outbufsize - slen, "qword ");
1362 if (t & BITS80)
1363 slen +=
1364 snprintf(output + slen, outbufsize - slen, "tword ");
1365 if (t & BITS128)
1366 slen +=
1367 snprintf(output + slen, outbufsize - slen, "oword ");
1368 if (t & BITS256)
1369 slen +=
1370 snprintf(output + slen, outbufsize - slen, "yword ");
1371 if (t & FAR)
1372 slen += snprintf(output + slen, outbufsize - slen, "far ");
1373 if (t & NEAR)
1374 slen +=
1375 snprintf(output + slen, outbufsize - slen, "near ");
1376 output[slen++] = '[';
1377 if (o->disp_size)
1378 slen += snprintf(output + slen, outbufsize - slen, "%s",
1379 (o->disp_size == 64 ? "qword " :
1380 o->disp_size == 32 ? "dword " :
1381 o->disp_size == 16 ? "word " :
1382 ""));
1383 if (o->eaflags & EAF_REL)
1384 slen += snprintf(output + slen, outbufsize - slen, "rel ");
1385 if (segover) {
1386 slen +=
1387 snprintf(output + slen, outbufsize - slen, "%s:",
1388 segover);
1389 segover = NULL;
1391 if (o->basereg != -1) {
1392 slen += snprintf(output + slen, outbufsize - slen, "%s",
1393 nasm_reg_names[(o->basereg-EXPR_REG_START)]);
1394 started = true;
1396 if (o->indexreg != -1) {
1397 if (started)
1398 output[slen++] = '+';
1399 slen += snprintf(output + slen, outbufsize - slen, "%s",
1400 nasm_reg_names[(o->indexreg-EXPR_REG_START)]);
1401 if (o->scale > 1)
1402 slen +=
1403 snprintf(output + slen, outbufsize - slen, "*%d",
1404 o->scale);
1405 started = true;
1409 if (o->segment & SEG_DISP8) {
1410 const char *prefix;
1411 uint8_t offset = offs;
1412 if ((int8_t)offset < 0) {
1413 prefix = "-";
1414 offset = -offset;
1415 } else {
1416 prefix = "+";
1418 slen +=
1419 snprintf(output + slen, outbufsize - slen, "%s0x%"PRIx8"",
1420 prefix, offset);
1421 } else if (o->segment & SEG_DISP16) {
1422 const char *prefix;
1423 uint16_t offset = offs;
1424 if ((int16_t)offset < 0 && started) {
1425 offset = -offset;
1426 prefix = "-";
1427 } else {
1428 prefix = started ? "+" : "";
1430 slen +=
1431 snprintf(output + slen, outbufsize - slen,
1432 "%s0x%"PRIx16"", prefix, offset);
1433 } else if (o->segment & SEG_DISP32) {
1434 if (prefix.asize == 64) {
1435 const char *prefix;
1436 uint64_t offset = (int64_t)(int32_t)offs;
1437 if ((int32_t)offs < 0 && started) {
1438 offset = -offset;
1439 prefix = "-";
1440 } else {
1441 prefix = started ? "+" : "";
1443 slen +=
1444 snprintf(output + slen, outbufsize - slen,
1445 "%s0x%"PRIx64"", prefix, offset);
1446 } else {
1447 const char *prefix;
1448 uint32_t offset = offs;
1449 if ((int32_t) offset < 0 && started) {
1450 offset = -offset;
1451 prefix = "-";
1452 } else {
1453 prefix = started ? "+" : "";
1455 slen +=
1456 snprintf(output + slen, outbufsize - slen,
1457 "%s0x%"PRIx32"", prefix, offset);
1460 output[slen++] = ']';
1461 } else {
1462 slen +=
1463 snprintf(output + slen, outbufsize - slen, "<operand%d>",
1467 output[slen] = '\0';
1468 if (segover) { /* unused segment override */
1469 char *p = output;
1470 int count = slen + 1;
1471 while (count--)
1472 p[count + 3] = p[count];
1473 strncpy(output, segover, 2);
1474 output[2] = ' ';
1476 return length;
1480 * This is called when we don't have a complete instruction. If it
1481 * is a standalone *single-byte* prefix show it as such, otherwise
1482 * print it as a literal.
1484 int32_t eatbyte(uint8_t *data, char *output, int outbufsize, int segsize)
1486 uint8_t byte = *data;
1487 const char *str = NULL;
1489 switch (byte) {
1490 case 0xF2:
1491 str = "repne";
1492 break;
1493 case 0xF3:
1494 str = "rep";
1495 break;
1496 case 0x9B:
1497 str = "wait";
1498 break;
1499 case 0xF0:
1500 str = "lock";
1501 break;
1502 case 0x2E:
1503 str = "cs";
1504 break;
1505 case 0x36:
1506 str = "ss";
1507 break;
1508 case 0x3E:
1509 str = "ss";
1510 break;
1511 case 0x26:
1512 str = "es";
1513 break;
1514 case 0x64:
1515 str = "fs";
1516 break;
1517 case 0x65:
1518 str = "gs";
1519 break;
1520 case 0x66:
1521 str = (segsize == 16) ? "o32" : "o16";
1522 break;
1523 case 0x67:
1524 str = (segsize == 32) ? "a16" : "a32";
1525 break;
1526 case REX_P + 0x0:
1527 case REX_P + 0x1:
1528 case REX_P + 0x2:
1529 case REX_P + 0x3:
1530 case REX_P + 0x4:
1531 case REX_P + 0x5:
1532 case REX_P + 0x6:
1533 case REX_P + 0x7:
1534 case REX_P + 0x8:
1535 case REX_P + 0x9:
1536 case REX_P + 0xA:
1537 case REX_P + 0xB:
1538 case REX_P + 0xC:
1539 case REX_P + 0xD:
1540 case REX_P + 0xE:
1541 case REX_P + 0xF:
1542 if (segsize == 64) {
1543 snprintf(output, outbufsize, "rex%s%s%s%s%s",
1544 (byte == REX_P) ? "" : ".",
1545 (byte & REX_W) ? "w" : "",
1546 (byte & REX_R) ? "r" : "",
1547 (byte & REX_X) ? "x" : "",
1548 (byte & REX_B) ? "b" : "");
1549 break;
1551 /* else fall through */
1552 default:
1553 snprintf(output, outbufsize, "db 0x%02x", byte);
1554 break;
1557 if (str)
1558 snprintf(output, outbufsize, "%s", str);
1560 return 1;