1 /* udis86 - libudis86/decode.c
3 * Copyright (c) 2002-2009 Vivek Thampi
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef __UD_STANDALONE__
33 #endif /* __UD_STANDALONE__ */
35 /* The max number of prefixes to an instruction */
36 #define MAX_PREFIXES 15
39 #define REX_W(r) ( ( 0xF & ( r ) ) >> 3 )
40 #define REX_R(r) ( ( 0x7 & ( r ) ) >> 2 )
41 #define REX_X(r) ( ( 0x3 & ( r ) ) >> 1 )
42 #define REX_B(r) ( ( 0x1 & ( r ) ) >> 0 )
43 #define REX_PFX_MASK(n) ( ( P_REXW(n) << 3 ) | \
44 ( P_REXR(n) << 2 ) | \
45 ( P_REXX(n) << 1 ) | \
48 /* scable-index-base bits */
49 #define SIB_S(b) ( ( b ) >> 6 )
50 #define SIB_I(b) ( ( ( b ) >> 3 ) & 7 )
51 #define SIB_B(b) ( ( b ) & 7 )
54 #define MODRM_REG(b) ( ( ( b ) >> 3 ) & 7 )
55 #define MODRM_NNN(b) ( ( ( b ) >> 3 ) & 7 )
56 #define MODRM_MOD(b) ( ( ( b ) >> 6 ) & 3 )
57 #define MODRM_RM(b) ( ( b ) & 7 )
59 static int decode_ext(struct ud
*u
, uint16_t ptr
);
61 enum reg_class
{ /* register classes */
77 * Load little-endian values from input
80 inp_uint8(struct ud
* u
)
82 return ud_inp_next(u
);
86 inp_uint16(struct ud
* u
)
92 return ret
| (r
<< 8);
96 inp_uint32(struct ud
* u
)
100 ret
= ud_inp_next(u
);
102 ret
= ret
| (r
<< 8);
104 ret
= ret
| (r
<< 16);
106 return ret
| (r
<< 24);
110 inp_uint64(struct ud
* u
)
114 ret
= ud_inp_next(u
);
116 ret
= ret
| (r
<< 8);
118 ret
= ret
| (r
<< 16);
120 ret
= ret
| (r
<< 24);
122 ret
= ret
| (r
<< 32);
124 ret
= ret
| (r
<< 40);
126 ret
= ret
| (r
<< 48);
128 return ret
| (r
<< 56);
133 eff_opr_mode(int dis_mode
, int rex_w
, int pfx_opr
)
135 if (dis_mode
== 64) {
136 return rex_w
? 64 : (pfx_opr
? 16 : 32);
137 } else if (dis_mode
== 32) {
138 return pfx_opr
? 16 : 32;
140 UD_ASSERT(dis_mode
== 16);
141 return pfx_opr
? 32 : 16;
147 eff_adr_mode(int dis_mode
, int pfx_adr
)
149 if (dis_mode
== 64) {
150 return pfx_adr
? 32 : 64;
151 } else if (dis_mode
== 32) {
152 return pfx_adr
? 16 : 32;
154 UD_ASSERT(dis_mode
== 16);
155 return pfx_adr
? 32 : 16;
160 /* Looks up mnemonic code in the mnemonic string table
161 * Returns NULL if the mnemonic code is invalid
164 ud_lookup_mnemonic(enum ud_mnemonic_code c
)
166 if (c
< UD_MAX_MNEMONIC_CODE
) {
167 return ud_mnemonics_str
[c
];
177 * Extracts instruction prefixes.
180 decode_prefixes(struct ud
*u
)
184 UD_RETURN_ON_ERROR(u
);
188 UD_RETURN_ON_ERROR(u
);
189 if (inp_len(u
) == MAX_INSN_LENGTH
) {
190 UD_RETURN_WITH_ERROR(u
, "max instruction length");
197 u
->pfx_seg
= UD_R_CS
;
200 u
->pfx_seg
= UD_R_SS
;
203 u
->pfx_seg
= UD_R_DS
;
206 u
->pfx_seg
= UD_R_ES
;
209 u
->pfx_seg
= UD_R_FS
;
212 u
->pfx_seg
= UD_R_GS
;
214 case 0x67 : /* adress-size override prefix */
235 if (u
->dis_mode
== 64 && (curr
& 0xF0) == 0x40) {
236 /* rex prefixes in 64bit mode, must be the last prefix
240 /* rewind back one byte in stream, since the above loop
241 * stops with a non-prefix byte.
249 static inline unsigned int modrm( struct ud
* u
)
251 if ( !u
->have_modrm
) {
252 u
->modrm
= ud_inp_next( u
);
260 resolve_operand_size( const struct ud
* u
, unsigned int s
)
265 return ( u
->opr_mode
);
267 return ( u
->opr_mode
== 16 ) ? 16 : 32;
269 return ( u
->opr_mode
== 16 ) ? 32 : u
->opr_mode
;
271 return ( u
->dis_mode
== 64 ) ? 64 : 32;
278 static int resolve_mnemonic( struct ud
* u
)
280 /* resolve 3dnow weirdness. */
281 if ( u
->mnemonic
== UD_I3dnow
) {
282 u
->mnemonic
= ud_itab
[ u
->le
->table
[ inp_curr( u
) ] ].mnemonic
;
284 /* SWAPGS is only valid in 64bits mode */
285 if ( u
->mnemonic
== UD_Iswapgs
&& u
->dis_mode
!= 64 ) {
286 UDERR(u
, "swapgs invalid in 64bits mode");
290 if (u
->mnemonic
== UD_Ixchg
) {
291 if ((u
->operand
[0].type
== UD_OP_REG
&& u
->operand
[0].base
== UD_R_AX
&&
292 u
->operand
[1].type
== UD_OP_REG
&& u
->operand
[1].base
== UD_R_AX
) ||
293 (u
->operand
[0].type
== UD_OP_REG
&& u
->operand
[0].base
== UD_R_EAX
&&
294 u
->operand
[1].type
== UD_OP_REG
&& u
->operand
[1].base
== UD_R_EAX
)) {
295 u
->operand
[0].type
= UD_NONE
;
296 u
->operand
[1].type
= UD_NONE
;
297 u
->mnemonic
= UD_Inop
;
301 if (u
->mnemonic
== UD_Inop
&& u
->pfx_repe
) {
303 u
->mnemonic
= UD_Ipause
;
309 /* -----------------------------------------------------------------------------
310 * decode_a()- Decodes operands of the type seg:offset
311 * -----------------------------------------------------------------------------
314 decode_a(struct ud
* u
, struct ud_operand
*op
)
316 if (u
->opr_mode
== 16) {
318 op
->type
= UD_OP_PTR
;
320 op
->lval
.ptr
.off
= inp_uint16(u
);
321 op
->lval
.ptr
.seg
= inp_uint16(u
);
324 op
->type
= UD_OP_PTR
;
326 op
->lval
.ptr
.off
= inp_uint32(u
);
327 op
->lval
.ptr
.seg
= inp_uint16(u
);
331 /* -----------------------------------------------------------------------------
332 * decode_gpr() - Returns decoded General Purpose Register
333 * -----------------------------------------------------------------------------
336 decode_gpr(register struct ud
* u
, unsigned int s
, unsigned char rm
)
340 return UD_R_RAX
+ rm
;
342 return UD_R_EAX
+ rm
;
346 if (u
->dis_mode
== 64 && u
->pfx_rex
) {
348 return UD_R_SPL
+ (rm
-4);
350 } else return UD_R_AL
+ rm
;
352 UD_ASSERT(!"invalid operand size");
358 decode_reg(struct ud
*u
,
359 struct ud_operand
*opr
,
365 size
= resolve_operand_size(u
, size
);
367 case REGCLASS_GPR
: reg
= decode_gpr(u
, size
, num
); break;
368 case REGCLASS_MMX
: reg
= UD_R_MM0
+ (num
& 7); break;
369 case REGCLASS_XMM
: reg
= UD_R_XMM0
+ num
; break;
370 case REGCLASS_CR
: reg
= UD_R_CR0
+ num
; break;
371 case REGCLASS_DB
: reg
= UD_R_DR0
+ num
; break;
372 case REGCLASS_SEG
: {
374 * Only 6 segment registers, anything else is an error.
377 UDERR(u
, "invalid segment register value");
380 reg
= UD_R_ES
+ (num
& 7);
385 UD_ASSERT(!"invalid register type");
388 opr
->type
= UD_OP_REG
;
397 * Decode Immediate values.
400 decode_imm(struct ud
* u
, unsigned int size
, struct ud_operand
*op
)
402 op
->size
= resolve_operand_size(u
, size
);
403 op
->type
= UD_OP_IMM
;
406 case 8: op
->lval
.sbyte
= inp_uint8(u
); break;
407 case 16: op
->lval
.uword
= inp_uint16(u
); break;
408 case 32: op
->lval
.udword
= inp_uint32(u
); break;
409 case 64: op
->lval
.uqword
= inp_uint64(u
); break;
418 * Decode mem address displacement.
421 decode_mem_disp(struct ud
* u
, unsigned int size
, struct ud_operand
*op
)
426 op
->lval
.ubyte
= inp_uint8(u
);
430 op
->lval
.uword
= inp_uint16(u
);
434 op
->lval
.udword
= inp_uint32(u
);
438 op
->lval
.uqword
= inp_uint64(u
);
449 * Decodes reg field of mod/rm byte
453 decode_modrm_reg(struct ud
*u
,
454 struct ud_operand
*operand
,
458 uint8_t reg
= (REX_R(u
->pfx_rex
) << 3) | MODRM_REG(modrm(u
));
459 decode_reg(u
, operand
, type
, reg
, size
);
466 * Decodes rm field of mod/rm byte
470 decode_modrm_rm(struct ud
*u
,
471 struct ud_operand
*op
,
472 unsigned char type
, /* register type */
473 unsigned int size
) /* operand size */
477 unsigned char mod
, rm
;
479 /* get mod, r/m and reg fields */
480 mod
= MODRM_MOD(modrm(u
));
481 rm
= (REX_B(u
->pfx_rex
) << 3) | MODRM_RM(modrm(u
));
484 * If mod is 11b, then the modrm.rm specifies a register.
488 decode_reg(u
, op
, type
, rm
, size
);
493 * !11b => Memory Address
495 op
->type
= UD_OP_MEM
;
496 op
->size
= resolve_operand_size(u
, size
);
498 if (u
->adr_mode
== 64) {
499 op
->base
= UD_R_RAX
+ rm
;
502 } else if (mod
== 2) {
504 } else if (mod
== 0 && (rm
& 7) == 5) {
511 * Scale-Index-Base (SIB)
516 op
->scale
= (1 << SIB_S(inp_curr(u
))) & ~1;
517 op
->index
= UD_R_RAX
+ (SIB_I(inp_curr(u
)) | (REX_X(u
->pfx_rex
) << 3));
518 op
->base
= UD_R_RAX
+ (SIB_B(inp_curr(u
)) | (REX_B(u
->pfx_rex
) << 3));
520 /* special conditions for base reference */
521 if (op
->index
== UD_R_RSP
) {
526 if (op
->base
== UD_R_RBP
|| op
->base
== UD_R_R13
) {
537 } else if (u
->adr_mode
== 32) {
538 op
->base
= UD_R_EAX
+ rm
;
541 } else if (mod
== 2) {
543 } else if (mod
== 0 && rm
== 5) {
550 /* Scale-Index-Base (SIB) */
554 op
->scale
= (1 << SIB_S(inp_curr(u
))) & ~1;
555 op
->index
= UD_R_EAX
+ (SIB_I(inp_curr(u
)) | (REX_X(u
->pfx_rex
) << 3));
556 op
->base
= UD_R_EAX
+ (SIB_B(inp_curr(u
)) | (REX_B(u
->pfx_rex
) << 3));
558 if (op
->index
== UD_R_ESP
) {
563 /* special condition for base reference */
564 if (op
->base
== UD_R_EBP
) {
576 const unsigned int bases
[] = { UD_R_BX
, UD_R_BX
, UD_R_BP
, UD_R_BP
,
577 UD_R_SI
, UD_R_DI
, UD_R_BP
, UD_R_BX
};
578 const unsigned int indices
[] = { UD_R_SI
, UD_R_DI
, UD_R_SI
, UD_R_DI
,
579 UD_NONE
, UD_NONE
, UD_NONE
, UD_NONE
};
580 op
->base
= bases
[rm
& 7];
581 op
->index
= indices
[rm
& 7];
582 if (mod
== 0 && rm
== 6) {
585 } else if (mod
== 1) {
587 } else if (mod
== 2) {
593 decode_mem_disp(u
, offset
, op
);
600 * Decode offset-only memory operand
603 decode_moffset(struct ud
*u
, unsigned int size
, struct ud_operand
*opr
)
605 opr
->type
= UD_OP_MEM
;
606 opr
->size
= resolve_operand_size(u
, size
);
607 decode_mem_disp(u
, u
->adr_mode
, opr
);
611 /* -----------------------------------------------------------------------------
612 * decode_operands() - Disassembles Operands.
613 * -----------------------------------------------------------------------------
616 decode_operand(struct ud
*u
,
617 struct ud_operand
*operand
,
618 enum ud_operand_code type
,
621 operand
->_oprcode
= type
;
625 decode_a(u
, operand
);
628 decode_modrm_rm(u
, operand
, REGCLASS_GPR
,
629 MODRM_MOD(modrm(u
)) == 3 ?
630 Mx_reg_size(size
) : Mx_mem_size(size
));
634 /* intended fall through */
636 if (MODRM_MOD(modrm(u
)) == 3) {
637 UDERR(u
, "expected modrm.mod != 3");
639 /* intended fall through */
641 decode_modrm_rm(u
, operand
, REGCLASS_GPR
, size
);
644 decode_modrm_reg(u
, operand
, REGCLASS_GPR
, size
);
648 decode_imm(u
, size
, operand
);
651 operand
->type
= UD_OP_CONST
;
652 operand
->lval
.udword
= 1;
655 if (MODRM_MOD(modrm(u
)) != 3) {
656 UDERR(u
, "expected modrm.mod == 3");
658 /* intended fall through */
660 decode_modrm_rm(u
, operand
, REGCLASS_MMX
, size
);
663 decode_modrm_reg(u
, operand
, REGCLASS_MMX
, size
);
666 if (MODRM_MOD(modrm(u
)) != 3) {
667 UDERR(u
, "expected modrm.mod == 3");
669 /* intended fall through */
671 decode_modrm_rm(u
, operand
, REGCLASS_XMM
, size
);
674 decode_modrm_reg(u
, operand
, REGCLASS_XMM
, size
);
677 decode_modrm_rm(u
, operand
, REGCLASS_XMM
,
678 MODRM_MOD(modrm(u
)) == 3 ?
679 Mx_reg_size(size
) : Mx_mem_size(size
));
682 decode_modrm_reg(u
, operand
, REGCLASS_SEG
, size
);
685 decode_moffset(u
, size
, operand
);
695 decode_reg(u
, operand
, REGCLASS_GPR
,
696 (REX_B(u
->pfx_rex
) << 3) | (type
- OP_R0
), size
);
702 decode_reg(u
, operand
, REGCLASS_GPR
, 0, size
);
707 decode_reg(u
, operand
, REGCLASS_GPR
, 1, size
);
712 decode_reg(u
, operand
, REGCLASS_GPR
, 2, size
);
720 /* in 64bits mode, only fs and gs are allowed */
721 if (u
->dis_mode
== 64) {
722 if (type
!= OP_FS
&& type
!= OP_GS
) {
723 UDERR(u
, "invalid segment register in 64bits");
726 operand
->type
= UD_OP_REG
;
727 operand
->base
= (type
- OP_ES
) + UD_R_ES
;
731 decode_imm(u
, size
, operand
);
732 operand
->type
= UD_OP_JIMM
;
735 if (MODRM_MOD(modrm(u
)) != 3) {
736 UDERR(u
, "expected modrm.mod == 3");
738 decode_modrm_rm(u
, operand
, REGCLASS_GPR
, size
);
741 decode_modrm_reg(u
, operand
, REGCLASS_CR
, size
);
744 decode_modrm_reg(u
, operand
, REGCLASS_DB
, size
);
747 operand
->type
= UD_OP_CONST
;
748 operand
->lval
.sbyte
= 3;
758 operand
->type
= UD_OP_REG
;
759 operand
->base
= (type
- OP_ST0
) + UD_R_ST0
;
772 * Disassemble upto 3 operands of the current instruction being
773 * disassembled. By the end of the function, the operand fields
774 * of the ud structure will have been filled.
777 decode_operands(struct ud
* u
)
779 decode_operand(u
, &u
->operand
[0],
780 u
->itab_entry
->operand1
.type
,
781 u
->itab_entry
->operand1
.size
);
782 decode_operand(u
, &u
->operand
[1],
783 u
->itab_entry
->operand2
.type
,
784 u
->itab_entry
->operand2
.size
);
785 decode_operand(u
, &u
->operand
[2],
786 u
->itab_entry
->operand3
.type
,
787 u
->itab_entry
->operand3
.size
);
791 /* -----------------------------------------------------------------------------
792 * clear_insn() - clear instruction structure
793 * -----------------------------------------------------------------------------
796 clear_insn(register struct ud
* u
)
808 u
->mnemonic
= UD_Inone
;
809 u
->itab_entry
= NULL
;
813 memset( &u
->operand
[ 0 ], 0, sizeof( struct ud_operand
) );
814 memset( &u
->operand
[ 1 ], 0, sizeof( struct ud_operand
) );
815 memset( &u
->operand
[ 2 ], 0, sizeof( struct ud_operand
) );
820 resolve_pfx_str(struct ud
* u
)
822 if (u
->pfx_str
== 0xf3) {
823 if (P_STR(u
->itab_entry
->prefix
)) {
828 } else if (u
->pfx_str
== 0xf2) {
836 resolve_mode( struct ud
* u
)
838 /* if in error state, bail out */
839 if ( u
->error
) return -1;
841 /* propagate prefix effects */
842 if ( u
->dis_mode
== 64 ) { /* set 64bit-mode flags */
844 /* Check validity of instruction m64 */
845 if ( P_INV64( u
->itab_entry
->prefix
) ) {
846 UDERR(u
, "instruction invalid in 64bits");
850 /* effective rex prefix is the effective mask for the
851 * instruction hard-coded in the opcode map.
853 u
->pfx_rex
= ( u
->pfx_rex
& 0x40 ) |
854 ( u
->pfx_rex
& REX_PFX_MASK( u
->itab_entry
->prefix
) );
856 /* whether this instruction has a default operand size of
857 * 64bit, also hardcoded into the opcode map.
859 u
->default64
= P_DEF64( u
->itab_entry
->prefix
);
860 /* calculate effective operand size */
861 if ( REX_W( u
->pfx_rex
) ) {
863 } else if ( u
->pfx_opr
) {
866 /* unless the default opr size of instruction is 64,
867 * the effective operand size in the absence of rex.w
870 u
->opr_mode
= ( u
->default64
) ? 64 : 32;
873 /* calculate effective address size */
874 u
->adr_mode
= (u
->pfx_adr
) ? 32 : 64;
875 } else if ( u
->dis_mode
== 32 ) { /* set 32bit-mode flags */
876 u
->opr_mode
= ( u
->pfx_opr
) ? 16 : 32;
877 u
->adr_mode
= ( u
->pfx_adr
) ? 16 : 32;
878 } else if ( u
->dis_mode
== 16 ) { /* set 16bit-mode flags */
879 u
->opr_mode
= ( u
->pfx_opr
) ? 32 : 16;
880 u
->adr_mode
= ( u
->pfx_adr
) ? 32 : 16;
883 /* set flags for implicit addressing */
884 u
->implicit_addr
= P_IMPADDR( u
->itab_entry
->prefix
);
891 decode_insn(struct ud
*u
, uint16_t ptr
)
893 UD_ASSERT((ptr
& 0x8000) == 0);
894 u
->itab_entry
= &ud_itab
[ ptr
];
895 u
->mnemonic
= u
->itab_entry
->mnemonic
;
896 return (resolve_pfx_str(u
) == 0 &&
897 resolve_mode(u
) == 0 &&
898 decode_operands(u
) == 0 &&
899 resolve_mnemonic(u
) == 0) ? 0 : -1;
906 * Decoding 3dnow is a little tricky because of its strange opcode
907 * structure. The final opcode disambiguation depends on the last
908 * byte that comes after the operands have been decoded. Fortunately,
909 * all 3dnow instructions have the same set of operand types. So we
910 * go ahead and decode the instruction by picking an arbitrarily chosen
911 * valid entry in the table, decode the operands, and read the final
912 * byte to resolve the menmonic.
915 decode_3dnow(struct ud
* u
)
918 UD_ASSERT(u
->le
->type
== UD_TAB__OPC_3DNOW
);
919 UD_ASSERT(u
->le
->table
[0xc] != 0);
920 decode_insn(u
, u
->le
->table
[0xc]);
925 ptr
= u
->le
->table
[inp_curr(u
)];
926 UD_ASSERT((ptr
& 0x8000) == 0);
927 u
->mnemonic
= ud_itab
[ptr
].mnemonic
;
933 decode_ssepfx(struct ud
*u
)
939 * String prefixes (f2, f3) take precedence over operand
946 idx
= ((pfx
& 0xf) + 1) / 2;
947 if (u
->le
->table
[idx
] == 0) {
950 if (idx
&& u
->le
->table
[idx
] != 0) {
952 * "Consume" the prefix as a part of the opcode, so it is no
953 * longer exported as an instruction prefix.
958 * consume "66" only if it was used for decoding, leaving
959 * it to be used as an operands size override for some
965 return decode_ext(u
, u
->le
->table
[idx
]);
972 * Decode opcode extensions (if any)
975 decode_ext(struct ud
*u
, uint16_t ptr
)
978 if ((ptr
& 0x8000) == 0) {
979 return decode_insn(u
, ptr
);
981 u
->le
= &ud_lookup_table_list
[(~0x8000 & ptr
)];
982 if (u
->le
->type
== UD_TAB__OPC_3DNOW
) {
983 return decode_3dnow(u
);
986 switch (u
->le
->type
) {
987 case UD_TAB__OPC_MOD
:
988 /* !11 = 0, 11 = 1 */
989 idx
= (MODRM_MOD(modrm(u
)) + 1) / 4;
991 /* disassembly mode/operand size/address size based tables.
992 * 16 = 0,, 32 = 1, 64 = 2
994 case UD_TAB__OPC_MODE
:
995 idx
= u
->dis_mode
!= 64 ? 0 : 1;
997 case UD_TAB__OPC_OSIZE
:
998 idx
= eff_opr_mode(u
->dis_mode
, REX_W(u
->pfx_rex
), u
->pfx_opr
) / 32;
1000 case UD_TAB__OPC_ASIZE
:
1001 idx
= eff_adr_mode(u
->dis_mode
, u
->pfx_adr
) / 32;
1003 case UD_TAB__OPC_X87
:
1004 idx
= modrm(u
) - 0xC0;
1006 case UD_TAB__OPC_VENDOR
:
1007 if (u
->vendor
== UD_VENDOR_ANY
) {
1008 /* choose a valid entry */
1009 idx
= (u
->le
->table
[idx
] != 0) ? 0 : 1;
1010 } else if (u
->vendor
== UD_VENDOR_AMD
) {
1016 case UD_TAB__OPC_RM
:
1017 idx
= MODRM_RM(modrm(u
));
1019 case UD_TAB__OPC_REG
:
1020 idx
= MODRM_REG(modrm(u
));
1022 case UD_TAB__OPC_SSE
:
1023 return decode_ssepfx(u
);
1025 UD_ASSERT(!"not reached");
1029 return decode_ext(u
, u
->le
->table
[idx
]);
1034 decode_opcode(struct ud
*u
)
1037 UD_ASSERT(u
->le
->type
== UD_TAB__OPC_TABLE
);
1042 u
->primary_opcode
= inp_curr(u
);
1043 ptr
= u
->le
->table
[inp_curr(u
)];
1045 u
->le
= &ud_lookup_table_list
[ptr
& ~0x8000];
1046 if (u
->le
->type
== UD_TAB__OPC_TABLE
) {
1047 return decode_opcode(u
);
1050 return decode_ext(u
, ptr
);
1054 /* =============================================================================
1055 * ud_decode() - Instruction decoder. Returns the number of bytes decoded.
1056 * =============================================================================
1059 ud_decode(struct ud
*u
)
1063 u
->le
= &ud_lookup_table_list
[0];
1064 u
->error
= decode_prefixes(u
) == -1 ||
1065 decode_opcode(u
) == -1 ||
1067 /* Handle decode error. */
1069 /* clear out the decode data. */
1071 /* mark the sequence of bytes as invalid. */
1072 u
->itab_entry
= &ud_itab
[0]; /* entry 0 is invalid */
1073 u
->mnemonic
= u
->itab_entry
->mnemonic
;
1076 /* maybe this stray segment override byte
1077 * should be spewed out?
1079 if ( !P_SEG( u
->itab_entry
->prefix
) &&
1080 u
->operand
[0].type
!= UD_OP_MEM
&&
1081 u
->operand
[1].type
!= UD_OP_MEM
)
1084 u
->insn_offset
= u
->pc
; /* set offset of instruction */
1085 u
->asm_buf_fill
= 0; /* set translation buffer index to 0 */
1086 u
->pc
+= u
->inp_ctr
; /* move program counter by bytes decoded */
1088 /* return number of bytes disassembled. */
1093 vim: set ts=2 sw=2 expandtab