1 //===-- X86Disassembler.cpp - Disassembler for x86 and x86_64 -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is part of the X86 Disassembler.
10 // It contains code to translate the data produced by the decoder into
11 // MCInsts.
12 //
13 //
14 // The X86 disassembler is a table-driven disassembler for the 16-, 32-, and
15 // 64-bit X86 instruction sets. The main decode sequence for an assembly
16 // instruction in this disassembler is:
17 //
18 // 1. Read the prefix bytes and determine the attributes of the instruction.
19 // These attributes, recorded in enum attributeBits
20 // (X86DisassemblerDecoderCommon.h), form a bitmask. The table CONTEXTS_SYM
21 // provides a mapping from bitmasks to contexts, which are represented by
22 // enum InstructionContext (ibid.).
23 //
24 // 2. Read the opcode, and determine what kind of opcode it is. The
25 // disassembler distinguishes four kinds of opcodes, which are enumerated in
26 // OpcodeType (X86DisassemblerDecoderCommon.h): one-byte (0xnn), two-byte
27 // (0x0f 0xnn), three-byte-38 (0x0f 0x38 0xnn), or three-byte-3a
28 // (0x0f 0x3a 0xnn). Mandatory prefixes are treated as part of the context.
29 //
30 // 3. Depending on the opcode type, look in one of four ClassDecision structures
31 // (X86DisassemblerDecoderCommon.h). Use the opcode class to determine which
32 // OpcodeDecision (ibid.) to look the opcode in. Look up the opcode, to get
33 // a ModRMDecision (ibid.).
34 //
35 // 4. Some instructions, such as escape opcodes or extended opcodes, or even
36 // instructions that have ModRM*Reg / ModRM*Mem forms in LLVM, need the
37 // ModR/M byte to complete decode. The ModRMDecision's type is an entry from
38 // ModRMDecisionType (X86DisassemblerDecoderCommon.h) that indicates if the
39 // ModR/M byte is required and how to interpret it.
40 //
41 // 5. After resolving the ModRMDecision, the disassembler has a unique ID
42 // of type InstrUID (X86DisassemblerDecoderCommon.h). Looking this ID up in
43 // INSTRUCTIONS_SYM yields the name of the instruction and the encodings and
44 // meanings of its operands.
45 //
46 // 6. For each operand, its encoding is an entry from OperandEncoding
47 // (X86DisassemblerDecoderCommon.h) and its type is an entry from
48 // OperandType (ibid.). The encoding indicates how to read it from the
49 // instruction; the type indicates how to interpret the value once it has
50 // been read. For example, a register operand could be stored in the R/M
51 // field of the ModR/M byte, the REG field of the ModR/M byte, or added to
52 // the main opcode. This is orthogonal from its meaning (an GPR or an XMM
53 // register, for instance). Given this information, the operands can be
54 // extracted and interpreted.
55 //
56 // 7. As the last step, the disassembler translates the instruction information
57 // and operands into a format understandable by the client - in this case, an
58 // MCInst for use by the MC infrastructure.
59 //
60 // The disassembler is broken broadly into two parts: the table emitter that
61 // emits the instruction decode tables discussed above during compilation, and
62 // the disassembler itself. The table emitter is documented in more detail in
63 // utils/TableGen/X86DisassemblerEmitter.h.
64 //
65 // X86Disassembler.cpp contains the code responsible for step 7, and for
66 // invoking the decoder to execute steps 1-6.
67 // X86DisassemblerDecoderCommon.h contains the definitions needed by both the
68 // table emitter and the disassembler.
69 // X86DisassemblerDecoder.h contains the public interface of the decoder,
70 // factored out into C for possible use by other projects.
71 // X86DisassemblerDecoder.c contains the source code of the decoder, which is
72 // responsible for steps 1-6.
73 //
74 //===----------------------------------------------------------------------===//
75
76 #include "MCTargetDesc/X86BaseInfo.h"
77 #include "MCTargetDesc/X86MCTargetDesc.h"
78 #include "TargetInfo/X86TargetInfo.h"
79 #include "X86DisassemblerDecoder.h"
80 #include "llvm/MC/MCContext.h"
81 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
82 #include "llvm/MC/MCExpr.h"
83 #include "llvm/MC/MCInst.h"
84 #include "llvm/MC/MCInstrInfo.h"
85 #include "llvm/MC/MCSubtargetInfo.h"
86 #include "llvm/Support/Debug.h"
87 #include "llvm/Support/Format.h"
88 #include "llvm/Support/TargetRegistry.h"
89 #include "llvm/Support/raw_ostream.h"
90
91 using namespace llvm;
92 using namespace llvm::X86Disassembler;
93
94 #define DEBUG_TYPE "x86-disassembler"
95
96 #define debug(s) LLVM_DEBUG(dbgs() << __LINE__ << ": " << s);
97
98 // Specifies whether a ModR/M byte is needed and (if so) which
99 // instruction each possible value of the ModR/M byte corresponds to. Once
100 // this information is known, we have narrowed down to a single instruction.
101 struct ModRMDecision {
102 uint8_t modrm_type;
103 uint16_t instructionIDs;
104 };
105
106 // Specifies which set of ModR/M->instruction tables to look at
107 // given a particular opcode.
108 struct OpcodeDecision {
109 ModRMDecision modRMDecisions[256];
110 };
111
112 // Specifies which opcode->instruction tables to look at given
113 // a particular context (set of attributes). Since there are many possible
114 // contexts, the decoder first uses CONTEXTS_SYM to determine which context
115 // applies given a specific set of attributes. Hence there are only IC_max
116 // entries in this table, rather than 2^(ATTR_max).
117 struct ContextDecision {
118 OpcodeDecision opcodeDecisions[IC_max];
119 };
120
121 #include "X86GenDisassemblerTables.inc"
122
decode(OpcodeType type,InstructionContext insnContext,uint8_t opcode,uint8_t modRM)123 static InstrUID decode(OpcodeType type, InstructionContext insnContext,
124 uint8_t opcode, uint8_t modRM) {
125 const struct ModRMDecision *dec;
126
127 switch (type) {
128 case ONEBYTE:
129 dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
130 break;
131 case TWOBYTE:
132 dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
133 break;
134 case THREEBYTE_38:
135 dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
136 break;
137 case THREEBYTE_3A:
138 dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
139 break;
140 case XOP8_MAP:
141 dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
142 break;
143 case XOP9_MAP:
144 dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
145 break;
146 case XOPA_MAP:
147 dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
148 break;
149 case THREEDNOW_MAP:
150 dec =
151 &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
152 break;
153 }
154
155 switch (dec->modrm_type) {
156 default:
157 llvm_unreachable("Corrupt table! Unknown modrm_type");
158 return 0;
159 case MODRM_ONEENTRY:
160 return modRMTable[dec->instructionIDs];
161 case MODRM_SPLITRM:
162 if (modFromModRM(modRM) == 0x3)
163 return modRMTable[dec->instructionIDs + 1];
164 return modRMTable[dec->instructionIDs];
165 case MODRM_SPLITREG:
166 if (modFromModRM(modRM) == 0x3)
167 return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3) + 8];
168 return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
169 case MODRM_SPLITMISC:
170 if (modFromModRM(modRM) == 0x3)
171 return modRMTable[dec->instructionIDs + (modRM & 0x3f) + 8];
172 return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
173 case MODRM_FULL:
174 return modRMTable[dec->instructionIDs + modRM];
175 }
176 }
177
peek(struct InternalInstruction * insn,uint8_t & byte)178 static bool peek(struct InternalInstruction *insn, uint8_t &byte) {
179 uint64_t offset = insn->readerCursor - insn->startLocation;
180 if (offset >= insn->bytes.size())
181 return true;
182 byte = insn->bytes[offset];
183 return false;
184 }
185
consume(InternalInstruction * insn,T & ptr)186 template <typename T> static bool consume(InternalInstruction *insn, T &ptr) {
187 auto r = insn->bytes;
188 uint64_t offset = insn->readerCursor - insn->startLocation;
189 if (offset + sizeof(T) > r.size())
190 return true;
191 T ret = 0;
192 for (unsigned i = 0; i < sizeof(T); ++i)
193 ret |= (uint64_t)r[offset + i] << (i * 8);
194 ptr = ret;
195 insn->readerCursor += sizeof(T);
196 return false;
197 }
198
isREX(struct InternalInstruction * insn,uint8_t prefix)199 static bool isREX(struct InternalInstruction *insn, uint8_t prefix) {
200 return insn->mode == MODE_64BIT && prefix >= 0x40 && prefix <= 0x4f;
201 }
202
203 // Consumes all of an instruction's prefix bytes, and marks the
204 // instruction as having them. Also sets the instruction's default operand,
205 // address, and other relevant data sizes to report operands correctly.
206 //
207 // insn must not be empty.
readPrefixes(struct InternalInstruction * insn)208 static int readPrefixes(struct InternalInstruction *insn) {
209 bool isPrefix = true;
210 uint8_t byte = 0;
211 uint8_t nextByte;
212
213 LLVM_DEBUG(dbgs() << "readPrefixes()");
214
215 while (isPrefix) {
216 // If we fail reading prefixes, just stop here and let the opcode reader
217 // deal with it.
218 if (consume(insn, byte))
219 break;
220
221 // If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
222 // break and let it be disassembled as a normal "instruction".
223 if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0) // LOCK
224 break;
225
226 if ((byte == 0xf2 || byte == 0xf3) && !peek(insn, nextByte)) {
227 // If the byte is 0xf2 or 0xf3, and any of the following conditions are
228 // met:
229 // - it is followed by a LOCK (0xf0) prefix
230 // - it is followed by an xchg instruction
231 // then it should be disassembled as a xacquire/xrelease not repne/rep.
232 if (((nextByte == 0xf0) ||
233 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90))) {
234 insn->xAcquireRelease = true;
235 if (!(byte == 0xf3 && nextByte == 0x90)) // PAUSE instruction support
236 break;
237 }
238 // Also if the byte is 0xf3, and the following condition is met:
239 // - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
240 // "mov mem, imm" (opcode 0xc6/0xc7) instructions.
241 // then it should be disassembled as an xrelease not rep.
242 if (byte == 0xf3 && (nextByte == 0x88 || nextByte == 0x89 ||
243 nextByte == 0xc6 || nextByte == 0xc7)) {
244 insn->xAcquireRelease = true;
245 break;
246 }
247 if (isREX(insn, nextByte)) {
248 uint8_t nnextByte;
249 // Go to REX prefix after the current one
250 if (consume(insn, nnextByte))
251 return -1;
252 // We should be able to read next byte after REX prefix
253 if (peek(insn, nnextByte))
254 return -1;
255 --insn->readerCursor;
256 }
257 }
258
259 switch (byte) {
260 case 0xf0: // LOCK
261 insn->hasLockPrefix = true;
262 break;
263 case 0xf2: // REPNE/REPNZ
264 case 0xf3: { // REP or REPE/REPZ
265 uint8_t nextByte;
266 if (peek(insn, nextByte))
267 break;
268 // TODO:
269 // 1. There could be several 0x66
270 // 2. if (nextByte == 0x66) and nextNextByte != 0x0f then
271 // it's not mandatory prefix
272 // 3. if (nextByte >= 0x40 && nextByte <= 0x4f) it's REX and we need
273 // 0x0f exactly after it to be mandatory prefix
274 if (isREX(insn, nextByte) || nextByte == 0x0f || nextByte == 0x66)
275 // The last of 0xf2 /0xf3 is mandatory prefix
276 insn->mandatoryPrefix = byte;
277 insn->repeatPrefix = byte;
278 break;
279 }
280 case 0x2e: // CS segment override -OR- Branch not taken
281 insn->segmentOverride = SEG_OVERRIDE_CS;
282 break;
283 case 0x36: // SS segment override -OR- Branch taken
284 insn->segmentOverride = SEG_OVERRIDE_SS;
285 break;
286 case 0x3e: // DS segment override
287 insn->segmentOverride = SEG_OVERRIDE_DS;
288 break;
289 case 0x26: // ES segment override
290 insn->segmentOverride = SEG_OVERRIDE_ES;
291 break;
292 case 0x64: // FS segment override
293 insn->segmentOverride = SEG_OVERRIDE_FS;
294 break;
295 case 0x65: // GS segment override
296 insn->segmentOverride = SEG_OVERRIDE_GS;
297 break;
298 case 0x66: { // Operand-size override {
299 uint8_t nextByte;
300 insn->hasOpSize = true;
301 if (peek(insn, nextByte))
302 break;
303 // 0x66 can't overwrite existing mandatory prefix and should be ignored
304 if (!insn->mandatoryPrefix && (nextByte == 0x0f || isREX(insn, nextByte)))
305 insn->mandatoryPrefix = byte;
306 break;
307 }
308 case 0x67: // Address-size override
309 insn->hasAdSize = true;
310 break;
311 default: // Not a prefix byte
312 isPrefix = false;
313 break;
314 }
315
316 if (isPrefix)
317 LLVM_DEBUG(dbgs() << format("Found prefix 0x%hhx", byte));
318 }
319
320 insn->vectorExtensionType = TYPE_NO_VEX_XOP;
321
322 if (byte == 0x62) {
323 uint8_t byte1, byte2;
324 if (consume(insn, byte1)) {
325 LLVM_DEBUG(dbgs() << "Couldn't read second byte of EVEX prefix");
326 return -1;
327 }
328
329 if (peek(insn, byte2)) {
330 LLVM_DEBUG(dbgs() << "Couldn't read third byte of EVEX prefix");
331 return -1;
332 }
333
334 if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
335 ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
336 insn->vectorExtensionType = TYPE_EVEX;
337 } else {
338 --insn->readerCursor; // unconsume byte1
339 --insn->readerCursor; // unconsume byte
340 }
341
342 if (insn->vectorExtensionType == TYPE_EVEX) {
343 insn->vectorExtensionPrefix[0] = byte;
344 insn->vectorExtensionPrefix[1] = byte1;
345 if (consume(insn, insn->vectorExtensionPrefix[2])) {
346 LLVM_DEBUG(dbgs() << "Couldn't read third byte of EVEX prefix");
347 return -1;
348 }
349 if (consume(insn, insn->vectorExtensionPrefix[3])) {
350 LLVM_DEBUG(dbgs() << "Couldn't read fourth byte of EVEX prefix");
351 return -1;
352 }
353
354 // We simulate the REX prefix for simplicity's sake
355 if (insn->mode == MODE_64BIT) {
356 insn->rexPrefix = 0x40 |
357 (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3) |
358 (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2) |
359 (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1) |
360 (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
361 }
362
363 LLVM_DEBUG(
364 dbgs() << format(
365 "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
366 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
367 insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]));
368 }
369 } else if (byte == 0xc4) {
370 uint8_t byte1;
371 if (peek(insn, byte1)) {
372 LLVM_DEBUG(dbgs() << "Couldn't read second byte of VEX");
373 return -1;
374 }
375
376 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
377 insn->vectorExtensionType = TYPE_VEX_3B;
378 else
379 --insn->readerCursor;
380
381 if (insn->vectorExtensionType == TYPE_VEX_3B) {
382 insn->vectorExtensionPrefix[0] = byte;
383 consume(insn, insn->vectorExtensionPrefix[1]);
384 consume(insn, insn->vectorExtensionPrefix[2]);
385
386 // We simulate the REX prefix for simplicity's sake
387
388 if (insn->mode == MODE_64BIT)
389 insn->rexPrefix = 0x40 |
390 (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3) |
391 (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2) |
392 (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1) |
393 (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
394
395 LLVM_DEBUG(dbgs() << format("Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
396 insn->vectorExtensionPrefix[0],
397 insn->vectorExtensionPrefix[1],
398 insn->vectorExtensionPrefix[2]));
399 }
400 } else if (byte == 0xc5) {
401 uint8_t byte1;
402 if (peek(insn, byte1)) {
403 LLVM_DEBUG(dbgs() << "Couldn't read second byte of VEX");
404 return -1;
405 }
406
407 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
408 insn->vectorExtensionType = TYPE_VEX_2B;
409 else
410 --insn->readerCursor;
411
412 if (insn->vectorExtensionType == TYPE_VEX_2B) {
413 insn->vectorExtensionPrefix[0] = byte;
414 consume(insn, insn->vectorExtensionPrefix[1]);
415
416 if (insn->mode == MODE_64BIT)
417 insn->rexPrefix =
418 0x40 | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
419
420 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
421 default:
422 break;
423 case VEX_PREFIX_66:
424 insn->hasOpSize = true;
425 break;
426 }
427
428 LLVM_DEBUG(dbgs() << format("Found VEX prefix 0x%hhx 0x%hhx",
429 insn->vectorExtensionPrefix[0],
430 insn->vectorExtensionPrefix[1]));
431 }
432 } else if (byte == 0x8f) {
433 uint8_t byte1;
434 if (peek(insn, byte1)) {
435 LLVM_DEBUG(dbgs() << "Couldn't read second byte of XOP");
436 return -1;
437 }
438
439 if ((byte1 & 0x38) != 0x0) // 0 in these 3 bits is a POP instruction.
440 insn->vectorExtensionType = TYPE_XOP;
441 else
442 --insn->readerCursor;
443
444 if (insn->vectorExtensionType == TYPE_XOP) {
445 insn->vectorExtensionPrefix[0] = byte;
446 consume(insn, insn->vectorExtensionPrefix[1]);
447 consume(insn, insn->vectorExtensionPrefix[2]);
448
449 // We simulate the REX prefix for simplicity's sake
450
451 if (insn->mode == MODE_64BIT)
452 insn->rexPrefix = 0x40 |
453 (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3) |
454 (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2) |
455 (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1) |
456 (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
457
458 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
459 default:
460 break;
461 case VEX_PREFIX_66:
462 insn->hasOpSize = true;
463 break;
464 }
465
466 LLVM_DEBUG(dbgs() << format("Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
467 insn->vectorExtensionPrefix[0],
468 insn->vectorExtensionPrefix[1],
469 insn->vectorExtensionPrefix[2]));
470 }
471 } else if (isREX(insn, byte)) {
472 if (peek(insn, nextByte))
473 return -1;
474 insn->rexPrefix = byte;
475 LLVM_DEBUG(dbgs() << format("Found REX prefix 0x%hhx", byte));
476 } else
477 --insn->readerCursor;
478
479 if (insn->mode == MODE_16BIT) {
480 insn->registerSize = (insn->hasOpSize ? 4 : 2);
481 insn->addressSize = (insn->hasAdSize ? 4 : 2);
482 insn->displacementSize = (insn->hasAdSize ? 4 : 2);
483 insn->immediateSize = (insn->hasOpSize ? 4 : 2);
484 } else if (insn->mode == MODE_32BIT) {
485 insn->registerSize = (insn->hasOpSize ? 2 : 4);
486 insn->addressSize = (insn->hasAdSize ? 2 : 4);
487 insn->displacementSize = (insn->hasAdSize ? 2 : 4);
488 insn->immediateSize = (insn->hasOpSize ? 2 : 4);
489 } else if (insn->mode == MODE_64BIT) {
490 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
491 insn->registerSize = 8;
492 insn->addressSize = (insn->hasAdSize ? 4 : 8);
493 insn->displacementSize = 4;
494 insn->immediateSize = 4;
495 insn->hasOpSize = false;
496 } else {
497 insn->registerSize = (insn->hasOpSize ? 2 : 4);
498 insn->addressSize = (insn->hasAdSize ? 4 : 8);
499 insn->displacementSize = (insn->hasOpSize ? 2 : 4);
500 insn->immediateSize = (insn->hasOpSize ? 2 : 4);
501 }
502 }
503
504 return 0;
505 }
506
507 // Consumes the SIB byte to determine addressing information.
readSIB(struct InternalInstruction * insn)508 static int readSIB(struct InternalInstruction *insn) {
509 SIBBase sibBaseBase = SIB_BASE_NONE;
510 uint8_t index, base;
511
512 LLVM_DEBUG(dbgs() << "readSIB()");
513 switch (insn->addressSize) {
514 case 2:
515 default:
516 llvm_unreachable("SIB-based addressing doesn't work in 16-bit mode");
517 case 4:
518 insn->sibIndexBase = SIB_INDEX_EAX;
519 sibBaseBase = SIB_BASE_EAX;
520 break;
521 case 8:
522 insn->sibIndexBase = SIB_INDEX_RAX;
523 sibBaseBase = SIB_BASE_RAX;
524 break;
525 }
526
527 if (consume(insn, insn->sib))
528 return -1;
529
530 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
531
532 if (index == 0x4) {
533 insn->sibIndex = SIB_INDEX_NONE;
534 } else {
535 insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
536 }
537
538 insn->sibScale = 1 << scaleFromSIB(insn->sib);
539
540 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
541
542 switch (base) {
543 case 0x5:
544 case 0xd:
545 switch (modFromModRM(insn->modRM)) {
546 case 0x0:
547 insn->eaDisplacement = EA_DISP_32;
548 insn->sibBase = SIB_BASE_NONE;
549 break;
550 case 0x1:
551 insn->eaDisplacement = EA_DISP_8;
552 insn->sibBase = (SIBBase)(sibBaseBase + base);
553 break;
554 case 0x2:
555 insn->eaDisplacement = EA_DISP_32;
556 insn->sibBase = (SIBBase)(sibBaseBase + base);
557 break;
558 default:
559 llvm_unreachable("Cannot have Mod = 0b11 and a SIB byte");
560 }
561 break;
562 default:
563 insn->sibBase = (SIBBase)(sibBaseBase + base);
564 break;
565 }
566
567 return 0;
568 }
569
readDisplacement(struct InternalInstruction * insn)570 static int readDisplacement(struct InternalInstruction *insn) {
571 int8_t d8;
572 int16_t d16;
573 int32_t d32;
574 LLVM_DEBUG(dbgs() << "readDisplacement()");
575
576 insn->displacementOffset = insn->readerCursor - insn->startLocation;
577 switch (insn->eaDisplacement) {
578 case EA_DISP_NONE:
579 break;
580 case EA_DISP_8:
581 if (consume(insn, d8))
582 return -1;
583 insn->displacement = d8;
584 break;
585 case EA_DISP_16:
586 if (consume(insn, d16))
587 return -1;
588 insn->displacement = d16;
589 break;
590 case EA_DISP_32:
591 if (consume(insn, d32))
592 return -1;
593 insn->displacement = d32;
594 break;
595 }
596
597 return 0;
598 }
599
600 // Consumes all addressing information (ModR/M byte, SIB byte, and displacement.
readModRM(struct InternalInstruction * insn)601 static int readModRM(struct InternalInstruction *insn) {
602 uint8_t mod, rm, reg, evexrm;
603 LLVM_DEBUG(dbgs() << "readModRM()");
604
605 if (insn->consumedModRM)
606 return 0;
607
608 if (consume(insn, insn->modRM))
609 return -1;
610 insn->consumedModRM = true;
611
612 mod = modFromModRM(insn->modRM);
613 rm = rmFromModRM(insn->modRM);
614 reg = regFromModRM(insn->modRM);
615
616 // This goes by insn->registerSize to pick the correct register, which messes
617 // up if we're using (say) XMM or 8-bit register operands. That gets fixed in
618 // fixupReg().
619 switch (insn->registerSize) {
620 case 2:
621 insn->regBase = MODRM_REG_AX;
622 insn->eaRegBase = EA_REG_AX;
623 break;
624 case 4:
625 insn->regBase = MODRM_REG_EAX;
626 insn->eaRegBase = EA_REG_EAX;
627 break;
628 case 8:
629 insn->regBase = MODRM_REG_RAX;
630 insn->eaRegBase = EA_REG_RAX;
631 break;
632 }
633
634 reg |= rFromREX(insn->rexPrefix) << 3;
635 rm |= bFromREX(insn->rexPrefix) << 3;
636
637 evexrm = 0;
638 if (insn->vectorExtensionType == TYPE_EVEX && insn->mode == MODE_64BIT) {
639 reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
640 evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
641 }
642
643 insn->reg = (Reg)(insn->regBase + reg);
644
645 switch (insn->addressSize) {
646 case 2: {
647 EABase eaBaseBase = EA_BASE_BX_SI;
648
649 switch (mod) {
650 case 0x0:
651 if (rm == 0x6) {
652 insn->eaBase = EA_BASE_NONE;
653 insn->eaDisplacement = EA_DISP_16;
654 if (readDisplacement(insn))
655 return -1;
656 } else {
657 insn->eaBase = (EABase)(eaBaseBase + rm);
658 insn->eaDisplacement = EA_DISP_NONE;
659 }
660 break;
661 case 0x1:
662 insn->eaBase = (EABase)(eaBaseBase + rm);
663 insn->eaDisplacement = EA_DISP_8;
664 insn->displacementSize = 1;
665 if (readDisplacement(insn))
666 return -1;
667 break;
668 case 0x2:
669 insn->eaBase = (EABase)(eaBaseBase + rm);
670 insn->eaDisplacement = EA_DISP_16;
671 if (readDisplacement(insn))
672 return -1;
673 break;
674 case 0x3:
675 insn->eaBase = (EABase)(insn->eaRegBase + rm);
676 if (readDisplacement(insn))
677 return -1;
678 break;
679 }
680 break;
681 }
682 case 4:
683 case 8: {
684 EABase eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
685
686 switch (mod) {
687 case 0x0:
688 insn->eaDisplacement = EA_DISP_NONE; // readSIB may override this
689 // In determining whether RIP-relative mode is used (rm=5),
690 // or whether a SIB byte is present (rm=4),
691 // the extension bits (REX.b and EVEX.x) are ignored.
692 switch (rm & 7) {
693 case 0x4: // SIB byte is present
694 insn->eaBase = (insn->addressSize == 4 ? EA_BASE_sib : EA_BASE_sib64);
695 if (readSIB(insn) || readDisplacement(insn))
696 return -1;
697 break;
698 case 0x5: // RIP-relative
699 insn->eaBase = EA_BASE_NONE;
700 insn->eaDisplacement = EA_DISP_32;
701 if (readDisplacement(insn))
702 return -1;
703 break;
704 default:
705 insn->eaBase = (EABase)(eaBaseBase + rm);
706 break;
707 }
708 break;
709 case 0x1:
710 insn->displacementSize = 1;
711 LLVM_FALLTHROUGH;
712 case 0x2:
713 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
714 switch (rm & 7) {
715 case 0x4: // SIB byte is present
716 insn->eaBase = EA_BASE_sib;
717 if (readSIB(insn) || readDisplacement(insn))
718 return -1;
719 break;
720 default:
721 insn->eaBase = (EABase)(eaBaseBase + rm);
722 if (readDisplacement(insn))
723 return -1;
724 break;
725 }
726 break;
727 case 0x3:
728 insn->eaDisplacement = EA_DISP_NONE;
729 insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
730 break;
731 }
732 break;
733 }
734 } // switch (insn->addressSize)
735
736 return 0;
737 }
738
739 #define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
740 static uint16_t name(struct InternalInstruction *insn, OperandType type, \
741 uint8_t index, uint8_t *valid) { \
742 *valid = 1; \
743 switch (type) { \
744 default: \
745 debug("Unhandled register type"); \
746 *valid = 0; \
747 return 0; \
748 case TYPE_Rv: \
749 return base + index; \
750 case TYPE_R8: \
751 index &= mask; \
752 if (index > 0xf) \
753 *valid = 0; \
754 if (insn->rexPrefix && index >= 4 && index <= 7) { \
755 return prefix##_SPL + (index - 4); \
756 } else { \
757 return prefix##_AL + index; \
758 } \
759 case TYPE_R16: \
760 index &= mask; \
761 if (index > 0xf) \
762 *valid = 0; \
763 return prefix##_AX + index; \
764 case TYPE_R32: \
765 index &= mask; \
766 if (index > 0xf) \
767 *valid = 0; \
768 return prefix##_EAX + index; \
769 case TYPE_R64: \
770 index &= mask; \
771 if (index > 0xf) \
772 *valid = 0; \
773 return prefix##_RAX + index; \
774 case TYPE_ZMM: \
775 return prefix##_ZMM0 + index; \
776 case TYPE_YMM: \
777 return prefix##_YMM0 + index; \
778 case TYPE_XMM: \
779 return prefix##_XMM0 + index; \
780 case TYPE_TMM: \
781 if (index > 7) \
782 *valid = 0; \
783 return prefix##_TMM0 + index; \
784 case TYPE_VK: \
785 index &= 0xf; \
786 if (index > 7) \
787 *valid = 0; \
788 return prefix##_K0 + index; \
789 case TYPE_VK_PAIR: \
790 if (index > 7) \
791 *valid = 0; \
792 return prefix##_K0_K1 + (index / 2); \
793 case TYPE_MM64: \
794 return prefix##_MM0 + (index & 0x7); \
795 case TYPE_SEGMENTREG: \
796 if ((index & 7) > 5) \
797 *valid = 0; \
798 return prefix##_ES + (index & 7); \
799 case TYPE_DEBUGREG: \
800 return prefix##_DR0 + index; \
801 case TYPE_CONTROLREG: \
802 return prefix##_CR0 + index; \
803 case TYPE_BNDR: \
804 if (index > 3) \
805 *valid = 0; \
806 return prefix##_BND0 + index; \
807 case TYPE_MVSIBX: \
808 return prefix##_XMM0 + index; \
809 case TYPE_MVSIBY: \
810 return prefix##_YMM0 + index; \
811 case TYPE_MVSIBZ: \
812 return prefix##_ZMM0 + index; \
813 } \
814 }
815
816 // Consult an operand type to determine the meaning of the reg or R/M field. If
817 // the operand is an XMM operand, for example, an operand would be XMM0 instead
818 // of AX, which readModRM() would otherwise misinterpret it as.
819 //
820 // @param insn - The instruction containing the operand.
821 // @param type - The operand type.
822 // @param index - The existing value of the field as reported by readModRM().
823 // @param valid - The address of a uint8_t. The target is set to 1 if the
824 // field is valid for the register class; 0 if not.
825 // @return - The proper value.
826 GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
827 GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
828
829 // Consult an operand specifier to determine which of the fixup*Value functions
830 // to use in correcting readModRM()'ss interpretation.
831 //
832 // @param insn - See fixup*Value().
833 // @param op - The operand specifier.
834 // @return - 0 if fixup was successful; -1 if the register returned was
835 // invalid for its class.
fixupReg(struct InternalInstruction * insn,const struct OperandSpecifier * op)836 static int fixupReg(struct InternalInstruction *insn,
837 const struct OperandSpecifier *op) {
838 uint8_t valid;
839 LLVM_DEBUG(dbgs() << "fixupReg()");
840
841 switch ((OperandEncoding)op->encoding) {
842 default:
843 debug("Expected a REG or R/M encoding in fixupReg");
844 return -1;
845 case ENCODING_VVVV:
846 insn->vvvv =
847 (Reg)fixupRegValue(insn, (OperandType)op->type, insn->vvvv, &valid);
848 if (!valid)
849 return -1;
850 break;
851 case ENCODING_REG:
852 insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
853 insn->reg - insn->regBase, &valid);
854 if (!valid)
855 return -1;
856 break;
857 case ENCODING_SIB:
858 CASE_ENCODING_RM:
859 if (insn->eaBase >= insn->eaRegBase) {
860 insn->eaBase = (EABase)fixupRMValue(
861 insn, (OperandType)op->type, insn->eaBase - insn->eaRegBase, &valid);
862 if (!valid)
863 return -1;
864 }
865 break;
866 }
867
868 return 0;
869 }
870
871 // Read the opcode (except the ModR/M byte in the case of extended or escape
872 // opcodes).
readOpcode(struct InternalInstruction * insn)873 static bool readOpcode(struct InternalInstruction *insn) {
874 uint8_t current;
875 LLVM_DEBUG(dbgs() << "readOpcode()");
876
877 insn->opcodeType = ONEBYTE;
878 if (insn->vectorExtensionType == TYPE_EVEX) {
879 switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
880 default:
881 LLVM_DEBUG(
882 dbgs() << format("Unhandled mm field for instruction (0x%hhx)",
883 mmFromEVEX2of4(insn->vectorExtensionPrefix[1])));
884 return true;
885 case VEX_LOB_0F:
886 insn->opcodeType = TWOBYTE;
887 return consume(insn, insn->opcode);
888 case VEX_LOB_0F38:
889 insn->opcodeType = THREEBYTE_38;
890 return consume(insn, insn->opcode);
891 case VEX_LOB_0F3A:
892 insn->opcodeType = THREEBYTE_3A;
893 return consume(insn, insn->opcode);
894 }
895 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
896 switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
897 default:
898 LLVM_DEBUG(
899 dbgs() << format("Unhandled m-mmmm field for instruction (0x%hhx)",
900 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])));
901 return true;
902 case VEX_LOB_0F:
903 insn->opcodeType = TWOBYTE;
904 return consume(insn, insn->opcode);
905 case VEX_LOB_0F38:
906 insn->opcodeType = THREEBYTE_38;
907 return consume(insn, insn->opcode);
908 case VEX_LOB_0F3A:
909 insn->opcodeType = THREEBYTE_3A;
910 return consume(insn, insn->opcode);
911 }
912 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
913 insn->opcodeType = TWOBYTE;
914 return consume(insn, insn->opcode);
915 } else if (insn->vectorExtensionType == TYPE_XOP) {
916 switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
917 default:
918 LLVM_DEBUG(
919 dbgs() << format("Unhandled m-mmmm field for instruction (0x%hhx)",
920 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])));
921 return true;
922 case XOP_MAP_SELECT_8:
923 insn->opcodeType = XOP8_MAP;
924 return consume(insn, insn->opcode);
925 case XOP_MAP_SELECT_9:
926 insn->opcodeType = XOP9_MAP;
927 return consume(insn, insn->opcode);
928 case XOP_MAP_SELECT_A:
929 insn->opcodeType = XOPA_MAP;
930 return consume(insn, insn->opcode);
931 }
932 }
933
934 if (consume(insn, current))
935 return true;
936
937 if (current == 0x0f) {
938 LLVM_DEBUG(
939 dbgs() << format("Found a two-byte escape prefix (0x%hhx)", current));
940 if (consume(insn, current))
941 return true;
942
943 if (current == 0x38) {
944 LLVM_DEBUG(dbgs() << format("Found a three-byte escape prefix (0x%hhx)",
945 current));
946 if (consume(insn, current))
947 return true;
948
949 insn->opcodeType = THREEBYTE_38;
950 } else if (current == 0x3a) {
951 LLVM_DEBUG(dbgs() << format("Found a three-byte escape prefix (0x%hhx)",
952 current));
953 if (consume(insn, current))
954 return true;
955
956 insn->opcodeType = THREEBYTE_3A;
957 } else if (current == 0x0f) {
958 LLVM_DEBUG(
959 dbgs() << format("Found a 3dnow escape prefix (0x%hhx)", current));
960
961 // Consume operands before the opcode to comply with the 3DNow encoding
962 if (readModRM(insn))
963 return true;
964
965 if (consume(insn, current))
966 return true;
967
968 insn->opcodeType = THREEDNOW_MAP;
969 } else {
970 LLVM_DEBUG(dbgs() << "Didn't find a three-byte escape prefix");
971 insn->opcodeType = TWOBYTE;
972 }
973 } else if (insn->mandatoryPrefix)
974 // The opcode with mandatory prefix must start with opcode escape.
975 // If not it's legacy repeat prefix
976 insn->mandatoryPrefix = 0;
977
978 // At this point we have consumed the full opcode.
979 // Anything we consume from here on must be unconsumed.
980 insn->opcode = current;
981
982 return false;
983 }
984
985 // Determine whether equiv is the 16-bit equivalent of orig (32-bit or 64-bit).
is16BitEquivalent(const char * orig,const char * equiv)986 static bool is16BitEquivalent(const char *orig, const char *equiv) {
987 for (int i = 0;; i++) {
988 if (orig[i] == '\0' && equiv[i] == '\0')
989 return true;
990 if (orig[i] == '\0' || equiv[i] == '\0')
991 return false;
992 if (orig[i] != equiv[i]) {
993 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
994 continue;
995 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
996 continue;
997 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
998 continue;
999 return false;
1000 }
1001 }
1002 }
1003
1004 // Determine whether this instruction is a 64-bit instruction.
is64Bit(const char * name)1005 static bool is64Bit(const char *name) {
1006 for (int i = 0;; ++i) {
1007 if (name[i] == '\0')
1008 return false;
1009 if (name[i] == '6' && name[i + 1] == '4')
1010 return true;
1011 }
1012 }
1013
1014 // Determine the ID of an instruction, consuming the ModR/M byte as appropriate
1015 // for extended and escape opcodes, and using a supplied attribute mask.
getInstructionIDWithAttrMask(uint16_t * instructionID,struct InternalInstruction * insn,uint16_t attrMask)1016 static int getInstructionIDWithAttrMask(uint16_t *instructionID,
1017 struct InternalInstruction *insn,
1018 uint16_t attrMask) {
1019 auto insnCtx = InstructionContext(x86DisassemblerContexts[attrMask]);
1020 const ContextDecision *decision;
1021 switch (insn->opcodeType) {
1022 case ONEBYTE:
1023 decision = &ONEBYTE_SYM;
1024 break;
1025 case TWOBYTE:
1026 decision = &TWOBYTE_SYM;
1027 break;
1028 case THREEBYTE_38:
1029 decision = &THREEBYTE38_SYM;
1030 break;
1031 case THREEBYTE_3A:
1032 decision = &THREEBYTE3A_SYM;
1033 break;
1034 case XOP8_MAP:
1035 decision = &XOP8_MAP_SYM;
1036 break;
1037 case XOP9_MAP:
1038 decision = &XOP9_MAP_SYM;
1039 break;
1040 case XOPA_MAP:
1041 decision = &XOPA_MAP_SYM;
1042 break;
1043 case THREEDNOW_MAP:
1044 decision = &THREEDNOW_MAP_SYM;
1045 break;
1046 }
1047
1048 if (decision->opcodeDecisions[insnCtx]
1049 .modRMDecisions[insn->opcode]
1050 .modrm_type != MODRM_ONEENTRY) {
1051 if (readModRM(insn))
1052 return -1;
1053 *instructionID =
1054 decode(insn->opcodeType, insnCtx, insn->opcode, insn->modRM);
1055 } else {
1056 *instructionID = decode(insn->opcodeType, insnCtx, insn->opcode, 0);
1057 }
1058
1059 return 0;
1060 }
1061
1062 // Determine the ID of an instruction, consuming the ModR/M byte as appropriate
1063 // for extended and escape opcodes. Determines the attributes and context for
1064 // the instruction before doing so.
getInstructionID(struct InternalInstruction * insn,const MCInstrInfo * mii)1065 static int getInstructionID(struct InternalInstruction *insn,
1066 const MCInstrInfo *mii) {
1067 uint16_t attrMask;
1068 uint16_t instructionID;
1069
1070 LLVM_DEBUG(dbgs() << "getID()");
1071
1072 attrMask = ATTR_NONE;
1073
1074 if (insn->mode == MODE_64BIT)
1075 attrMask |= ATTR_64BIT;
1076
1077 if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1078 attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX;
1079
1080 if (insn->vectorExtensionType == TYPE_EVEX) {
1081 switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) {
1082 case VEX_PREFIX_66:
1083 attrMask |= ATTR_OPSIZE;
1084 break;
1085 case VEX_PREFIX_F3:
1086 attrMask |= ATTR_XS;
1087 break;
1088 case VEX_PREFIX_F2:
1089 attrMask |= ATTR_XD;
1090 break;
1091 }
1092
1093 if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1094 attrMask |= ATTR_EVEXKZ;
1095 if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1096 attrMask |= ATTR_EVEXB;
1097 if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1098 attrMask |= ATTR_EVEXK;
1099 if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1100 attrMask |= ATTR_VEXL;
1101 if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1102 attrMask |= ATTR_EVEXL2;
1103 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1104 switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1105 case VEX_PREFIX_66:
1106 attrMask |= ATTR_OPSIZE;
1107 break;
1108 case VEX_PREFIX_F3:
1109 attrMask |= ATTR_XS;
1110 break;
1111 case VEX_PREFIX_F2:
1112 attrMask |= ATTR_XD;
1113 break;
1114 }
1115
1116 if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1117 attrMask |= ATTR_VEXL;
1118 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1119 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1120 case VEX_PREFIX_66:
1121 attrMask |= ATTR_OPSIZE;
1122 if (insn->hasAdSize)
1123 attrMask |= ATTR_ADSIZE;
1124 break;
1125 case VEX_PREFIX_F3:
1126 attrMask |= ATTR_XS;
1127 break;
1128 case VEX_PREFIX_F2:
1129 attrMask |= ATTR_XD;
1130 break;
1131 }
1132
1133 if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1134 attrMask |= ATTR_VEXL;
1135 } else if (insn->vectorExtensionType == TYPE_XOP) {
1136 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1137 case VEX_PREFIX_66:
1138 attrMask |= ATTR_OPSIZE;
1139 break;
1140 case VEX_PREFIX_F3:
1141 attrMask |= ATTR_XS;
1142 break;
1143 case VEX_PREFIX_F2:
1144 attrMask |= ATTR_XD;
1145 break;
1146 }
1147
1148 if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1149 attrMask |= ATTR_VEXL;
1150 } else {
1151 return -1;
1152 }
1153 } else if (!insn->mandatoryPrefix) {
1154 // If we don't have mandatory prefix we should use legacy prefixes here
1155 if (insn->hasOpSize && (insn->mode != MODE_16BIT))
1156 attrMask |= ATTR_OPSIZE;
1157 if (insn->hasAdSize)
1158 attrMask |= ATTR_ADSIZE;
1159 if (insn->opcodeType == ONEBYTE) {
1160 if (insn->repeatPrefix == 0xf3 && (insn->opcode == 0x90))
1161 // Special support for PAUSE
1162 attrMask |= ATTR_XS;
1163 } else {
1164 if (insn->repeatPrefix == 0xf2)
1165 attrMask |= ATTR_XD;
1166 else if (insn->repeatPrefix == 0xf3)
1167 attrMask |= ATTR_XS;
1168 }
1169 } else {
1170 switch (insn->mandatoryPrefix) {
1171 case 0xf2:
1172 attrMask |= ATTR_XD;
1173 break;
1174 case 0xf3:
1175 attrMask |= ATTR_XS;
1176 break;
1177 case 0x66:
1178 if (insn->mode != MODE_16BIT)
1179 attrMask |= ATTR_OPSIZE;
1180 if (insn->hasAdSize)
1181 attrMask |= ATTR_ADSIZE;
1182 break;
1183 case 0x67:
1184 attrMask |= ATTR_ADSIZE;
1185 break;
1186 }
1187 }
1188
1189 if (insn->rexPrefix & 0x08) {
1190 attrMask |= ATTR_REXW;
1191 attrMask &= ~ATTR_ADSIZE;
1192 }
1193
1194 if (insn->mode == MODE_16BIT) {
1195 // JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1196 // of the AdSize prefix is inverted w.r.t. 32-bit mode.
1197 if (insn->opcodeType == ONEBYTE && insn->opcode == 0xE3)
1198 attrMask ^= ATTR_ADSIZE;
1199 // If we're in 16-bit mode and this is one of the relative jumps and opsize
1200 // prefix isn't present, we need to force the opsize attribute since the
1201 // prefix is inverted relative to 32-bit mode.
1202 if (!insn->hasOpSize && insn->opcodeType == ONEBYTE &&
1203 (insn->opcode == 0xE8 || insn->opcode == 0xE9))
1204 attrMask |= ATTR_OPSIZE;
1205
1206 if (!insn->hasOpSize && insn->opcodeType == TWOBYTE &&
1207 insn->opcode >= 0x80 && insn->opcode <= 0x8F)
1208 attrMask |= ATTR_OPSIZE;
1209 }
1210
1211
1212 if (getInstructionIDWithAttrMask(&instructionID, insn, attrMask))
1213 return -1;
1214
1215 // The following clauses compensate for limitations of the tables.
1216
1217 if (insn->mode != MODE_64BIT &&
1218 insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1219 // The tables can't distinquish between cases where the W-bit is used to
1220 // select register size and cases where its a required part of the opcode.
1221 if ((insn->vectorExtensionType == TYPE_EVEX &&
1222 wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1223 (insn->vectorExtensionType == TYPE_VEX_3B &&
1224 wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1225 (insn->vectorExtensionType == TYPE_XOP &&
1226 wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1227
1228 uint16_t instructionIDWithREXW;
1229 if (getInstructionIDWithAttrMask(&instructionIDWithREXW, insn,
1230 attrMask | ATTR_REXW)) {
1231 insn->instructionID = instructionID;
1232 insn->spec = &INSTRUCTIONS_SYM[instructionID];
1233 return 0;
1234 }
1235
1236 auto SpecName = mii->getName(instructionIDWithREXW);
1237 // If not a 64-bit instruction. Switch the opcode.
1238 if (!is64Bit(SpecName.data())) {
1239 insn->instructionID = instructionIDWithREXW;
1240 insn->spec = &INSTRUCTIONS_SYM[instructionIDWithREXW];
1241 return 0;
1242 }
1243 }
1244 }
1245
1246 // Absolute moves, umonitor, and movdir64b need special handling.
1247 // -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1248 // inverted w.r.t.
1249 // -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1250 // any position.
1251 if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1252 (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1253 (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1254 // Make sure we observed the prefixes in any position.
1255 if (insn->hasAdSize)
1256 attrMask |= ATTR_ADSIZE;
1257 if (insn->hasOpSize)
1258 attrMask |= ATTR_OPSIZE;
1259
1260 // In 16-bit, invert the attributes.
1261 if (insn->mode == MODE_16BIT) {
1262 attrMask ^= ATTR_ADSIZE;
1263
1264 // The OpSize attribute is only valid with the absolute moves.
1265 if (insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0))
1266 attrMask ^= ATTR_OPSIZE;
1267 }
1268
1269 if (getInstructionIDWithAttrMask(&instructionID, insn, attrMask))
1270 return -1;
1271
1272 insn->instructionID = instructionID;
1273 insn->spec = &INSTRUCTIONS_SYM[instructionID];
1274 return 0;
1275 }
1276
1277 if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
1278 !(attrMask & ATTR_OPSIZE)) {
1279 // The instruction tables make no distinction between instructions that
1280 // allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1281 // particular spot (i.e., many MMX operations). In general we're
1282 // conservative, but in the specific case where OpSize is present but not in
1283 // the right place we check if there's a 16-bit operation.
1284 const struct InstructionSpecifier *spec;
1285 uint16_t instructionIDWithOpsize;
1286 llvm::StringRef specName, specWithOpSizeName;
1287
1288 spec = &INSTRUCTIONS_SYM[instructionID];
1289
1290 if (getInstructionIDWithAttrMask(&instructionIDWithOpsize, insn,
1291 attrMask | ATTR_OPSIZE)) {
1292 // ModRM required with OpSize but not present. Give up and return the
1293 // version without OpSize set.
1294 insn->instructionID = instructionID;
1295 insn->spec = spec;
1296 return 0;
1297 }
1298
1299 specName = mii->getName(instructionID);
1300 specWithOpSizeName = mii->getName(instructionIDWithOpsize);
1301
1302 if (is16BitEquivalent(specName.data(), specWithOpSizeName.data()) &&
1303 (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1304 insn->instructionID = instructionIDWithOpsize;
1305 insn->spec = &INSTRUCTIONS_SYM[instructionIDWithOpsize];
1306 } else {
1307 insn->instructionID = instructionID;
1308 insn->spec = spec;
1309 }
1310 return 0;
1311 }
1312
1313 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1314 insn->rexPrefix & 0x01) {
1315 // NOOP shouldn't decode as NOOP if REX.b is set. Instead it should decode
1316 // as XCHG %r8, %eax.
1317 const struct InstructionSpecifier *spec;
1318 uint16_t instructionIDWithNewOpcode;
1319 const struct InstructionSpecifier *specWithNewOpcode;
1320
1321 spec = &INSTRUCTIONS_SYM[instructionID];
1322
1323 // Borrow opcode from one of the other XCHGar opcodes
1324 insn->opcode = 0x91;
1325
1326 if (getInstructionIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1327 attrMask)) {
1328 insn->opcode = 0x90;
1329
1330 insn->instructionID = instructionID;
1331 insn->spec = spec;
1332 return 0;
1333 }
1334
1335 specWithNewOpcode = &INSTRUCTIONS_SYM[instructionIDWithNewOpcode];
1336
1337 // Change back
1338 insn->opcode = 0x90;
1339
1340 insn->instructionID = instructionIDWithNewOpcode;
1341 insn->spec = specWithNewOpcode;
1342
1343 return 0;
1344 }
1345
1346 insn->instructionID = instructionID;
1347 insn->spec = &INSTRUCTIONS_SYM[insn->instructionID];
1348
1349 return 0;
1350 }
1351
1352 // Read an operand from the opcode field of an instruction and interprets it
1353 // appropriately given the operand width. Handles AddRegFrm instructions.
1354 //
1355 // @param insn - the instruction whose opcode field is to be read.
1356 // @param size - The width (in bytes) of the register being specified.
1357 // 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1358 // RAX.
1359 // @return - 0 on success; nonzero otherwise.
readOpcodeRegister(struct InternalInstruction * insn,uint8_t size)1360 static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size) {
1361 LLVM_DEBUG(dbgs() << "readOpcodeRegister()");
1362
1363 if (size == 0)
1364 size = insn->registerSize;
1365
1366 switch (size) {
1367 case 1:
1368 insn->opcodeRegister = (Reg)(
1369 MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) | (insn->opcode & 7)));
1370 if (insn->rexPrefix && insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1371 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1372 insn->opcodeRegister =
1373 (Reg)(MODRM_REG_SPL + (insn->opcodeRegister - MODRM_REG_AL - 4));
1374 }
1375
1376 break;
1377 case 2:
1378 insn->opcodeRegister = (Reg)(
1379 MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) | (insn->opcode & 7)));
1380 break;
1381 case 4:
1382 insn->opcodeRegister =
1383 (Reg)(MODRM_REG_EAX +
1384 ((bFromREX(insn->rexPrefix) << 3) | (insn->opcode & 7)));
1385 break;
1386 case 8:
1387 insn->opcodeRegister =
1388 (Reg)(MODRM_REG_RAX +
1389 ((bFromREX(insn->rexPrefix) << 3) | (insn->opcode & 7)));
1390 break;
1391 }
1392
1393 return 0;
1394 }
1395
1396 // Consume an immediate operand from an instruction, given the desired operand
1397 // size.
1398 //
1399 // @param insn - The instruction whose operand is to be read.
1400 // @param size - The width (in bytes) of the operand.
1401 // @return - 0 if the immediate was successfully consumed; nonzero
1402 // otherwise.
readImmediate(struct InternalInstruction * insn,uint8_t size)1403 static int readImmediate(struct InternalInstruction *insn, uint8_t size) {
1404 uint8_t imm8;
1405 uint16_t imm16;
1406 uint32_t imm32;
1407 uint64_t imm64;
1408
1409 LLVM_DEBUG(dbgs() << "readImmediate()");
1410
1411 assert(insn->numImmediatesConsumed < 2 && "Already consumed two immediates");
1412
1413 insn->immediateSize = size;
1414 insn->immediateOffset = insn->readerCursor - insn->startLocation;
1415
1416 switch (size) {
1417 case 1:
1418 if (consume(insn, imm8))
1419 return -1;
1420 insn->immediates[insn->numImmediatesConsumed] = imm8;
1421 break;
1422 case 2:
1423 if (consume(insn, imm16))
1424 return -1;
1425 insn->immediates[insn->numImmediatesConsumed] = imm16;
1426 break;
1427 case 4:
1428 if (consume(insn, imm32))
1429 return -1;
1430 insn->immediates[insn->numImmediatesConsumed] = imm32;
1431 break;
1432 case 8:
1433 if (consume(insn, imm64))
1434 return -1;
1435 insn->immediates[insn->numImmediatesConsumed] = imm64;
1436 break;
1437 default:
1438 llvm_unreachable("invalid size");
1439 }
1440
1441 insn->numImmediatesConsumed++;
1442
1443 return 0;
1444 }
1445
1446 // Consume vvvv from an instruction if it has a VEX prefix.
readVVVV(struct InternalInstruction * insn)1447 static int readVVVV(struct InternalInstruction *insn) {
1448 LLVM_DEBUG(dbgs() << "readVVVV()");
1449
1450 int vvvv;
1451 if (insn->vectorExtensionType == TYPE_EVEX)
1452 vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1453 vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
1454 else if (insn->vectorExtensionType == TYPE_VEX_3B)
1455 vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
1456 else if (insn->vectorExtensionType == TYPE_VEX_2B)
1457 vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
1458 else if (insn->vectorExtensionType == TYPE_XOP)
1459 vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
1460 else
1461 return -1;
1462
1463 if (insn->mode != MODE_64BIT)
1464 vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
1465
1466 insn->vvvv = static_cast<Reg>(vvvv);
1467 return 0;
1468 }
1469
1470 // Read an mask register from the opcode field of an instruction.
1471 //
1472 // @param insn - The instruction whose opcode field is to be read.
1473 // @return - 0 on success; nonzero otherwise.
readMaskRegister(struct InternalInstruction * insn)1474 static int readMaskRegister(struct InternalInstruction *insn) {
1475 LLVM_DEBUG(dbgs() << "readMaskRegister()");
1476
1477 if (insn->vectorExtensionType != TYPE_EVEX)
1478 return -1;
1479
1480 insn->writemask =
1481 static_cast<Reg>(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
1482 return 0;
1483 }
1484
1485 // Consults the specifier for an instruction and consumes all
1486 // operands for that instruction, interpreting them as it goes.
readOperands(struct InternalInstruction * insn)1487 static int readOperands(struct InternalInstruction *insn) {
1488 int hasVVVV, needVVVV;
1489 int sawRegImm = 0;
1490
1491 LLVM_DEBUG(dbgs() << "readOperands()");
1492
1493 // If non-zero vvvv specified, make sure one of the operands uses it.
1494 hasVVVV = !readVVVV(insn);
1495 needVVVV = hasVVVV && (insn->vvvv != 0);
1496
1497 for (const auto &Op : x86OperandSets[insn->spec->operands]) {
1498 switch (Op.encoding) {
1499 case ENCODING_NONE:
1500 case ENCODING_SI:
1501 case ENCODING_DI:
1502 break;
1503 CASE_ENCODING_VSIB:
1504 // VSIB can use the V2 bit so check only the other bits.
1505 if (needVVVV)
1506 needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
1507 if (readModRM(insn))
1508 return -1;
1509
1510 // Reject if SIB wasn't used.
1511 if (insn->eaBase != EA_BASE_sib && insn->eaBase != EA_BASE_sib64)
1512 return -1;
1513
1514 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1515 if (insn->sibIndex == SIB_INDEX_NONE)
1516 insn->sibIndex = (SIBIndex)(insn->sibIndexBase + 4);
1517
1518 // If EVEX.v2 is set this is one of the 16-31 registers.
1519 if (insn->vectorExtensionType == TYPE_EVEX && insn->mode == MODE_64BIT &&
1520 v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1521 insn->sibIndex = (SIBIndex)(insn->sibIndex + 16);
1522
1523 // Adjust the index register to the correct size.
1524 switch ((OperandType)Op.type) {
1525 default:
1526 debug("Unhandled VSIB index type");
1527 return -1;
1528 case TYPE_MVSIBX:
1529 insn->sibIndex =
1530 (SIBIndex)(SIB_INDEX_XMM0 + (insn->sibIndex - insn->sibIndexBase));
1531 break;
1532 case TYPE_MVSIBY:
1533 insn->sibIndex =
1534 (SIBIndex)(SIB_INDEX_YMM0 + (insn->sibIndex - insn->sibIndexBase));
1535 break;
1536 case TYPE_MVSIBZ:
1537 insn->sibIndex =
1538 (SIBIndex)(SIB_INDEX_ZMM0 + (insn->sibIndex - insn->sibIndexBase));
1539 break;
1540 }
1541
1542 // Apply the AVX512 compressed displacement scaling factor.
1543 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1544 insn->displacement *= 1 << (Op.encoding - ENCODING_VSIB);
1545 break;
1546 case ENCODING_SIB:
1547 // Reject if SIB wasn't used.
1548 if (insn->eaBase != EA_BASE_sib && insn->eaBase != EA_BASE_sib64)
1549 return -1;
1550 if (readModRM(insn))
1551 return -1;
1552 if (fixupReg(insn, &Op))
1553 return -1;
1554 break;
1555 case ENCODING_REG:
1556 CASE_ENCODING_RM:
1557 if (readModRM(insn))
1558 return -1;
1559 if (fixupReg(insn, &Op))
1560 return -1;
1561 // Apply the AVX512 compressed displacement scaling factor.
1562 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1563 insn->displacement *= 1 << (Op.encoding - ENCODING_RM);
1564 break;
1565 case ENCODING_IB:
1566 if (sawRegImm) {
1567 // Saw a register immediate so don't read again and instead split the
1568 // previous immediate. FIXME: This is a hack.
1569 insn->immediates[insn->numImmediatesConsumed] =
1570 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1571 ++insn->numImmediatesConsumed;
1572 break;
1573 }
1574 if (readImmediate(insn, 1))
1575 return -1;
1576 if (Op.type == TYPE_XMM || Op.type == TYPE_YMM)
1577 sawRegImm = 1;
1578 break;
1579 case ENCODING_IW:
1580 if (readImmediate(insn, 2))
1581 return -1;
1582 break;
1583 case ENCODING_ID:
1584 if (readImmediate(insn, 4))
1585 return -1;
1586 break;
1587 case ENCODING_IO:
1588 if (readImmediate(insn, 8))
1589 return -1;
1590 break;
1591 case ENCODING_Iv:
1592 if (readImmediate(insn, insn->immediateSize))
1593 return -1;
1594 break;
1595 case ENCODING_Ia:
1596 if (readImmediate(insn, insn->addressSize))
1597 return -1;
1598 break;
1599 case ENCODING_IRC:
1600 insn->RC = (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 1) |
1601 lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
1602 break;
1603 case ENCODING_RB:
1604 if (readOpcodeRegister(insn, 1))
1605 return -1;
1606 break;
1607 case ENCODING_RW:
1608 if (readOpcodeRegister(insn, 2))
1609 return -1;
1610 break;
1611 case ENCODING_RD:
1612 if (readOpcodeRegister(insn, 4))
1613 return -1;
1614 break;
1615 case ENCODING_RO:
1616 if (readOpcodeRegister(insn, 8))
1617 return -1;
1618 break;
1619 case ENCODING_Rv:
1620 if (readOpcodeRegister(insn, 0))
1621 return -1;
1622 break;
1623 case ENCODING_CC:
1624 insn->immediates[1] = insn->opcode & 0xf;
1625 break;
1626 case ENCODING_FP:
1627 break;
1628 case ENCODING_VVVV:
1629 needVVVV = 0; // Mark that we have found a VVVV operand.
1630 if (!hasVVVV)
1631 return -1;
1632 if (insn->mode != MODE_64BIT)
1633 insn->vvvv = static_cast<Reg>(insn->vvvv & 0x7);
1634 if (fixupReg(insn, &Op))
1635 return -1;
1636 break;
1637 case ENCODING_WRITEMASK:
1638 if (readMaskRegister(insn))
1639 return -1;
1640 break;
1641 case ENCODING_DUP:
1642 break;
1643 default:
1644 LLVM_DEBUG(dbgs() << "Encountered an operand with an unknown encoding.");
1645 return -1;
1646 }
1647 }
1648
1649 // If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail
1650 if (needVVVV)
1651 return -1;
1652
1653 return 0;
1654 }
1655
1656 namespace llvm {
1657
1658 // Fill-ins to make the compiler happy. These constants are never actually
1659 // assigned; they are just filler to make an automatically-generated switch
1660 // statement work.
1661 namespace X86 {
1662 enum {
1663 BX_SI = 500,
1664 BX_DI = 501,
1665 BP_SI = 502,
1666 BP_DI = 503,
1667 sib = 504,
1668 sib64 = 505
1669 };
1670 } // namespace X86
1671
1672 } // namespace llvm
1673
1674 static bool translateInstruction(MCInst &target,
1675 InternalInstruction &source,
1676 const MCDisassembler *Dis);
1677
1678 namespace {
1679
1680 /// Generic disassembler for all X86 platforms. All each platform class should
1681 /// have to do is subclass the constructor, and provide a different
1682 /// disassemblerMode value.
1683 class X86GenericDisassembler : public MCDisassembler {
1684 std::unique_ptr<const MCInstrInfo> MII;
1685 public:
1686 X86GenericDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx,
1687 std::unique_ptr<const MCInstrInfo> MII);
1688 public:
1689 DecodeStatus getInstruction(MCInst &instr, uint64_t &size,
1690 ArrayRef<uint8_t> Bytes, uint64_t Address,
1691 raw_ostream &cStream) const override;
1692
1693 private:
1694 DisassemblerMode fMode;
1695 };
1696
1697 } // namespace
1698
X86GenericDisassembler(const MCSubtargetInfo & STI,MCContext & Ctx,std::unique_ptr<const MCInstrInfo> MII)1699 X86GenericDisassembler::X86GenericDisassembler(
1700 const MCSubtargetInfo &STI,
1701 MCContext &Ctx,
1702 std::unique_ptr<const MCInstrInfo> MII)
1703 : MCDisassembler(STI, Ctx), MII(std::move(MII)) {
1704 const FeatureBitset &FB = STI.getFeatureBits();
1705 if (FB[X86::Mode16Bit]) {
1706 fMode = MODE_16BIT;
1707 return;
1708 } else if (FB[X86::Mode32Bit]) {
1709 fMode = MODE_32BIT;
1710 return;
1711 } else if (FB[X86::Mode64Bit]) {
1712 fMode = MODE_64BIT;
1713 return;
1714 }
1715
1716 llvm_unreachable("Invalid CPU mode");
1717 }
1718
getInstruction(MCInst & Instr,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address,raw_ostream & CStream) const1719 MCDisassembler::DecodeStatus X86GenericDisassembler::getInstruction(
1720 MCInst &Instr, uint64_t &Size, ArrayRef<uint8_t> Bytes, uint64_t Address,
1721 raw_ostream &CStream) const {
1722 CommentStream = &CStream;
1723
1724 InternalInstruction Insn;
1725 memset(&Insn, 0, sizeof(InternalInstruction));
1726 Insn.bytes = Bytes;
1727 Insn.startLocation = Address;
1728 Insn.readerCursor = Address;
1729 Insn.mode = fMode;
1730
1731 if (Bytes.empty() || readPrefixes(&Insn) || readOpcode(&Insn) ||
1732 getInstructionID(&Insn, MII.get()) || Insn.instructionID == 0 ||
1733 readOperands(&Insn)) {
1734 Size = Insn.readerCursor - Address;
1735 return Fail;
1736 }
1737
1738 Insn.operands = x86OperandSets[Insn.spec->operands];
1739 Insn.length = Insn.readerCursor - Insn.startLocation;
1740 Size = Insn.length;
1741 if (Size > 15)
1742 LLVM_DEBUG(dbgs() << "Instruction exceeds 15-byte limit");
1743
1744 bool Ret = translateInstruction(Instr, Insn, this);
1745 if (!Ret) {
1746 unsigned Flags = X86::IP_NO_PREFIX;
1747 if (Insn.hasAdSize)
1748 Flags |= X86::IP_HAS_AD_SIZE;
1749 if (!Insn.mandatoryPrefix) {
1750 if (Insn.hasOpSize)
1751 Flags |= X86::IP_HAS_OP_SIZE;
1752 if (Insn.repeatPrefix == 0xf2)
1753 Flags |= X86::IP_HAS_REPEAT_NE;
1754 else if (Insn.repeatPrefix == 0xf3 &&
1755 // It should not be 'pause' f3 90
1756 Insn.opcode != 0x90)
1757 Flags |= X86::IP_HAS_REPEAT;
1758 if (Insn.hasLockPrefix)
1759 Flags |= X86::IP_HAS_LOCK;
1760 }
1761 Instr.setFlags(Flags);
1762 }
1763 return (!Ret) ? Success : Fail;
1764 }
1765
1766 //
1767 // Private code that translates from struct InternalInstructions to MCInsts.
1768 //
1769
1770 /// translateRegister - Translates an internal register to the appropriate LLVM
1771 /// register, and appends it as an operand to an MCInst.
1772 ///
1773 /// @param mcInst - The MCInst to append to.
1774 /// @param reg - The Reg to append.
translateRegister(MCInst & mcInst,Reg reg)1775 static void translateRegister(MCInst &mcInst, Reg reg) {
1776 #define ENTRY(x) X86::x,
1777 static constexpr MCPhysReg llvmRegnums[] = {ALL_REGS};
1778 #undef ENTRY
1779
1780 MCPhysReg llvmRegnum = llvmRegnums[reg];
1781 mcInst.addOperand(MCOperand::createReg(llvmRegnum));
1782 }
1783
1784 /// tryAddingSymbolicOperand - trys to add a symbolic operand in place of the
1785 /// immediate Value in the MCInst.
1786 ///
1787 /// @param Value - The immediate Value, has had any PC adjustment made by
1788 /// the caller.
1789 /// @param isBranch - If the instruction is a branch instruction
1790 /// @param Address - The starting address of the instruction
1791 /// @param Offset - The byte offset to this immediate in the instruction
1792 /// @param Width - The byte width of this immediate in the instruction
1793 ///
1794 /// If the getOpInfo() function was set when setupForSymbolicDisassembly() was
1795 /// called then that function is called to get any symbolic information for the
1796 /// immediate in the instruction using the Address, Offset and Width. If that
1797 /// returns non-zero then the symbolic information it returns is used to create
1798 /// an MCExpr and that is added as an operand to the MCInst. If getOpInfo()
1799 /// returns zero and isBranch is true then a symbol look up for immediate Value
1800 /// is done and if a symbol is found an MCExpr is created with that, else
1801 /// an MCExpr with the immediate Value is created. This function returns true
1802 /// if it adds an operand to the MCInst and false otherwise.
tryAddingSymbolicOperand(int64_t Value,bool isBranch,uint64_t Address,uint64_t Offset,uint64_t Width,MCInst & MI,const MCDisassembler * Dis)1803 static bool tryAddingSymbolicOperand(int64_t Value, bool isBranch,
1804 uint64_t Address, uint64_t Offset,
1805 uint64_t Width, MCInst &MI,
1806 const MCDisassembler *Dis) {
1807 return Dis->tryAddingSymbolicOperand(MI, Value, Address, isBranch,
1808 Offset, Width);
1809 }
1810
1811 /// tryAddingPcLoadReferenceComment - trys to add a comment as to what is being
1812 /// referenced by a load instruction with the base register that is the rip.
1813 /// These can often be addresses in a literal pool. The Address of the
1814 /// instruction and its immediate Value are used to determine the address
1815 /// being referenced in the literal pool entry. The SymbolLookUp call back will
1816 /// return a pointer to a literal 'C' string if the referenced address is an
1817 /// address into a section with 'C' string literals.
tryAddingPcLoadReferenceComment(uint64_t Address,uint64_t Value,const void * Decoder)1818 static void tryAddingPcLoadReferenceComment(uint64_t Address, uint64_t Value,
1819 const void *Decoder) {
1820 const MCDisassembler *Dis = static_cast<const MCDisassembler*>(Decoder);
1821 Dis->tryAddingPcLoadReferenceComment(Value, Address);
1822 }
1823
1824 static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
1825 0, // SEG_OVERRIDE_NONE
1826 X86::CS,
1827 X86::SS,
1828 X86::DS,
1829 X86::ES,
1830 X86::FS,
1831 X86::GS
1832 };
1833
1834 /// translateSrcIndex - Appends a source index operand to an MCInst.
1835 ///
1836 /// @param mcInst - The MCInst to append to.
1837 /// @param insn - The internal instruction.
translateSrcIndex(MCInst & mcInst,InternalInstruction & insn)1838 static bool translateSrcIndex(MCInst &mcInst, InternalInstruction &insn) {
1839 unsigned baseRegNo;
1840
1841 if (insn.mode == MODE_64BIT)
1842 baseRegNo = insn.hasAdSize ? X86::ESI : X86::RSI;
1843 else if (insn.mode == MODE_32BIT)
1844 baseRegNo = insn.hasAdSize ? X86::SI : X86::ESI;
1845 else {
1846 assert(insn.mode == MODE_16BIT);
1847 baseRegNo = insn.hasAdSize ? X86::ESI : X86::SI;
1848 }
1849 MCOperand baseReg = MCOperand::createReg(baseRegNo);
1850 mcInst.addOperand(baseReg);
1851
1852 MCOperand segmentReg;
1853 segmentReg = MCOperand::createReg(segmentRegnums[insn.segmentOverride]);
1854 mcInst.addOperand(segmentReg);
1855 return false;
1856 }
1857
1858 /// translateDstIndex - Appends a destination index operand to an MCInst.
1859 ///
1860 /// @param mcInst - The MCInst to append to.
1861 /// @param insn - The internal instruction.
1862
translateDstIndex(MCInst & mcInst,InternalInstruction & insn)1863 static bool translateDstIndex(MCInst &mcInst, InternalInstruction &insn) {
1864 unsigned baseRegNo;
1865
1866 if (insn.mode == MODE_64BIT)
1867 baseRegNo = insn.hasAdSize ? X86::EDI : X86::RDI;
1868 else if (insn.mode == MODE_32BIT)
1869 baseRegNo = insn.hasAdSize ? X86::DI : X86::EDI;
1870 else {
1871 assert(insn.mode == MODE_16BIT);
1872 baseRegNo = insn.hasAdSize ? X86::EDI : X86::DI;
1873 }
1874 MCOperand baseReg = MCOperand::createReg(baseRegNo);
1875 mcInst.addOperand(baseReg);
1876 return false;
1877 }
1878
1879 /// translateImmediate - Appends an immediate operand to an MCInst.
1880 ///
1881 /// @param mcInst - The MCInst to append to.
1882 /// @param immediate - The immediate value to append.
1883 /// @param operand - The operand, as stored in the descriptor table.
1884 /// @param insn - The internal instruction.
translateImmediate(MCInst & mcInst,uint64_t immediate,const OperandSpecifier & operand,InternalInstruction & insn,const MCDisassembler * Dis)1885 static void translateImmediate(MCInst &mcInst, uint64_t immediate,
1886 const OperandSpecifier &operand,
1887 InternalInstruction &insn,
1888 const MCDisassembler *Dis) {
1889 // Sign-extend the immediate if necessary.
1890
1891 OperandType type = (OperandType)operand.type;
1892
1893 bool isBranch = false;
1894 uint64_t pcrel = 0;
1895 if (type == TYPE_REL) {
1896 isBranch = true;
1897 pcrel = insn.startLocation +
1898 insn.immediateOffset + insn.immediateSize;
1899 switch (operand.encoding) {
1900 default:
1901 break;
1902 case ENCODING_Iv:
1903 switch (insn.displacementSize) {
1904 default:
1905 break;
1906 case 1:
1907 if(immediate & 0x80)
1908 immediate |= ~(0xffull);
1909 break;
1910 case 2:
1911 if(immediate & 0x8000)
1912 immediate |= ~(0xffffull);
1913 break;
1914 case 4:
1915 if(immediate & 0x80000000)
1916 immediate |= ~(0xffffffffull);
1917 break;
1918 case 8:
1919 break;
1920 }
1921 break;
1922 case ENCODING_IB:
1923 if(immediate & 0x80)
1924 immediate |= ~(0xffull);
1925 break;
1926 case ENCODING_IW:
1927 if(immediate & 0x8000)
1928 immediate |= ~(0xffffull);
1929 break;
1930 case ENCODING_ID:
1931 if(immediate & 0x80000000)
1932 immediate |= ~(0xffffffffull);
1933 break;
1934 }
1935 }
1936 // By default sign-extend all X86 immediates based on their encoding.
1937 else if (type == TYPE_IMM) {
1938 switch (operand.encoding) {
1939 default:
1940 break;
1941 case ENCODING_IB:
1942 if(immediate & 0x80)
1943 immediate |= ~(0xffull);
1944 break;
1945 case ENCODING_IW:
1946 if(immediate & 0x8000)
1947 immediate |= ~(0xffffull);
1948 break;
1949 case ENCODING_ID:
1950 if(immediate & 0x80000000)
1951 immediate |= ~(0xffffffffull);
1952 break;
1953 case ENCODING_IO:
1954 break;
1955 }
1956 }
1957
1958 switch (type) {
1959 case TYPE_XMM:
1960 mcInst.addOperand(MCOperand::createReg(X86::XMM0 + (immediate >> 4)));
1961 return;
1962 case TYPE_YMM:
1963 mcInst.addOperand(MCOperand::createReg(X86::YMM0 + (immediate >> 4)));
1964 return;
1965 case TYPE_ZMM:
1966 mcInst.addOperand(MCOperand::createReg(X86::ZMM0 + (immediate >> 4)));
1967 return;
1968 default:
1969 // operand is 64 bits wide. Do nothing.
1970 break;
1971 }
1972
1973 if(!tryAddingSymbolicOperand(immediate + pcrel, isBranch, insn.startLocation,
1974 insn.immediateOffset, insn.immediateSize,
1975 mcInst, Dis))
1976 mcInst.addOperand(MCOperand::createImm(immediate));
1977
1978 if (type == TYPE_MOFFS) {
1979 MCOperand segmentReg;
1980 segmentReg = MCOperand::createReg(segmentRegnums[insn.segmentOverride]);
1981 mcInst.addOperand(segmentReg);
1982 }
1983 }
1984
1985 /// translateRMRegister - Translates a register stored in the R/M field of the
1986 /// ModR/M byte to its LLVM equivalent and appends it to an MCInst.
1987 /// @param mcInst - The MCInst to append to.
1988 /// @param insn - The internal instruction to extract the R/M field
1989 /// from.
1990 /// @return - 0 on success; -1 otherwise
translateRMRegister(MCInst & mcInst,InternalInstruction & insn)1991 static bool translateRMRegister(MCInst &mcInst,
1992 InternalInstruction &insn) {
1993 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
1994 debug("A R/M register operand may not have a SIB byte");
1995 return true;
1996 }
1997
1998 switch (insn.eaBase) {
1999 default:
2000 debug("Unexpected EA base register");
2001 return true;
2002 case EA_BASE_NONE:
2003 debug("EA_BASE_NONE for ModR/M base");
2004 return true;
2005 #define ENTRY(x) case EA_BASE_##x:
2006 ALL_EA_BASES
2007 #undef ENTRY
2008 debug("A R/M register operand may not have a base; "
2009 "the operand must be a register.");
2010 return true;
2011 #define ENTRY(x) \
2012 case EA_REG_##x: \
2013 mcInst.addOperand(MCOperand::createReg(X86::x)); break;
2014 ALL_REGS
2015 #undef ENTRY
2016 }
2017
2018 return false;
2019 }
2020
2021 /// translateRMMemory - Translates a memory operand stored in the Mod and R/M
2022 /// fields of an internal instruction (and possibly its SIB byte) to a memory
2023 /// operand in LLVM's format, and appends it to an MCInst.
2024 ///
2025 /// @param mcInst - The MCInst to append to.
2026 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
2027 /// from.
2028 /// @param ForceSIB - The instruction must use SIB.
2029 /// @return - 0 on success; nonzero otherwise
translateRMMemory(MCInst & mcInst,InternalInstruction & insn,const MCDisassembler * Dis,bool ForceSIB=false)2030 static bool translateRMMemory(MCInst &mcInst, InternalInstruction &insn,
2031 const MCDisassembler *Dis,
2032 bool ForceSIB = false) {
2033 // Addresses in an MCInst are represented as five operands:
2034 // 1. basereg (register) The R/M base, or (if there is a SIB) the
2035 // SIB base
2036 // 2. scaleamount (immediate) 1, or (if there is a SIB) the specified
2037 // scale amount
2038 // 3. indexreg (register) x86_registerNONE, or (if there is a SIB)
2039 // the index (which is multiplied by the
2040 // scale amount)
2041 // 4. displacement (immediate) 0, or the displacement if there is one
2042 // 5. segmentreg (register) x86_registerNONE for now, but could be set
2043 // if we have segment overrides
2044
2045 MCOperand baseReg;
2046 MCOperand scaleAmount;
2047 MCOperand indexReg;
2048 MCOperand displacement;
2049 MCOperand segmentReg;
2050 uint64_t pcrel = 0;
2051
2052 if (insn.eaBase == EA_BASE_sib || insn.eaBase == EA_BASE_sib64) {
2053 if (insn.sibBase != SIB_BASE_NONE) {
2054 switch (insn.sibBase) {
2055 default:
2056 debug("Unexpected sibBase");
2057 return true;
2058 #define ENTRY(x) \
2059 case SIB_BASE_##x: \
2060 baseReg = MCOperand::createReg(X86::x); break;
2061 ALL_SIB_BASES
2062 #undef ENTRY
2063 }
2064 } else {
2065 baseReg = MCOperand::createReg(X86::NoRegister);
2066 }
2067
2068 if (insn.sibIndex != SIB_INDEX_NONE) {
2069 switch (insn.sibIndex) {
2070 default:
2071 debug("Unexpected sibIndex");
2072 return true;
2073 #define ENTRY(x) \
2074 case SIB_INDEX_##x: \
2075 indexReg = MCOperand::createReg(X86::x); break;
2076 EA_BASES_32BIT
2077 EA_BASES_64BIT
2078 REGS_XMM
2079 REGS_YMM
2080 REGS_ZMM
2081 #undef ENTRY
2082 }
2083 } else {
2084 // Use EIZ/RIZ for a few ambiguous cases where the SIB byte is present,
2085 // but no index is used and modrm alone should have been enough.
2086 // -No base register in 32-bit mode. In 64-bit mode this is used to
2087 // avoid rip-relative addressing.
2088 // -Any base register used other than ESP/RSP/R12D/R12. Using these as a
2089 // base always requires a SIB byte.
2090 // -A scale other than 1 is used.
2091 if (!ForceSIB &&
2092 (insn.sibScale != 1 ||
2093 (insn.sibBase == SIB_BASE_NONE && insn.mode != MODE_64BIT) ||
2094 (insn.sibBase != SIB_BASE_NONE &&
2095 insn.sibBase != SIB_BASE_ESP && insn.sibBase != SIB_BASE_RSP &&
2096 insn.sibBase != SIB_BASE_R12D && insn.sibBase != SIB_BASE_R12))) {
2097 indexReg = MCOperand::createReg(insn.addressSize == 4 ? X86::EIZ :
2098 X86::RIZ);
2099 } else
2100 indexReg = MCOperand::createReg(X86::NoRegister);
2101 }
2102
2103 scaleAmount = MCOperand::createImm(insn.sibScale);
2104 } else {
2105 switch (insn.eaBase) {
2106 case EA_BASE_NONE:
2107 if (insn.eaDisplacement == EA_DISP_NONE) {
2108 debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
2109 return true;
2110 }
2111 if (insn.mode == MODE_64BIT){
2112 pcrel = insn.startLocation +
2113 insn.displacementOffset + insn.displacementSize;
2114 tryAddingPcLoadReferenceComment(insn.startLocation +
2115 insn.displacementOffset,
2116 insn.displacement + pcrel, Dis);
2117 // Section 2.2.1.6
2118 baseReg = MCOperand::createReg(insn.addressSize == 4 ? X86::EIP :
2119 X86::RIP);
2120 }
2121 else
2122 baseReg = MCOperand::createReg(X86::NoRegister);
2123
2124 indexReg = MCOperand::createReg(X86::NoRegister);
2125 break;
2126 case EA_BASE_BX_SI:
2127 baseReg = MCOperand::createReg(X86::BX);
2128 indexReg = MCOperand::createReg(X86::SI);
2129 break;
2130 case EA_BASE_BX_DI:
2131 baseReg = MCOperand::createReg(X86::BX);
2132 indexReg = MCOperand::createReg(X86::DI);
2133 break;
2134 case EA_BASE_BP_SI:
2135 baseReg = MCOperand::createReg(X86::BP);
2136 indexReg = MCOperand::createReg(X86::SI);
2137 break;
2138 case EA_BASE_BP_DI:
2139 baseReg = MCOperand::createReg(X86::BP);
2140 indexReg = MCOperand::createReg(X86::DI);
2141 break;
2142 default:
2143 indexReg = MCOperand::createReg(X86::NoRegister);
2144 switch (insn.eaBase) {
2145 default:
2146 debug("Unexpected eaBase");
2147 return true;
2148 // Here, we will use the fill-ins defined above. However,
2149 // BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
2150 // sib and sib64 were handled in the top-level if, so they're only
2151 // placeholders to keep the compiler happy.
2152 #define ENTRY(x) \
2153 case EA_BASE_##x: \
2154 baseReg = MCOperand::createReg(X86::x); break;
2155 ALL_EA_BASES
2156 #undef ENTRY
2157 #define ENTRY(x) case EA_REG_##x:
2158 ALL_REGS
2159 #undef ENTRY
2160 debug("A R/M memory operand may not be a register; "
2161 "the base field must be a base.");
2162 return true;
2163 }
2164 }
2165
2166 scaleAmount = MCOperand::createImm(1);
2167 }
2168
2169 displacement = MCOperand::createImm(insn.displacement);
2170
2171 segmentReg = MCOperand::createReg(segmentRegnums[insn.segmentOverride]);
2172
2173 mcInst.addOperand(baseReg);
2174 mcInst.addOperand(scaleAmount);
2175 mcInst.addOperand(indexReg);
2176 if(!tryAddingSymbolicOperand(insn.displacement + pcrel, false,
2177 insn.startLocation, insn.displacementOffset,
2178 insn.displacementSize, mcInst, Dis))
2179 mcInst.addOperand(displacement);
2180 mcInst.addOperand(segmentReg);
2181 return false;
2182 }
2183
2184 /// translateRM - Translates an operand stored in the R/M (and possibly SIB)
2185 /// byte of an instruction to LLVM form, and appends it to an MCInst.
2186 ///
2187 /// @param mcInst - The MCInst to append to.
2188 /// @param operand - The operand, as stored in the descriptor table.
2189 /// @param insn - The instruction to extract Mod, R/M, and SIB fields
2190 /// from.
2191 /// @return - 0 on success; nonzero otherwise
translateRM(MCInst & mcInst,const OperandSpecifier & operand,InternalInstruction & insn,const MCDisassembler * Dis)2192 static bool translateRM(MCInst &mcInst, const OperandSpecifier &operand,
2193 InternalInstruction &insn, const MCDisassembler *Dis) {
2194 switch (operand.type) {
2195 default:
2196 debug("Unexpected type for a R/M operand");
2197 return true;
2198 case TYPE_R8:
2199 case TYPE_R16:
2200 case TYPE_R32:
2201 case TYPE_R64:
2202 case TYPE_Rv:
2203 case TYPE_MM64:
2204 case TYPE_XMM:
2205 case TYPE_YMM:
2206 case TYPE_ZMM:
2207 case TYPE_TMM:
2208 case TYPE_VK_PAIR:
2209 case TYPE_VK:
2210 case TYPE_DEBUGREG:
2211 case TYPE_CONTROLREG:
2212 case TYPE_BNDR:
2213 return translateRMRegister(mcInst, insn);
2214 case TYPE_M:
2215 case TYPE_MVSIBX:
2216 case TYPE_MVSIBY:
2217 case TYPE_MVSIBZ:
2218 return translateRMMemory(mcInst, insn, Dis);
2219 case TYPE_MSIB:
2220 return translateRMMemory(mcInst, insn, Dis, true);
2221 }
2222 }
2223
2224 /// translateFPRegister - Translates a stack position on the FPU stack to its
2225 /// LLVM form, and appends it to an MCInst.
2226 ///
2227 /// @param mcInst - The MCInst to append to.
2228 /// @param stackPos - The stack position to translate.
translateFPRegister(MCInst & mcInst,uint8_t stackPos)2229 static void translateFPRegister(MCInst &mcInst,
2230 uint8_t stackPos) {
2231 mcInst.addOperand(MCOperand::createReg(X86::ST0 + stackPos));
2232 }
2233
2234 /// translateMaskRegister - Translates a 3-bit mask register number to
2235 /// LLVM form, and appends it to an MCInst.
2236 ///
2237 /// @param mcInst - The MCInst to append to.
2238 /// @param maskRegNum - Number of mask register from 0 to 7.
2239 /// @return - false on success; true otherwise.
translateMaskRegister(MCInst & mcInst,uint8_t maskRegNum)2240 static bool translateMaskRegister(MCInst &mcInst,
2241 uint8_t maskRegNum) {
2242 if (maskRegNum >= 8) {
2243 debug("Invalid mask register number");
2244 return true;
2245 }
2246
2247 mcInst.addOperand(MCOperand::createReg(X86::K0 + maskRegNum));
2248 return false;
2249 }
2250
2251 /// translateOperand - Translates an operand stored in an internal instruction
2252 /// to LLVM's format and appends it to an MCInst.
2253 ///
2254 /// @param mcInst - The MCInst to append to.
2255 /// @param operand - The operand, as stored in the descriptor table.
2256 /// @param insn - The internal instruction.
2257 /// @return - false on success; true otherwise.
translateOperand(MCInst & mcInst,const OperandSpecifier & operand,InternalInstruction & insn,const MCDisassembler * Dis)2258 static bool translateOperand(MCInst &mcInst, const OperandSpecifier &operand,
2259 InternalInstruction &insn,
2260 const MCDisassembler *Dis) {
2261 switch (operand.encoding) {
2262 default:
2263 debug("Unhandled operand encoding during translation");
2264 return true;
2265 case ENCODING_REG:
2266 translateRegister(mcInst, insn.reg);
2267 return false;
2268 case ENCODING_WRITEMASK:
2269 return translateMaskRegister(mcInst, insn.writemask);
2270 case ENCODING_SIB:
2271 CASE_ENCODING_RM:
2272 CASE_ENCODING_VSIB:
2273 return translateRM(mcInst, operand, insn, Dis);
2274 case ENCODING_IB:
2275 case ENCODING_IW:
2276 case ENCODING_ID:
2277 case ENCODING_IO:
2278 case ENCODING_Iv:
2279 case ENCODING_Ia:
2280 translateImmediate(mcInst,
2281 insn.immediates[insn.numImmediatesTranslated++],
2282 operand,
2283 insn,
2284 Dis);
2285 return false;
2286 case ENCODING_IRC:
2287 mcInst.addOperand(MCOperand::createImm(insn.RC));
2288 return false;
2289 case ENCODING_SI:
2290 return translateSrcIndex(mcInst, insn);
2291 case ENCODING_DI:
2292 return translateDstIndex(mcInst, insn);
2293 case ENCODING_RB:
2294 case ENCODING_RW:
2295 case ENCODING_RD:
2296 case ENCODING_RO:
2297 case ENCODING_Rv:
2298 translateRegister(mcInst, insn.opcodeRegister);
2299 return false;
2300 case ENCODING_CC:
2301 mcInst.addOperand(MCOperand::createImm(insn.immediates[1]));
2302 return false;
2303 case ENCODING_FP:
2304 translateFPRegister(mcInst, insn.modRM & 7);
2305 return false;
2306 case ENCODING_VVVV:
2307 translateRegister(mcInst, insn.vvvv);
2308 return false;
2309 case ENCODING_DUP:
2310 return translateOperand(mcInst, insn.operands[operand.type - TYPE_DUP0],
2311 insn, Dis);
2312 }
2313 }
2314
2315 /// translateInstruction - Translates an internal instruction and all its
2316 /// operands to an MCInst.
2317 ///
2318 /// @param mcInst - The MCInst to populate with the instruction's data.
2319 /// @param insn - The internal instruction.
2320 /// @return - false on success; true otherwise.
translateInstruction(MCInst & mcInst,InternalInstruction & insn,const MCDisassembler * Dis)2321 static bool translateInstruction(MCInst &mcInst,
2322 InternalInstruction &insn,
2323 const MCDisassembler *Dis) {
2324 if (!insn.spec) {
2325 debug("Instruction has no specification");
2326 return true;
2327 }
2328
2329 mcInst.clear();
2330 mcInst.setOpcode(insn.instructionID);
2331 // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
2332 // prefix bytes should be disassembled as xrelease and xacquire then set the
2333 // opcode to those instead of the rep and repne opcodes.
2334 if (insn.xAcquireRelease) {
2335 if(mcInst.getOpcode() == X86::REP_PREFIX)
2336 mcInst.setOpcode(X86::XRELEASE_PREFIX);
2337 else if(mcInst.getOpcode() == X86::REPNE_PREFIX)
2338 mcInst.setOpcode(X86::XACQUIRE_PREFIX);
2339 }
2340
2341 insn.numImmediatesTranslated = 0;
2342
2343 for (const auto &Op : insn.operands) {
2344 if (Op.encoding != ENCODING_NONE) {
2345 if (translateOperand(mcInst, Op, insn, Dis)) {
2346 return true;
2347 }
2348 }
2349 }
2350
2351 return false;
2352 }
2353
createX86Disassembler(const Target & T,const MCSubtargetInfo & STI,MCContext & Ctx)2354 static MCDisassembler *createX86Disassembler(const Target &T,
2355 const MCSubtargetInfo &STI,
2356 MCContext &Ctx) {
2357 std::unique_ptr<const MCInstrInfo> MII(T.createMCInstrInfo());
2358 return new X86GenericDisassembler(STI, Ctx, std::move(MII));
2359 }
2360
LLVMInitializeX86Disassembler()2361 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86Disassembler() {
2362 // Register the disassembler.
2363 TargetRegistry::RegisterMCDisassembler(getTheX86_32Target(),
2364 createX86Disassembler);
2365 TargetRegistry::RegisterMCDisassembler(getTheX86_64Target(),
2366 createX86Disassembler);
2367 }
2368