blob: 67f52e55fac4960310a3b32c685615264bfaf379 [file] [log] [blame]
Richard Smith89ee75d2014-04-20 21:07:34 +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//===----------------------------------------------------------------------===//
Sean Callanan04cc3072009-12-19 02:59:52 +000015
16#ifndef X86DISASSEMBLERDECODER_H
17#define X86DISASSEMBLERDECODER_H
18
Sean Callanan04cc3072009-12-19 02:59:52 +000019#define INSTRUCTION_IDS \
Craig Topper8702c5b2012-09-11 04:19:21 +000020 uint16_t instructionIDs;
Sean Callanan04cc3072009-12-19 02:59:52 +000021
22#include "X86DisassemblerDecoderCommon.h"
Craig Topperfb39f972012-07-31 04:58:05 +000023
Sean Callanan04cc3072009-12-19 02:59:52 +000024#undef INSTRUCTION_SPECIFIER_FIELDS
25#undef INSTRUCTION_IDS
Craig Topperfb39f972012-07-31 04:58:05 +000026
Richard Smith89ee75d2014-04-20 21:07:34 +000027namespace llvm {
28namespace X86Disassembler {
29
Sean Callanan04cc3072009-12-19 02:59:52 +000030/*
31 * Accessor functions for various fields of an Intel instruction
32 */
Sean Callananc3fd5232011-03-15 01:23:15 +000033#define modFromModRM(modRM) (((modRM) & 0xc0) >> 6)
34#define regFromModRM(modRM) (((modRM) & 0x38) >> 3)
35#define rmFromModRM(modRM) ((modRM) & 0x7)
36#define scaleFromSIB(sib) (((sib) & 0xc0) >> 6)
37#define indexFromSIB(sib) (((sib) & 0x38) >> 3)
38#define baseFromSIB(sib) ((sib) & 0x7)
39#define wFromREX(rex) (((rex) & 0x8) >> 3)
40#define rFromREX(rex) (((rex) & 0x4) >> 2)
41#define xFromREX(rex) (((rex) & 0x2) >> 1)
42#define bFromREX(rex) ((rex) & 0x1)
Craig Topperfb39f972012-07-31 04:58:05 +000043
Elena Demikhovsky371e3632013-12-25 11:40:51 +000044#define rFromEVEX2of4(evex) (((~(evex)) & 0x80) >> 7)
45#define xFromEVEX2of4(evex) (((~(evex)) & 0x40) >> 6)
46#define bFromEVEX2of4(evex) (((~(evex)) & 0x20) >> 5)
47#define r2FromEVEX2of4(evex) (((~(evex)) & 0x10) >> 4)
48#define mmFromEVEX2of4(evex) ((evex) & 0x3)
49#define wFromEVEX3of4(evex) (((evex) & 0x80) >> 7)
50#define vvvvFromEVEX3of4(evex) (((~(evex)) & 0x78) >> 3)
51#define ppFromEVEX3of4(evex) ((evex) & 0x3)
52#define zFromEVEX4of4(evex) (((evex) & 0x80) >> 7)
53#define l2FromEVEX4of4(evex) (((evex) & 0x40) >> 6)
54#define lFromEVEX4of4(evex) (((evex) & 0x20) >> 5)
55#define bFromEVEX4of4(evex) (((evex) & 0x10) >> 4)
56#define v2FromEVEX4of4(evex) (((~evex) & 0x8) >> 3)
57#define aaaFromEVEX4of4(evex) ((evex) & 0x7)
58
Sean Callananc3fd5232011-03-15 01:23:15 +000059#define rFromVEX2of3(vex) (((~(vex)) & 0x80) >> 7)
60#define xFromVEX2of3(vex) (((~(vex)) & 0x40) >> 6)
61#define bFromVEX2of3(vex) (((~(vex)) & 0x20) >> 5)
62#define mmmmmFromVEX2of3(vex) ((vex) & 0x1f)
63#define wFromVEX3of3(vex) (((vex) & 0x80) >> 7)
64#define vvvvFromVEX3of3(vex) (((~(vex)) & 0x78) >> 3)
65#define lFromVEX3of3(vex) (((vex) & 0x4) >> 2)
66#define ppFromVEX3of3(vex) ((vex) & 0x3)
67
68#define rFromVEX2of2(vex) (((~(vex)) & 0x80) >> 7)
69#define vvvvFromVEX2of2(vex) (((~(vex)) & 0x78) >> 3)
70#define lFromVEX2of2(vex) (((vex) & 0x4) >> 2)
71#define ppFromVEX2of2(vex) ((vex) & 0x3)
Sean Callanan04cc3072009-12-19 02:59:52 +000072
Craig Topper9e3e38a2013-10-03 05:17:48 +000073#define rFromXOP2of3(xop) (((~(xop)) & 0x80) >> 7)
74#define xFromXOP2of3(xop) (((~(xop)) & 0x40) >> 6)
75#define bFromXOP2of3(xop) (((~(xop)) & 0x20) >> 5)
76#define mmmmmFromXOP2of3(xop) ((xop) & 0x1f)
77#define wFromXOP3of3(xop) (((xop) & 0x80) >> 7)
78#define vvvvFromXOP3of3(vex) (((~(vex)) & 0x78) >> 3)
79#define lFromXOP3of3(xop) (((xop) & 0x4) >> 2)
80#define ppFromXOP3of3(xop) ((xop) & 0x3)
81
Sean Callanan04cc3072009-12-19 02:59:52 +000082/*
83 * These enums represent Intel registers for use by the decoder.
84 */
85
86#define REGS_8BIT \
87 ENTRY(AL) \
88 ENTRY(CL) \
89 ENTRY(DL) \
90 ENTRY(BL) \
91 ENTRY(AH) \
92 ENTRY(CH) \
93 ENTRY(DH) \
94 ENTRY(BH) \
95 ENTRY(R8B) \
96 ENTRY(R9B) \
97 ENTRY(R10B) \
98 ENTRY(R11B) \
99 ENTRY(R12B) \
100 ENTRY(R13B) \
101 ENTRY(R14B) \
102 ENTRY(R15B) \
103 ENTRY(SPL) \
104 ENTRY(BPL) \
105 ENTRY(SIL) \
106 ENTRY(DIL)
107
108#define EA_BASES_16BIT \
109 ENTRY(BX_SI) \
110 ENTRY(BX_DI) \
111 ENTRY(BP_SI) \
112 ENTRY(BP_DI) \
113 ENTRY(SI) \
114 ENTRY(DI) \
115 ENTRY(BP) \
116 ENTRY(BX) \
117 ENTRY(R8W) \
118 ENTRY(R9W) \
119 ENTRY(R10W) \
120 ENTRY(R11W) \
121 ENTRY(R12W) \
122 ENTRY(R13W) \
123 ENTRY(R14W) \
124 ENTRY(R15W)
125
126#define REGS_16BIT \
127 ENTRY(AX) \
128 ENTRY(CX) \
129 ENTRY(DX) \
130 ENTRY(BX) \
131 ENTRY(SP) \
132 ENTRY(BP) \
133 ENTRY(SI) \
134 ENTRY(DI) \
135 ENTRY(R8W) \
136 ENTRY(R9W) \
137 ENTRY(R10W) \
138 ENTRY(R11W) \
139 ENTRY(R12W) \
140 ENTRY(R13W) \
141 ENTRY(R14W) \
142 ENTRY(R15W)
143
144#define EA_BASES_32BIT \
145 ENTRY(EAX) \
146 ENTRY(ECX) \
147 ENTRY(EDX) \
148 ENTRY(EBX) \
149 ENTRY(sib) \
150 ENTRY(EBP) \
151 ENTRY(ESI) \
152 ENTRY(EDI) \
153 ENTRY(R8D) \
154 ENTRY(R9D) \
155 ENTRY(R10D) \
156 ENTRY(R11D) \
157 ENTRY(R12D) \
158 ENTRY(R13D) \
159 ENTRY(R14D) \
160 ENTRY(R15D)
161
162#define REGS_32BIT \
163 ENTRY(EAX) \
164 ENTRY(ECX) \
165 ENTRY(EDX) \
166 ENTRY(EBX) \
167 ENTRY(ESP) \
168 ENTRY(EBP) \
169 ENTRY(ESI) \
170 ENTRY(EDI) \
171 ENTRY(R8D) \
172 ENTRY(R9D) \
173 ENTRY(R10D) \
174 ENTRY(R11D) \
175 ENTRY(R12D) \
176 ENTRY(R13D) \
177 ENTRY(R14D) \
178 ENTRY(R15D)
179
180#define EA_BASES_64BIT \
181 ENTRY(RAX) \
182 ENTRY(RCX) \
183 ENTRY(RDX) \
184 ENTRY(RBX) \
185 ENTRY(sib64) \
186 ENTRY(RBP) \
187 ENTRY(RSI) \
188 ENTRY(RDI) \
189 ENTRY(R8) \
190 ENTRY(R9) \
191 ENTRY(R10) \
192 ENTRY(R11) \
193 ENTRY(R12) \
194 ENTRY(R13) \
195 ENTRY(R14) \
196 ENTRY(R15)
197
198#define REGS_64BIT \
199 ENTRY(RAX) \
200 ENTRY(RCX) \
201 ENTRY(RDX) \
202 ENTRY(RBX) \
203 ENTRY(RSP) \
204 ENTRY(RBP) \
205 ENTRY(RSI) \
206 ENTRY(RDI) \
207 ENTRY(R8) \
208 ENTRY(R9) \
209 ENTRY(R10) \
210 ENTRY(R11) \
211 ENTRY(R12) \
212 ENTRY(R13) \
213 ENTRY(R14) \
214 ENTRY(R15)
215
216#define REGS_MMX \
217 ENTRY(MM0) \
218 ENTRY(MM1) \
219 ENTRY(MM2) \
220 ENTRY(MM3) \
221 ENTRY(MM4) \
222 ENTRY(MM5) \
223 ENTRY(MM6) \
224 ENTRY(MM7)
225
226#define REGS_XMM \
227 ENTRY(XMM0) \
228 ENTRY(XMM1) \
229 ENTRY(XMM2) \
230 ENTRY(XMM3) \
231 ENTRY(XMM4) \
232 ENTRY(XMM5) \
233 ENTRY(XMM6) \
234 ENTRY(XMM7) \
235 ENTRY(XMM8) \
236 ENTRY(XMM9) \
237 ENTRY(XMM10) \
238 ENTRY(XMM11) \
239 ENTRY(XMM12) \
240 ENTRY(XMM13) \
241 ENTRY(XMM14) \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000242 ENTRY(XMM15) \
243 ENTRY(XMM16) \
244 ENTRY(XMM17) \
245 ENTRY(XMM18) \
246 ENTRY(XMM19) \
247 ENTRY(XMM20) \
248 ENTRY(XMM21) \
249 ENTRY(XMM22) \
250 ENTRY(XMM23) \
251 ENTRY(XMM24) \
252 ENTRY(XMM25) \
253 ENTRY(XMM26) \
254 ENTRY(XMM27) \
255 ENTRY(XMM28) \
256 ENTRY(XMM29) \
257 ENTRY(XMM30) \
258 ENTRY(XMM31)
Sean Callananc3fd5232011-03-15 01:23:15 +0000259
260#define REGS_YMM \
261 ENTRY(YMM0) \
262 ENTRY(YMM1) \
263 ENTRY(YMM2) \
264 ENTRY(YMM3) \
265 ENTRY(YMM4) \
266 ENTRY(YMM5) \
267 ENTRY(YMM6) \
268 ENTRY(YMM7) \
269 ENTRY(YMM8) \
270 ENTRY(YMM9) \
271 ENTRY(YMM10) \
272 ENTRY(YMM11) \
273 ENTRY(YMM12) \
274 ENTRY(YMM13) \
275 ENTRY(YMM14) \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000276 ENTRY(YMM15) \
277 ENTRY(YMM16) \
278 ENTRY(YMM17) \
279 ENTRY(YMM18) \
280 ENTRY(YMM19) \
281 ENTRY(YMM20) \
282 ENTRY(YMM21) \
283 ENTRY(YMM22) \
284 ENTRY(YMM23) \
285 ENTRY(YMM24) \
286 ENTRY(YMM25) \
287 ENTRY(YMM26) \
288 ENTRY(YMM27) \
289 ENTRY(YMM28) \
290 ENTRY(YMM29) \
291 ENTRY(YMM30) \
292 ENTRY(YMM31)
293
294#define REGS_ZMM \
295 ENTRY(ZMM0) \
296 ENTRY(ZMM1) \
297 ENTRY(ZMM2) \
298 ENTRY(ZMM3) \
299 ENTRY(ZMM4) \
300 ENTRY(ZMM5) \
301 ENTRY(ZMM6) \
302 ENTRY(ZMM7) \
303 ENTRY(ZMM8) \
304 ENTRY(ZMM9) \
305 ENTRY(ZMM10) \
306 ENTRY(ZMM11) \
307 ENTRY(ZMM12) \
308 ENTRY(ZMM13) \
309 ENTRY(ZMM14) \
310 ENTRY(ZMM15) \
311 ENTRY(ZMM16) \
312 ENTRY(ZMM17) \
313 ENTRY(ZMM18) \
314 ENTRY(ZMM19) \
315 ENTRY(ZMM20) \
316 ENTRY(ZMM21) \
317 ENTRY(ZMM22) \
318 ENTRY(ZMM23) \
319 ENTRY(ZMM24) \
320 ENTRY(ZMM25) \
321 ENTRY(ZMM26) \
322 ENTRY(ZMM27) \
323 ENTRY(ZMM28) \
324 ENTRY(ZMM29) \
325 ENTRY(ZMM30) \
326 ENTRY(ZMM31)
Craig Topperfb39f972012-07-31 04:58:05 +0000327
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000328#define REGS_MASKS \
329 ENTRY(K0) \
330 ENTRY(K1) \
331 ENTRY(K2) \
332 ENTRY(K3) \
333 ENTRY(K4) \
334 ENTRY(K5) \
335 ENTRY(K6) \
336 ENTRY(K7)
337
Sean Callanan04cc3072009-12-19 02:59:52 +0000338#define REGS_SEGMENT \
339 ENTRY(ES) \
340 ENTRY(CS) \
341 ENTRY(SS) \
342 ENTRY(DS) \
343 ENTRY(FS) \
344 ENTRY(GS)
Craig Topperfb39f972012-07-31 04:58:05 +0000345
Sean Callanan04cc3072009-12-19 02:59:52 +0000346#define REGS_DEBUG \
347 ENTRY(DR0) \
348 ENTRY(DR1) \
349 ENTRY(DR2) \
350 ENTRY(DR3) \
351 ENTRY(DR4) \
352 ENTRY(DR5) \
353 ENTRY(DR6) \
354 ENTRY(DR7)
355
Sean Callanane7e1cf92010-05-06 20:59:00 +0000356#define REGS_CONTROL \
357 ENTRY(CR0) \
358 ENTRY(CR1) \
359 ENTRY(CR2) \
360 ENTRY(CR3) \
361 ENTRY(CR4) \
362 ENTRY(CR5) \
363 ENTRY(CR6) \
364 ENTRY(CR7) \
365 ENTRY(CR8)
Craig Topperfb39f972012-07-31 04:58:05 +0000366
Sean Callanan04cc3072009-12-19 02:59:52 +0000367#define ALL_EA_BASES \
368 EA_BASES_16BIT \
369 EA_BASES_32BIT \
370 EA_BASES_64BIT
Craig Topperfb39f972012-07-31 04:58:05 +0000371
Sean Callanan04cc3072009-12-19 02:59:52 +0000372#define ALL_SIB_BASES \
373 REGS_32BIT \
374 REGS_64BIT
375
376#define ALL_REGS \
377 REGS_8BIT \
378 REGS_16BIT \
379 REGS_32BIT \
380 REGS_64BIT \
381 REGS_MMX \
382 REGS_XMM \
Sean Callananc3fd5232011-03-15 01:23:15 +0000383 REGS_YMM \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000384 REGS_ZMM \
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000385 REGS_MASKS \
Sean Callanan04cc3072009-12-19 02:59:52 +0000386 REGS_SEGMENT \
387 REGS_DEBUG \
Sean Callanane7e1cf92010-05-06 20:59:00 +0000388 REGS_CONTROL \
Sean Callanan04cc3072009-12-19 02:59:52 +0000389 ENTRY(RIP)
390
391/*
Craig Topperfb39f972012-07-31 04:58:05 +0000392 * EABase - All possible values of the base field for effective-address
Sean Callanan04cc3072009-12-19 02:59:52 +0000393 * computations, a.k.a. the Mod and R/M fields of the ModR/M byte. We
394 * distinguish between bases (EA_BASE_*) and registers that just happen to be
395 * referred to when Mod == 0b11 (EA_REG_*).
396 */
397typedef enum {
398 EA_BASE_NONE,
399#define ENTRY(x) EA_BASE_##x,
400 ALL_EA_BASES
401#undef ENTRY
402#define ENTRY(x) EA_REG_##x,
403 ALL_REGS
404#undef ENTRY
405 EA_max
406} EABase;
Craig Topperfb39f972012-07-31 04:58:05 +0000407
408/*
Sean Callanan04cc3072009-12-19 02:59:52 +0000409 * SIBIndex - All possible values of the SIB index field.
410 * Borrows entries from ALL_EA_BASES with the special case that
411 * sib is synonymous with NONE.
Manman Rena0982042012-06-26 19:47:59 +0000412 * Vector SIB: index can be XMM or YMM.
Sean Callanan04cc3072009-12-19 02:59:52 +0000413 */
414typedef enum {
415 SIB_INDEX_NONE,
416#define ENTRY(x) SIB_INDEX_##x,
417 ALL_EA_BASES
Manman Rena0982042012-06-26 19:47:59 +0000418 REGS_XMM
419 REGS_YMM
Elena Demikhovsky003e7d72013-07-28 08:28:38 +0000420 REGS_ZMM
Sean Callanan04cc3072009-12-19 02:59:52 +0000421#undef ENTRY
422 SIB_INDEX_max
423} SIBIndex;
Craig Topperfb39f972012-07-31 04:58:05 +0000424
Sean Callanan04cc3072009-12-19 02:59:52 +0000425/*
426 * SIBBase - All possible values of the SIB base field.
427 */
428typedef enum {
429 SIB_BASE_NONE,
430#define ENTRY(x) SIB_BASE_##x,
431 ALL_SIB_BASES
432#undef ENTRY
433 SIB_BASE_max
434} SIBBase;
435
436/*
437 * EADisplacement - Possible displacement types for effective-address
438 * computations.
439 */
440typedef enum {
441 EA_DISP_NONE,
442 EA_DISP_8,
443 EA_DISP_16,
444 EA_DISP_32
445} EADisplacement;
446
447/*
448 * Reg - All possible values of the reg field in the ModR/M byte.
449 */
450typedef enum {
Sean Callanan2f9443f2009-12-22 02:07:42 +0000451#define ENTRY(x) MODRM_REG_##x,
Sean Callanan04cc3072009-12-19 02:59:52 +0000452 ALL_REGS
453#undef ENTRY
Sean Callanan2f9443f2009-12-22 02:07:42 +0000454 MODRM_REG_max
Sean Callanan04cc3072009-12-19 02:59:52 +0000455} Reg;
Craig Topperfb39f972012-07-31 04:58:05 +0000456
Sean Callanan04cc3072009-12-19 02:59:52 +0000457/*
458 * SegmentOverride - All possible segment overrides.
459 */
460typedef enum {
461 SEG_OVERRIDE_NONE,
462 SEG_OVERRIDE_CS,
463 SEG_OVERRIDE_SS,
464 SEG_OVERRIDE_DS,
465 SEG_OVERRIDE_ES,
466 SEG_OVERRIDE_FS,
467 SEG_OVERRIDE_GS,
468 SEG_OVERRIDE_max
469} SegmentOverride;
Craig Topperfb39f972012-07-31 04:58:05 +0000470
Sean Callananc3fd5232011-03-15 01:23:15 +0000471/*
472 * VEXLeadingOpcodeByte - Possible values for the VEX.m-mmmm field
473 */
474
475typedef enum {
476 VEX_LOB_0F = 0x1,
477 VEX_LOB_0F38 = 0x2,
Craig Topper42e8a632013-10-03 06:18:26 +0000478 VEX_LOB_0F3A = 0x3
Sean Callananc3fd5232011-03-15 01:23:15 +0000479} VEXLeadingOpcodeByte;
480
Craig Topper9e3e38a2013-10-03 05:17:48 +0000481typedef enum {
482 XOP_MAP_SELECT_8 = 0x8,
483 XOP_MAP_SELECT_9 = 0x9,
484 XOP_MAP_SELECT_A = 0xA
485} XOPMapSelect;
486
Sean Callananc3fd5232011-03-15 01:23:15 +0000487/*
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000488 * VEXPrefixCode - Possible values for the VEX.pp/EVEX.pp field
Sean Callananc3fd5232011-03-15 01:23:15 +0000489 */
490
491typedef enum {
492 VEX_PREFIX_NONE = 0x0,
493 VEX_PREFIX_66 = 0x1,
494 VEX_PREFIX_F3 = 0x2,
495 VEX_PREFIX_F2 = 0x3
496} VEXPrefixCode;
Sean Callanan04cc3072009-12-19 02:59:52 +0000497
Craig Topper9e3e38a2013-10-03 05:17:48 +0000498typedef enum {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000499 TYPE_NO_VEX_XOP = 0x0,
500 TYPE_VEX_2B = 0x1,
501 TYPE_VEX_3B = 0x2,
502 TYPE_EVEX = 0x3,
503 TYPE_XOP = 0x4
504} VectorExtensionType;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000505
Sean Callanan04cc3072009-12-19 02:59:52 +0000506typedef uint8_t BOOL;
507
508/*
509 * byteReader_t - Type for the byte reader that the consumer must provide to
510 * the decoder. Reads a single byte from the instruction's address space.
511 * @param arg - A baton that the consumer can associate with any internal
512 * state that it needs.
513 * @param byte - A pointer to a single byte in memory that should be set to
514 * contain the value at address.
515 * @param address - The address in the instruction's address space that should
516 * be read from.
517 * @return - -1 if the byte cannot be read for any reason; 0 otherwise.
518 */
Roman Divacky67923802012-09-05 21:17:34 +0000519typedef int (*byteReader_t)(const void* arg, uint8_t* byte, uint64_t address);
Sean Callanan04cc3072009-12-19 02:59:52 +0000520
521/*
522 * dlog_t - Type for the logging function that the consumer can provide to
523 * get debugging output from the decoder.
524 * @param arg - A baton that the consumer can associate with any internal
525 * state that it needs.
526 * @param log - A string that contains the message. Will be reused after
527 * the logger returns.
528 */
529typedef void (*dlog_t)(void* arg, const char *log);
530
531/*
Richard Smith82b47d52014-04-20 21:35:26 +0000532 * The specification for how to extract and interpret a full instruction and
533 * its operands.
534 */
535struct InstructionSpecifier {
536 uint16_t operands;
537};
538
539/*
Sean Callanan04cc3072009-12-19 02:59:52 +0000540 * The x86 internal instruction, which is produced by the decoder.
541 */
542struct InternalInstruction {
543 /* Reader interface (C) */
544 byteReader_t reader;
545 /* Opaque value passed to the reader */
Roman Divacky67923802012-09-05 21:17:34 +0000546 const void* readerArg;
Sean Callanan04cc3072009-12-19 02:59:52 +0000547 /* The address of the next byte to read via the reader */
548 uint64_t readerCursor;
549
550 /* Logger interface (C) */
551 dlog_t dlog;
552 /* Opaque value passed to the logger */
553 void* dlogArg;
554
555 /* General instruction information */
Craig Topperfb39f972012-07-31 04:58:05 +0000556
Sean Callanan04cc3072009-12-19 02:59:52 +0000557 /* The mode to disassemble for (64-bit, protected, real) */
558 DisassemblerMode mode;
559 /* The start of the instruction, usable with the reader */
560 uint64_t startLocation;
561 /* The length of the instruction, in bytes */
562 size_t length;
Craig Topperfb39f972012-07-31 04:58:05 +0000563
Sean Callanan04cc3072009-12-19 02:59:52 +0000564 /* Prefix state */
Craig Topperfb39f972012-07-31 04:58:05 +0000565
Sean Callanan04cc3072009-12-19 02:59:52 +0000566 /* 1 if the prefix byte corresponding to the entry is present; 0 if not */
567 uint8_t prefixPresent[0x100];
568 /* contains the location (for use with the reader) of the prefix byte */
569 uint64_t prefixLocations[0x100];
Alp Tokercb402912014-01-24 17:20:08 +0000570 /* The value of the vector extension prefix(EVEX/VEX/XOP), if present */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000571 uint8_t vectorExtensionPrefix[4];
572 /* The type of the vector extension prefix */
573 VectorExtensionType vectorExtensionType;
Sean Callanan04cc3072009-12-19 02:59:52 +0000574 /* The value of the REX prefix, if present */
575 uint8_t rexPrefix;
Sean Callanan04cc3072009-12-19 02:59:52 +0000576 /* The location where a mandatory prefix would have to be (i.e., right before
577 the opcode, or right before the REX prefix if one is present) */
578 uint64_t necessaryPrefixLocation;
579 /* The segment override type */
580 SegmentOverride segmentOverride;
Kevin Enderby35fd7922013-06-20 22:32:18 +0000581 /* 1 if the prefix byte, 0xf2 or 0xf3 is xacquire or xrelease */
582 BOOL xAcquireRelease;
Craig Topperfb39f972012-07-31 04:58:05 +0000583
Sean Callanan5e8603d2011-02-21 21:55:05 +0000584 /* Sizes of various critical pieces of data, in bytes */
Sean Callanan04cc3072009-12-19 02:59:52 +0000585 uint8_t registerSize;
586 uint8_t addressSize;
587 uint8_t displacementSize;
588 uint8_t immediateSize;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000589
590 /* Offsets from the start of the instruction to the pieces of data, which is
591 needed to find relocation entries for adding symbolic operands */
592 uint8_t displacementOffset;
593 uint8_t immediateOffset;
Craig Topperfb39f972012-07-31 04:58:05 +0000594
Sean Callanan04cc3072009-12-19 02:59:52 +0000595 /* opcode state */
Craig Topperfb39f972012-07-31 04:58:05 +0000596
Sean Callanan04cc3072009-12-19 02:59:52 +0000597 /* The last byte of the opcode, not counting any ModR/M extension */
598 uint8_t opcode;
599 /* The ModR/M byte of the instruction, if it is an opcode extension */
600 uint8_t modRMExtension;
Craig Topperfb39f972012-07-31 04:58:05 +0000601
Sean Callanan04cc3072009-12-19 02:59:52 +0000602 /* decode state */
Craig Topperfb39f972012-07-31 04:58:05 +0000603
Sean Callanan04cc3072009-12-19 02:59:52 +0000604 /* The type of opcode, used for indexing into the array of decode tables */
605 OpcodeType opcodeType;
606 /* The instruction ID, extracted from the decode table */
607 uint16_t instructionID;
608 /* The specifier for the instruction, from the instruction info table */
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000609 const struct InstructionSpecifier *spec;
Craig Topperfb39f972012-07-31 04:58:05 +0000610
Sean Callanan04cc3072009-12-19 02:59:52 +0000611 /* state for additional bytes, consumed during operand decode. Pattern:
612 consumed___ indicates that the byte was already consumed and does not
613 need to be consumed again */
Sean Callananc3fd5232011-03-15 01:23:15 +0000614
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000615 /* The VEX.vvvv field, which contains a third register operand for some AVX
Sean Callananc3fd5232011-03-15 01:23:15 +0000616 instructions */
617 Reg vvvv;
Craig Topperfb39f972012-07-31 04:58:05 +0000618
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000619 /* The writemask for AVX-512 instructions which is contained in EVEX.aaa */
620 Reg writemask;
621
Sean Callanan04cc3072009-12-19 02:59:52 +0000622 /* The ModR/M byte, which contains most register operands and some portion of
623 all memory operands */
624 BOOL consumedModRM;
625 uint8_t modRM;
Craig Topperfb39f972012-07-31 04:58:05 +0000626
Sean Callanan04cc3072009-12-19 02:59:52 +0000627 /* The SIB byte, used for more complex 32- or 64-bit memory operands */
628 BOOL consumedSIB;
629 uint8_t sib;
630
631 /* The displacement, used for memory operands */
632 BOOL consumedDisplacement;
633 int32_t displacement;
Craig Topperfb39f972012-07-31 04:58:05 +0000634
Sean Callanan04cc3072009-12-19 02:59:52 +0000635 /* Immediates. There can be two in some cases */
636 uint8_t numImmediatesConsumed;
637 uint8_t numImmediatesTranslated;
638 uint64_t immediates[2];
Craig Topperfb39f972012-07-31 04:58:05 +0000639
Sean Callanan04cc3072009-12-19 02:59:52 +0000640 /* A register or immediate operand encoded into the opcode */
Sean Callanan04cc3072009-12-19 02:59:52 +0000641 Reg opcodeRegister;
Craig Topperfb39f972012-07-31 04:58:05 +0000642
Sean Callanan04cc3072009-12-19 02:59:52 +0000643 /* Portions of the ModR/M byte */
Craig Topperfb39f972012-07-31 04:58:05 +0000644
Sean Callanan04cc3072009-12-19 02:59:52 +0000645 /* These fields determine the allowable values for the ModR/M fields, which
646 depend on operand and address widths */
647 EABase eaBaseBase;
648 EABase eaRegBase;
649 Reg regBase;
650
651 /* The Mod and R/M fields can encode a base for an effective address, or a
652 register. These are separated into two fields here */
653 EABase eaBase;
654 EADisplacement eaDisplacement;
655 /* The reg field always encodes a register */
656 Reg reg;
Craig Topperfb39f972012-07-31 04:58:05 +0000657
Sean Callanan04cc3072009-12-19 02:59:52 +0000658 /* SIB state */
659 SIBIndex sibIndex;
660 uint8_t sibScale;
661 SIBBase sibBase;
Craig Topperb8aec082012-08-01 07:39:18 +0000662
663 const struct OperandSpecifier *operands;
Sean Callanan04cc3072009-12-19 02:59:52 +0000664};
665
666/* decodeInstruction - Decode one instruction and store the decoding results in
667 * a buffer provided by the consumer.
668 * @param insn - The buffer to store the instruction in. Allocated by the
669 * consumer.
670 * @param reader - The byteReader_t for the bytes to be read.
671 * @param readerArg - An argument to pass to the reader for storing context
672 * specific to the consumer. May be NULL.
673 * @param logger - The dlog_t to be used in printing status messages from the
674 * disassembler. May be NULL.
675 * @param loggerArg - An argument to pass to the logger for storing context
676 * specific to the logger. May be NULL.
677 * @param startLoc - The address (in the reader's address space) of the first
678 * byte in the instruction.
679 * @param mode - The mode (16-bit, 32-bit, 64-bit) to decode in.
680 * @return - Nonzero if there was an error during decode, 0 otherwise.
681 */
682int decodeInstruction(struct InternalInstruction* insn,
683 byteReader_t reader,
Roman Divacky67923802012-09-05 21:17:34 +0000684 const void* readerArg,
Sean Callanan04cc3072009-12-19 02:59:52 +0000685 dlog_t logger,
686 void* loggerArg,
Roman Divacky67923802012-09-05 21:17:34 +0000687 const void* miiArg,
Sean Callanan04cc3072009-12-19 02:59:52 +0000688 uint64_t startLoc,
689 DisassemblerMode mode);
690
Richard Smith89ee75d2014-04-20 21:07:34 +0000691/* \brief Debug - Print a message to debugs()
Sean Callanan010b3732010-04-02 21:23:51 +0000692 * @param file - The name of the file printing the debug message.
693 * @param line - The line number that printed the debug message.
694 * @param s - The message to print.
695 */
Craig Topperfb39f972012-07-31 04:58:05 +0000696
Richard Smith89ee75d2014-04-20 21:07:34 +0000697void Debug(const char *file, unsigned line, const char *s);
Sean Callanan010b3732010-04-02 21:23:51 +0000698
Richard Smith89ee75d2014-04-20 21:07:34 +0000699const char *GetInstrName(unsigned Opcode, const void *mii);
Benjamin Kramer478e8de2012-02-11 14:50:54 +0000700
Richard Smith89ee75d2014-04-20 21:07:34 +0000701} // namespace X86Disassembler
702} // namespace llvm
Craig Topperfb39f972012-07-31 04:58:05 +0000703
Sean Callanan04cc3072009-12-19 02:59:52 +0000704#endif