blob: c03c07a3895c539b1a6bde98352a956542b4deb2 [file] [log] [blame]
Sean Callanan8ed9f512009-12-19 02:59:52 +00001/*===- X86DisassemblerDecoderInternal.h - Disassembler decoder -----*- C -*-==*
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
7 *
8 *===----------------------------------------------------------------------===*
9 *
10 * This file is part of the X86 Disassembler.
11 * It contains the public interface of the instruction decoder.
12 * Documentation for the disassembler can be found in X86Disassembler.h.
13 *
14 *===----------------------------------------------------------------------===*/
15
16#ifndef X86DISASSEMBLERDECODER_H
17#define X86DISASSEMBLERDECODER_H
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#define INSTRUCTION_SPECIFIER_FIELDS \
24 const char* name;
25
26#define INSTRUCTION_IDS \
27 InstrUID* instructionIDs;
28
29#include "X86DisassemblerDecoderCommon.h"
30
31#undef INSTRUCTION_SPECIFIER_FIELDS
32#undef INSTRUCTION_IDS
33
34/*
35 * Accessor functions for various fields of an Intel instruction
36 */
Sean Callanan5227cc82009-12-23 01:32:29 +000037#define modFromModRM(modRM) ((modRM & 0xc0) >> 6)
38#define regFromModRM(modRM) ((modRM & 0x38) >> 3)
39#define rmFromModRM(modRM) (modRM & 0x7)
40#define scaleFromSIB(sib) ((sib & 0xc0) >> 6)
41#define indexFromSIB(sib) ((sib & 0x38) >> 3)
42#define baseFromSIB(sib) (sib & 0x7)
43#define wFromREX(rex) ((rex & 0x8) >> 3)
44#define rFromREX(rex) ((rex & 0x4) >> 2)
45#define xFromREX(rex) ((rex & 0x2) >> 1)
46#define bFromREX(rex) (rex & 0x1)
Sean Callanan8ed9f512009-12-19 02:59:52 +000047
48/*
49 * These enums represent Intel registers for use by the decoder.
50 */
51
52#define REGS_8BIT \
53 ENTRY(AL) \
54 ENTRY(CL) \
55 ENTRY(DL) \
56 ENTRY(BL) \
57 ENTRY(AH) \
58 ENTRY(CH) \
59 ENTRY(DH) \
60 ENTRY(BH) \
61 ENTRY(R8B) \
62 ENTRY(R9B) \
63 ENTRY(R10B) \
64 ENTRY(R11B) \
65 ENTRY(R12B) \
66 ENTRY(R13B) \
67 ENTRY(R14B) \
68 ENTRY(R15B) \
69 ENTRY(SPL) \
70 ENTRY(BPL) \
71 ENTRY(SIL) \
72 ENTRY(DIL)
73
74#define EA_BASES_16BIT \
75 ENTRY(BX_SI) \
76 ENTRY(BX_DI) \
77 ENTRY(BP_SI) \
78 ENTRY(BP_DI) \
79 ENTRY(SI) \
80 ENTRY(DI) \
81 ENTRY(BP) \
82 ENTRY(BX) \
83 ENTRY(R8W) \
84 ENTRY(R9W) \
85 ENTRY(R10W) \
86 ENTRY(R11W) \
87 ENTRY(R12W) \
88 ENTRY(R13W) \
89 ENTRY(R14W) \
90 ENTRY(R15W)
91
92#define REGS_16BIT \
93 ENTRY(AX) \
94 ENTRY(CX) \
95 ENTRY(DX) \
96 ENTRY(BX) \
97 ENTRY(SP) \
98 ENTRY(BP) \
99 ENTRY(SI) \
100 ENTRY(DI) \
101 ENTRY(R8W) \
102 ENTRY(R9W) \
103 ENTRY(R10W) \
104 ENTRY(R11W) \
105 ENTRY(R12W) \
106 ENTRY(R13W) \
107 ENTRY(R14W) \
108 ENTRY(R15W)
109
110#define EA_BASES_32BIT \
111 ENTRY(EAX) \
112 ENTRY(ECX) \
113 ENTRY(EDX) \
114 ENTRY(EBX) \
115 ENTRY(sib) \
116 ENTRY(EBP) \
117 ENTRY(ESI) \
118 ENTRY(EDI) \
119 ENTRY(R8D) \
120 ENTRY(R9D) \
121 ENTRY(R10D) \
122 ENTRY(R11D) \
123 ENTRY(R12D) \
124 ENTRY(R13D) \
125 ENTRY(R14D) \
126 ENTRY(R15D)
127
128#define REGS_32BIT \
129 ENTRY(EAX) \
130 ENTRY(ECX) \
131 ENTRY(EDX) \
132 ENTRY(EBX) \
133 ENTRY(ESP) \
134 ENTRY(EBP) \
135 ENTRY(ESI) \
136 ENTRY(EDI) \
137 ENTRY(R8D) \
138 ENTRY(R9D) \
139 ENTRY(R10D) \
140 ENTRY(R11D) \
141 ENTRY(R12D) \
142 ENTRY(R13D) \
143 ENTRY(R14D) \
144 ENTRY(R15D)
145
146#define EA_BASES_64BIT \
147 ENTRY(RAX) \
148 ENTRY(RCX) \
149 ENTRY(RDX) \
150 ENTRY(RBX) \
151 ENTRY(sib64) \
152 ENTRY(RBP) \
153 ENTRY(RSI) \
154 ENTRY(RDI) \
155 ENTRY(R8) \
156 ENTRY(R9) \
157 ENTRY(R10) \
158 ENTRY(R11) \
159 ENTRY(R12) \
160 ENTRY(R13) \
161 ENTRY(R14) \
162 ENTRY(R15)
163
164#define REGS_64BIT \
165 ENTRY(RAX) \
166 ENTRY(RCX) \
167 ENTRY(RDX) \
168 ENTRY(RBX) \
169 ENTRY(RSP) \
170 ENTRY(RBP) \
171 ENTRY(RSI) \
172 ENTRY(RDI) \
173 ENTRY(R8) \
174 ENTRY(R9) \
175 ENTRY(R10) \
176 ENTRY(R11) \
177 ENTRY(R12) \
178 ENTRY(R13) \
179 ENTRY(R14) \
180 ENTRY(R15)
181
182#define REGS_MMX \
183 ENTRY(MM0) \
184 ENTRY(MM1) \
185 ENTRY(MM2) \
186 ENTRY(MM3) \
187 ENTRY(MM4) \
188 ENTRY(MM5) \
189 ENTRY(MM6) \
190 ENTRY(MM7)
191
192#define REGS_XMM \
193 ENTRY(XMM0) \
194 ENTRY(XMM1) \
195 ENTRY(XMM2) \
196 ENTRY(XMM3) \
197 ENTRY(XMM4) \
198 ENTRY(XMM5) \
199 ENTRY(XMM6) \
200 ENTRY(XMM7) \
201 ENTRY(XMM8) \
202 ENTRY(XMM9) \
203 ENTRY(XMM10) \
204 ENTRY(XMM11) \
205 ENTRY(XMM12) \
206 ENTRY(XMM13) \
207 ENTRY(XMM14) \
208 ENTRY(XMM15)
209
210#define REGS_SEGMENT \
211 ENTRY(ES) \
212 ENTRY(CS) \
213 ENTRY(SS) \
214 ENTRY(DS) \
215 ENTRY(FS) \
216 ENTRY(GS)
217
218#define REGS_DEBUG \
219 ENTRY(DR0) \
220 ENTRY(DR1) \
221 ENTRY(DR2) \
222 ENTRY(DR3) \
223 ENTRY(DR4) \
224 ENTRY(DR5) \
225 ENTRY(DR6) \
226 ENTRY(DR7)
227
228#define REGS_CONTROL_32BIT \
229 ENTRY(ECR0) \
230 ENTRY(ECR1) \
231 ENTRY(ECR2) \
232 ENTRY(ECR3) \
233 ENTRY(ECR4) \
234 ENTRY(ECR5) \
235 ENTRY(ECR6) \
236 ENTRY(ECR7)
237
238#define REGS_CONTROL_64BIT \
239 ENTRY(RCR0) \
240 ENTRY(RCR1) \
241 ENTRY(RCR2) \
242 ENTRY(RCR3) \
243 ENTRY(RCR4) \
244 ENTRY(RCR5) \
245 ENTRY(RCR6) \
246 ENTRY(RCR7) \
247 ENTRY(RCR8)
248
249#define ALL_EA_BASES \
250 EA_BASES_16BIT \
251 EA_BASES_32BIT \
252 EA_BASES_64BIT
253
254#define ALL_SIB_BASES \
255 REGS_32BIT \
256 REGS_64BIT
257
258#define ALL_REGS \
259 REGS_8BIT \
260 REGS_16BIT \
261 REGS_32BIT \
262 REGS_64BIT \
263 REGS_MMX \
264 REGS_XMM \
265 REGS_SEGMENT \
266 REGS_DEBUG \
267 REGS_CONTROL_32BIT \
268 REGS_CONTROL_64BIT \
269 ENTRY(RIP)
270
271/*
272 * EABase - All possible values of the base field for effective-address
273 * computations, a.k.a. the Mod and R/M fields of the ModR/M byte. We
274 * distinguish between bases (EA_BASE_*) and registers that just happen to be
275 * referred to when Mod == 0b11 (EA_REG_*).
276 */
277typedef enum {
278 EA_BASE_NONE,
279#define ENTRY(x) EA_BASE_##x,
280 ALL_EA_BASES
281#undef ENTRY
282#define ENTRY(x) EA_REG_##x,
283 ALL_REGS
284#undef ENTRY
285 EA_max
286} EABase;
287
288/*
289 * SIBIndex - All possible values of the SIB index field.
290 * Borrows entries from ALL_EA_BASES with the special case that
291 * sib is synonymous with NONE.
292 */
293typedef enum {
294 SIB_INDEX_NONE,
295#define ENTRY(x) SIB_INDEX_##x,
296 ALL_EA_BASES
297#undef ENTRY
298 SIB_INDEX_max
299} SIBIndex;
300
301/*
302 * SIBBase - All possible values of the SIB base field.
303 */
304typedef enum {
305 SIB_BASE_NONE,
306#define ENTRY(x) SIB_BASE_##x,
307 ALL_SIB_BASES
308#undef ENTRY
309 SIB_BASE_max
310} SIBBase;
311
312/*
313 * EADisplacement - Possible displacement types for effective-address
314 * computations.
315 */
316typedef enum {
317 EA_DISP_NONE,
318 EA_DISP_8,
319 EA_DISP_16,
320 EA_DISP_32
321} EADisplacement;
322
323/*
324 * Reg - All possible values of the reg field in the ModR/M byte.
325 */
326typedef enum {
Sean Callanan06b766d2009-12-22 02:07:42 +0000327#define ENTRY(x) MODRM_REG_##x,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000328 ALL_REGS
329#undef ENTRY
Sean Callanan06b766d2009-12-22 02:07:42 +0000330 MODRM_REG_max
Sean Callanan8ed9f512009-12-19 02:59:52 +0000331} Reg;
332
333/*
334 * SegmentOverride - All possible segment overrides.
335 */
336typedef enum {
337 SEG_OVERRIDE_NONE,
338 SEG_OVERRIDE_CS,
339 SEG_OVERRIDE_SS,
340 SEG_OVERRIDE_DS,
341 SEG_OVERRIDE_ES,
342 SEG_OVERRIDE_FS,
343 SEG_OVERRIDE_GS,
344 SEG_OVERRIDE_max
345} SegmentOverride;
346
347typedef uint8_t BOOL;
348
349/*
350 * byteReader_t - Type for the byte reader that the consumer must provide to
351 * the decoder. Reads a single byte from the instruction's address space.
352 * @param arg - A baton that the consumer can associate with any internal
353 * state that it needs.
354 * @param byte - A pointer to a single byte in memory that should be set to
355 * contain the value at address.
356 * @param address - The address in the instruction's address space that should
357 * be read from.
358 * @return - -1 if the byte cannot be read for any reason; 0 otherwise.
359 */
360typedef int (*byteReader_t)(void* arg, uint8_t* byte, uint64_t address);
361
362/*
363 * dlog_t - Type for the logging function that the consumer can provide to
364 * get debugging output from the decoder.
365 * @param arg - A baton that the consumer can associate with any internal
366 * state that it needs.
367 * @param log - A string that contains the message. Will be reused after
368 * the logger returns.
369 */
370typedef void (*dlog_t)(void* arg, const char *log);
371
372/*
373 * The x86 internal instruction, which is produced by the decoder.
374 */
375struct InternalInstruction {
376 /* Reader interface (C) */
377 byteReader_t reader;
378 /* Opaque value passed to the reader */
379 void* readerArg;
380 /* The address of the next byte to read via the reader */
381 uint64_t readerCursor;
382
383 /* Logger interface (C) */
384 dlog_t dlog;
385 /* Opaque value passed to the logger */
386 void* dlogArg;
387
388 /* General instruction information */
389
390 /* The mode to disassemble for (64-bit, protected, real) */
391 DisassemblerMode mode;
392 /* The start of the instruction, usable with the reader */
393 uint64_t startLocation;
394 /* The length of the instruction, in bytes */
395 size_t length;
396
397 /* Prefix state */
398
399 /* 1 if the prefix byte corresponding to the entry is present; 0 if not */
400 uint8_t prefixPresent[0x100];
401 /* contains the location (for use with the reader) of the prefix byte */
402 uint64_t prefixLocations[0x100];
403 /* The value of the REX prefix, if present */
404 uint8_t rexPrefix;
405 /* The location of the REX prefix */
406 uint64_t rexLocation;
407 /* The location where a mandatory prefix would have to be (i.e., right before
408 the opcode, or right before the REX prefix if one is present) */
409 uint64_t necessaryPrefixLocation;
410 /* The segment override type */
411 SegmentOverride segmentOverride;
412
413 /* Sizes of various critical pieces of data */
414 uint8_t registerSize;
415 uint8_t addressSize;
416 uint8_t displacementSize;
417 uint8_t immediateSize;
418
419 /* opcode state */
420
421 /* The value of the two-byte escape prefix (usually 0x0f) */
422 uint8_t twoByteEscape;
423 /* The value of the three-byte escape prefix (usually 0x38 or 0x3a) */
424 uint8_t threeByteEscape;
425 /* The last byte of the opcode, not counting any ModR/M extension */
426 uint8_t opcode;
427 /* The ModR/M byte of the instruction, if it is an opcode extension */
428 uint8_t modRMExtension;
429
430 /* decode state */
431
432 /* The type of opcode, used for indexing into the array of decode tables */
433 OpcodeType opcodeType;
434 /* The instruction ID, extracted from the decode table */
435 uint16_t instructionID;
436 /* The specifier for the instruction, from the instruction info table */
437 struct InstructionSpecifier* spec;
438
439 /* state for additional bytes, consumed during operand decode. Pattern:
440 consumed___ indicates that the byte was already consumed and does not
441 need to be consumed again */
442
443 /* The ModR/M byte, which contains most register operands and some portion of
444 all memory operands */
445 BOOL consumedModRM;
446 uint8_t modRM;
447
448 /* The SIB byte, used for more complex 32- or 64-bit memory operands */
449 BOOL consumedSIB;
450 uint8_t sib;
451
452 /* The displacement, used for memory operands */
453 BOOL consumedDisplacement;
454 int32_t displacement;
455
456 /* Immediates. There can be two in some cases */
457 uint8_t numImmediatesConsumed;
458 uint8_t numImmediatesTranslated;
459 uint64_t immediates[2];
460
461 /* A register or immediate operand encoded into the opcode */
462 BOOL consumedOpcodeModifier;
463 uint8_t opcodeModifier;
464 Reg opcodeRegister;
465
466 /* Portions of the ModR/M byte */
467
468 /* These fields determine the allowable values for the ModR/M fields, which
469 depend on operand and address widths */
470 EABase eaBaseBase;
471 EABase eaRegBase;
472 Reg regBase;
473
474 /* The Mod and R/M fields can encode a base for an effective address, or a
475 register. These are separated into two fields here */
476 EABase eaBase;
477 EADisplacement eaDisplacement;
478 /* The reg field always encodes a register */
479 Reg reg;
480
481 /* SIB state */
482 SIBIndex sibIndex;
483 uint8_t sibScale;
484 SIBBase sibBase;
485};
486
487/* decodeInstruction - Decode one instruction and store the decoding results in
488 * a buffer provided by the consumer.
489 * @param insn - The buffer to store the instruction in. Allocated by the
490 * consumer.
491 * @param reader - The byteReader_t for the bytes to be read.
492 * @param readerArg - An argument to pass to the reader for storing context
493 * specific to the consumer. May be NULL.
494 * @param logger - The dlog_t to be used in printing status messages from the
495 * disassembler. May be NULL.
496 * @param loggerArg - An argument to pass to the logger for storing context
497 * specific to the logger. May be NULL.
498 * @param startLoc - The address (in the reader's address space) of the first
499 * byte in the instruction.
500 * @param mode - The mode (16-bit, 32-bit, 64-bit) to decode in.
501 * @return - Nonzero if there was an error during decode, 0 otherwise.
502 */
503int decodeInstruction(struct InternalInstruction* insn,
504 byteReader_t reader,
505 void* readerArg,
506 dlog_t logger,
507 void* loggerArg,
508 uint64_t startLoc,
509 DisassemblerMode mode);
510
511#ifdef __cplusplus
512}
513#endif
514
515#endif