blob: 54d550b606520f2cf4dabc0fd6c40cee6db85fc9 [file] [log] [blame]
Craig Topperabfe07e2014-10-07 07:29:46 +00001//===-- X86DisassemblerDecoder.cpp - Disassembler decoder -----------------===//
Richard Smith89ee75d2014-04-20 21:07:34 +00002//
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 implementation 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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include <cstdarg> /* for va_*() */
17#include <cstdio> /* for vsnprintf() */
18#include <cstdlib> /* for exit() */
19#include <cstring> /* for memset() */
Sean Callanan04cc3072009-12-19 02:59:52 +000020
21#include "X86DisassemblerDecoder.h"
22
Richard Smith89ee75d2014-04-20 21:07:34 +000023using namespace llvm::X86Disassembler;
24
Richard Smithac15f1c2014-04-20 21:52:16 +000025/// Specifies whether a ModR/M byte is needed and (if so) which
26/// instruction each possible value of the ModR/M byte corresponds to. Once
27/// this information is known, we have narrowed down to a single instruction.
28struct ModRMDecision {
29 uint8_t modrm_type;
30 uint16_t instructionIDs;
31};
32
33/// Specifies which set of ModR/M->instruction tables to look at
34/// given a particular opcode.
35struct OpcodeDecision {
36 ModRMDecision modRMDecisions[256];
37};
38
39/// Specifies which opcode->instruction tables to look at given
40/// a particular context (set of attributes). Since there are many possible
41/// contexts, the decoder first uses CONTEXTS_SYM to determine which context
42/// applies given a specific set of attributes. Hence there are only IC_max
43/// entries in this table, rather than 2^(ATTR_max).
44struct ContextDecision {
45 OpcodeDecision opcodeDecisions[IC_max];
46};
47
Sean Callanan04cc3072009-12-19 02:59:52 +000048#include "X86GenDisassemblerTables.inc"
49
Sean Callanan010b3732010-04-02 21:23:51 +000050#ifndef NDEBUG
Richard Smith89ee75d2014-04-20 21:07:34 +000051#define debug(s) do { Debug(__FILE__, __LINE__, s); } while (0)
Sean Callanan010b3732010-04-02 21:23:51 +000052#else
53#define debug(s) do { } while (0)
54#endif
55
Sean Callanan04cc3072009-12-19 02:59:52 +000056/*
57 * contextForAttrs - Client for the instruction context table. Takes a set of
58 * attributes and returns the appropriate decode context.
59 *
60 * @param attrMask - Attributes, from the enumeration attributeBits.
61 * @return - The InstructionContext to use when looking up an
62 * an instruction with these attributes.
63 */
Elena Demikhovsky371e3632013-12-25 11:40:51 +000064static InstructionContext contextForAttrs(uint16_t attrMask) {
Richard Smith89ee75d2014-04-20 21:07:34 +000065 return static_cast<InstructionContext>(CONTEXTS_SYM[attrMask]);
Sean Callanan04cc3072009-12-19 02:59:52 +000066}
67
68/*
69 * modRMRequired - Reads the appropriate instruction table to determine whether
70 * the ModR/M byte is required to decode a particular instruction.
71 *
72 * @param type - The opcode type (i.e., how many bytes it has).
73 * @param insnContext - The context for the instruction, as returned by
74 * contextForAttrs.
75 * @param opcode - The last byte of the instruction's opcode, not counting
76 * ModR/M extensions and escapes.
Richard Smith5d5061032014-04-20 22:15:37 +000077 * @return - true if the ModR/M byte is required, false otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +000078 */
Sean Callanan588785c2009-12-22 22:51:40 +000079static int modRMRequired(OpcodeType type,
Craig Topper21c33652011-10-02 16:56:09 +000080 InstructionContext insnContext,
Elena Demikhovsky371e3632013-12-25 11:40:51 +000081 uint16_t opcode) {
Craig Toppere73658d2014-04-28 04:05:08 +000082 const struct ContextDecision* decision = nullptr;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +000083
Sean Callanan04cc3072009-12-19 02:59:52 +000084 switch (type) {
85 case ONEBYTE:
86 decision = &ONEBYTE_SYM;
87 break;
88 case TWOBYTE:
89 decision = &TWOBYTE_SYM;
90 break;
91 case THREEBYTE_38:
92 decision = &THREEBYTE38_SYM;
93 break;
94 case THREEBYTE_3A:
95 decision = &THREEBYTE3A_SYM;
96 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +000097 case XOP8_MAP:
98 decision = &XOP8_MAP_SYM;
99 break;
100 case XOP9_MAP:
101 decision = &XOP9_MAP_SYM;
102 break;
103 case XOPA_MAP:
104 decision = &XOPA_MAP_SYM;
105 break;
Craig Topper097b47a2018-03-24 07:48:54 +0000106 case THREEDNOW_MAP:
107 decision = &THREEDNOW_MAP_SYM;
108 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000109 }
Ahmed Charles636a3d62012-02-19 11:37:01 +0000110
Sean Callanan04cc3072009-12-19 02:59:52 +0000111 return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].
112 modrm_type != MODRM_ONEENTRY;
Sean Callanan04cc3072009-12-19 02:59:52 +0000113}
114
115/*
116 * decode - Reads the appropriate instruction table to obtain the unique ID of
117 * an instruction.
118 *
119 * @param type - See modRMRequired().
120 * @param insnContext - See modRMRequired().
121 * @param opcode - See modRMRequired().
122 * @param modRM - The ModR/M byte if required, or any value if not.
Sean Callanan010b3732010-04-02 21:23:51 +0000123 * @return - The UID of the instruction, or 0 on failure.
Sean Callanan04cc3072009-12-19 02:59:52 +0000124 */
Sean Callanan588785c2009-12-22 22:51:40 +0000125static InstrUID decode(OpcodeType type,
Sean Callanan010b3732010-04-02 21:23:51 +0000126 InstructionContext insnContext,
127 uint8_t opcode,
128 uint8_t modRM) {
Craig Toppere73658d2014-04-28 04:05:08 +0000129 const struct ModRMDecision* dec = nullptr;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000130
Sean Callanan04cc3072009-12-19 02:59:52 +0000131 switch (type) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000132 case ONEBYTE:
133 dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
134 break;
135 case TWOBYTE:
136 dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
137 break;
138 case THREEBYTE_38:
139 dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
140 break;
141 case THREEBYTE_3A:
142 dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
143 break;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000144 case XOP8_MAP:
145 dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
146 break;
147 case XOP9_MAP:
148 dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
149 break;
150 case XOPA_MAP:
151 dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
152 break;
Craig Topper097b47a2018-03-24 07:48:54 +0000153 case THREEDNOW_MAP:
154 dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
155 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000156 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000157
Sean Callanan04cc3072009-12-19 02:59:52 +0000158 switch (dec->modrm_type) {
159 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000160 debug("Corrupt table! Unknown modrm_type");
161 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000162 case MODRM_ONEENTRY:
Craig Topper487e7442012-02-09 07:45:30 +0000163 return modRMTable[dec->instructionIDs];
Sean Callanan04cc3072009-12-19 02:59:52 +0000164 case MODRM_SPLITRM:
165 if (modFromModRM(modRM) == 0x3)
Craig Topper487e7442012-02-09 07:45:30 +0000166 return modRMTable[dec->instructionIDs+1];
167 return modRMTable[dec->instructionIDs];
Craig Toppera0cd9702012-02-09 08:58:07 +0000168 case MODRM_SPLITREG:
169 if (modFromModRM(modRM) == 0x3)
170 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8];
171 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Craig Topper963305b2012-09-13 05:45:42 +0000172 case MODRM_SPLITMISC:
173 if (modFromModRM(modRM) == 0x3)
174 return modRMTable[dec->instructionIDs+(modRM & 0x3f)+8];
175 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Sean Callanan04cc3072009-12-19 02:59:52 +0000176 case MODRM_FULL:
Craig Topper487e7442012-02-09 07:45:30 +0000177 return modRMTable[dec->instructionIDs+modRM];
Sean Callanan04cc3072009-12-19 02:59:52 +0000178 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000179}
180
181/*
182 * specifierForUID - Given a UID, returns the name and operand specification for
183 * that instruction.
184 *
185 * @param uid - The unique ID for the instruction. This should be returned by
186 * decode(); specifierForUID will not check bounds.
187 * @return - A pointer to the specification for that instruction.
188 */
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000189static const struct InstructionSpecifier *specifierForUID(InstrUID uid) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000190 return &INSTRUCTIONS_SYM[uid];
191}
192
193/*
194 * consumeByte - Uses the reader function provided by the user to consume one
195 * byte from the instruction's memory and advance the cursor.
196 *
197 * @param insn - The instruction with the reader function to use. The cursor
198 * for this instruction is advanced.
199 * @param byte - A pointer to a pre-allocated memory buffer to be populated
200 * with the data read.
201 * @return - 0 if the read was successful; nonzero otherwise.
202 */
Sean Callanan588785c2009-12-22 22:51:40 +0000203static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000204 int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000205
Sean Callanan04cc3072009-12-19 02:59:52 +0000206 if (!ret)
207 ++(insn->readerCursor);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000208
Sean Callanan04cc3072009-12-19 02:59:52 +0000209 return ret;
210}
211
212/*
213 * lookAtByte - Like consumeByte, but does not advance the cursor.
214 *
215 * @param insn - See consumeByte().
216 * @param byte - See consumeByte().
217 * @return - See consumeByte().
218 */
Sean Callanan588785c2009-12-22 22:51:40 +0000219static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000220 return insn->reader(insn->readerArg, byte, insn->readerCursor);
221}
222
Sean Callanan588785c2009-12-22 22:51:40 +0000223static void unconsumeByte(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000224 insn->readerCursor--;
225}
226
Sean Callanan588785c2009-12-22 22:51:40 +0000227#define CONSUME_FUNC(name, type) \
228 static int name(struct InternalInstruction* insn, type* ptr) { \
229 type combined = 0; \
230 unsigned offset; \
231 for (offset = 0; offset < sizeof(type); ++offset) { \
232 uint8_t byte; \
233 int ret = insn->reader(insn->readerArg, \
234 &byte, \
235 insn->readerCursor + offset); \
236 if (ret) \
237 return ret; \
Richard Smith228e6d42012-08-24 23:29:28 +0000238 combined = combined | ((uint64_t)byte << (offset * 8)); \
Sean Callanan588785c2009-12-22 22:51:40 +0000239 } \
240 *ptr = combined; \
241 insn->readerCursor += sizeof(type); \
242 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +0000243 }
244
245/*
246 * consume* - Use the reader function provided by the user to consume data
247 * values of various sizes from the instruction's memory and advance the
248 * cursor appropriately. These readers perform endian conversion.
249 *
250 * @param insn - See consumeByte().
251 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
252 * be populated with the data read.
253 * @return - See consumeByte().
254 */
255CONSUME_FUNC(consumeInt8, int8_t)
256CONSUME_FUNC(consumeInt16, int16_t)
257CONSUME_FUNC(consumeInt32, int32_t)
258CONSUME_FUNC(consumeUInt16, uint16_t)
259CONSUME_FUNC(consumeUInt32, uint32_t)
260CONSUME_FUNC(consumeUInt64, uint64_t)
261
262/*
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000263 * dbgprintf - Uses the logging function provided by the user to log a single
Sean Callanan04cc3072009-12-19 02:59:52 +0000264 * message, typically without a carriage-return.
265 *
266 * @param insn - The instruction containing the logging function.
267 * @param format - See printf().
268 * @param ... - See printf().
269 */
Sean Callanan588785c2009-12-22 22:51:40 +0000270static void dbgprintf(struct InternalInstruction* insn,
271 const char* format,
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000272 ...) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000273 char buffer[256];
274 va_list ap;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000275
Sean Callanan04cc3072009-12-19 02:59:52 +0000276 if (!insn->dlog)
277 return;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000278
Sean Callanan04cc3072009-12-19 02:59:52 +0000279 va_start(ap, format);
280 (void)vsnprintf(buffer, sizeof(buffer), format, ap);
281 va_end(ap);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000282
Sean Callanan04cc3072009-12-19 02:59:52 +0000283 insn->dlog(insn->dlogArg, buffer);
Sean Callanan04cc3072009-12-19 02:59:52 +0000284}
285
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000286static bool isREX(struct InternalInstruction *insn, uint8_t prefix) {
287 if (insn->mode == MODE_64BIT)
288 return prefix >= 0x40 && prefix <= 0x4f;
289 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000290}
291
292/*
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000293 * setPrefixPresent - Marks that a particular prefix is present as mandatory
Sean Callanan04cc3072009-12-19 02:59:52 +0000294 *
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000295 * @param insn - The instruction to be marked as having the prefix.
296 * @param prefix - The prefix that is present.
Sean Callanan04cc3072009-12-19 02:59:52 +0000297 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000298static void setPrefixPresent(struct InternalInstruction *insn, uint8_t prefix) {
299 uint8_t nextByte;
300 switch (prefix) {
Maksim Panchenko89e4abe2018-07-05 23:32:42 +0000301 case 0xf0:
302 insn->hasLockPrefix = true;
303 break;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000304 case 0xf2:
305 case 0xf3:
306 if (lookAtByte(insn, &nextByte))
307 break;
308 // TODO:
309 // 1. There could be several 0x66
310 // 2. if (nextByte == 0x66) and nextNextByte != 0x0f then
311 // it's not mandatory prefix
312 // 3. if (nextByte >= 0x40 && nextByte <= 0x4f) it's REX and we need
313 // 0x0f exactly after it to be mandatory prefix
314 if (isREX(insn, nextByte) || nextByte == 0x0f || nextByte == 0x66)
315 // The last of 0xf2 /0xf3 is mandatory prefix
316 insn->mandatoryPrefix = prefix;
317 insn->repeatPrefix = prefix;
318 break;
319 case 0x66:
320 if (lookAtByte(insn, &nextByte))
321 break;
322 // 0x66 can't overwrite existing mandatory prefix and should be ignored
323 if (!insn->mandatoryPrefix && (nextByte == 0x0f || isREX(insn, nextByte)))
324 insn->mandatoryPrefix = prefix;
325 break;
326 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000327}
328
329/*
330 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
331 * instruction as having them. Also sets the instruction's default operand,
332 * address, and other relevant data sizes to report operands correctly.
333 *
334 * @param insn - The instruction whose prefixes are to be read.
335 * @return - 0 if the instruction could be read until the end of the prefix
336 * bytes, and no prefixes conflicted; nonzero otherwise.
337 */
338static int readPrefixes(struct InternalInstruction* insn) {
Richard Smith5d5061032014-04-20 22:15:37 +0000339 bool isPrefix = true;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000340 uint8_t byte = 0;
Richard Mitton79917a92013-08-30 21:32:42 +0000341 uint8_t nextByte;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000342
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000343 dbgprintf(insn, "readPrefixes()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000344
Sean Callanan04cc3072009-12-19 02:59:52 +0000345 while (isPrefix) {
Richard Mitton576ee002013-08-30 21:19:48 +0000346 /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
Sean Callanan04cc3072009-12-19 02:59:52 +0000347 if (consumeByte(insn, &byte))
Richard Mitton576ee002013-08-30 21:19:48 +0000348 break;
Kevin Enderby014e1cd2012-03-09 17:52:49 +0000349
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000350 /*
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000351 * If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
352 * break and let it be disassembled as a normal "instruction".
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000353 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000354 if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0) // LOCK
Richard Mitton576ee002013-08-30 21:19:48 +0000355 break;
356
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000357 if ((byte == 0xf2 || byte == 0xf3) && !lookAtByte(insn, &nextByte)) {
Kevin Enderby35fd7922013-06-20 22:32:18 +0000358 /*
359 * If the byte is 0xf2 or 0xf3, and any of the following conditions are
360 * met:
361 * - it is followed by a LOCK (0xf0) prefix
362 * - it is followed by an xchg instruction
363 * then it should be disassembled as a xacquire/xrelease not repne/rep.
364 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000365 if (((nextByte == 0xf0) ||
366 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90))) {
Richard Smith5d5061032014-04-20 22:15:37 +0000367 insn->xAcquireRelease = true;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000368 if (!(byte == 0xf3 && nextByte == 0x90)) // PAUSE instruction support
369 break;
370 }
Kevin Enderby35fd7922013-06-20 22:32:18 +0000371 /*
372 * Also if the byte is 0xf3, and the following condition is met:
373 * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
374 * "mov mem, imm" (opcode 0xc6/0xc7) instructions.
375 * then it should be disassembled as an xrelease not rep.
376 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000377 if (byte == 0xf3 && (nextByte == 0x88 || nextByte == 0x89 ||
378 nextByte == 0xc6 || nextByte == 0xc7)) {
Richard Smith5d5061032014-04-20 22:15:37 +0000379 insn->xAcquireRelease = true;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000380 if (nextByte != 0x90) // PAUSE instruction support
381 break;
382 }
383 if (isREX(insn, nextByte)) {
384 uint8_t nnextByte;
385 // Go to REX prefix after the current one
386 if (consumeByte(insn, &nnextByte))
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000387 return -1;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000388 // We should be able to read next byte after REX prefix
389 if (lookAtByte(insn, &nnextByte))
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000390 return -1;
391 unconsumeByte(insn);
392 }
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000393 }
394
Sean Callanan04cc3072009-12-19 02:59:52 +0000395 switch (byte) {
396 case 0xf0: /* LOCK */
397 case 0xf2: /* REPNE/REPNZ */
398 case 0xf3: /* REP or REPE/REPZ */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000399 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000400 break;
401 case 0x2e: /* CS segment override -OR- Branch not taken */
402 case 0x36: /* SS segment override -OR- Branch taken */
403 case 0x3e: /* DS segment override */
404 case 0x26: /* ES segment override */
405 case 0x64: /* FS segment override */
406 case 0x65: /* GS segment override */
407 switch (byte) {
408 case 0x2e:
409 insn->segmentOverride = SEG_OVERRIDE_CS;
410 break;
411 case 0x36:
412 insn->segmentOverride = SEG_OVERRIDE_SS;
413 break;
414 case 0x3e:
415 insn->segmentOverride = SEG_OVERRIDE_DS;
416 break;
417 case 0x26:
418 insn->segmentOverride = SEG_OVERRIDE_ES;
419 break;
420 case 0x64:
421 insn->segmentOverride = SEG_OVERRIDE_FS;
422 break;
423 case 0x65:
424 insn->segmentOverride = SEG_OVERRIDE_GS;
425 break;
426 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000427 debug("Unhandled override");
428 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000429 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000430 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000431 break;
432 case 0x66: /* Operand-size override */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000433 insn->hasOpSize = true;
434 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000435 break;
436 case 0x67: /* Address-size override */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000437 insn->hasAdSize = true;
438 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000439 break;
440 default: /* Not a prefix byte */
Richard Smith5d5061032014-04-20 22:15:37 +0000441 isPrefix = false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000442 break;
443 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000444
Sean Callanan04cc3072009-12-19 02:59:52 +0000445 if (isPrefix)
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000446 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000447 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000448
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000449 insn->vectorExtensionType = TYPE_NO_VEX_XOP;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000450
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000451 if (byte == 0x62) {
452 uint8_t byte1, byte2;
453
454 if (consumeByte(insn, &byte1)) {
455 dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
456 return -1;
457 }
458
459 if (lookAtByte(insn, &byte2)) {
460 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
461 return -1;
462 }
463
464 if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
465 ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
466 insn->vectorExtensionType = TYPE_EVEX;
Craig Topper273515e2014-10-07 07:29:48 +0000467 } else {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000468 unconsumeByte(insn); /* unconsume byte1 */
469 unconsumeByte(insn); /* unconsume byte */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000470 }
471
472 if (insn->vectorExtensionType == TYPE_EVEX) {
473 insn->vectorExtensionPrefix[0] = byte;
474 insn->vectorExtensionPrefix[1] = byte1;
475 if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) {
476 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
477 return -1;
478 }
479 if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) {
480 dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
481 return -1;
482 }
483
484 /* We simulate the REX prefix for simplicity's sake */
485 if (insn->mode == MODE_64BIT) {
486 insn->rexPrefix = 0x40
487 | (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3)
488 | (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2)
489 | (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1)
490 | (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
491 }
492
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000493 dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
494 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
495 insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
496 }
Craig Topper273515e2014-10-07 07:29:48 +0000497 } else if (byte == 0xc4) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000498 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000499
Sean Callananc3fd5232011-03-15 01:23:15 +0000500 if (lookAtByte(insn, &byte1)) {
501 dbgprintf(insn, "Couldn't read second byte of VEX");
502 return -1;
503 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000504
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000505 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000506 insn->vectorExtensionType = TYPE_VEX_3B;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000507 else
Sean Callanan04cc3072009-12-19 02:59:52 +0000508 unconsumeByte(insn);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000509
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000510 if (insn->vectorExtensionType == TYPE_VEX_3B) {
511 insn->vectorExtensionPrefix[0] = byte;
512 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
513 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000514
515 /* We simulate the REX prefix for simplicity's sake */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000516
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000517 if (insn->mode == MODE_64BIT)
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000518 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000519 | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3)
520 | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2)
521 | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1)
522 | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000523
Craig Topper9e3e38a2013-10-03 05:17:48 +0000524 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000525 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
526 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000527 }
Craig Topper273515e2014-10-07 07:29:48 +0000528 } else if (byte == 0xc5) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000529 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000530
Sean Callananc3fd5232011-03-15 01:23:15 +0000531 if (lookAtByte(insn, &byte1)) {
532 dbgprintf(insn, "Couldn't read second byte of VEX");
533 return -1;
534 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000535
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000536 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000537 insn->vectorExtensionType = TYPE_VEX_2B;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000538 else
Sean Callananc3fd5232011-03-15 01:23:15 +0000539 unconsumeByte(insn);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000540
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000541 if (insn->vectorExtensionType == TYPE_VEX_2B) {
542 insn->vectorExtensionPrefix[0] = byte;
543 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000544
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000545 if (insn->mode == MODE_64BIT)
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000546 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000547 | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000548
Craig Topper273515e2014-10-07 07:29:48 +0000549 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000550 default:
551 break;
552 case VEX_PREFIX_66:
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000553 insn->hasOpSize = true;
Sean Callananc3fd5232011-03-15 01:23:15 +0000554 break;
555 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000556
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000557 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
558 insn->vectorExtensionPrefix[0],
559 insn->vectorExtensionPrefix[1]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000560 }
Craig Topper273515e2014-10-07 07:29:48 +0000561 } else if (byte == 0x8f) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000562 uint8_t byte1;
563
564 if (lookAtByte(insn, &byte1)) {
565 dbgprintf(insn, "Couldn't read second byte of XOP");
566 return -1;
567 }
568
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000569 if ((byte1 & 0x38) != 0x0) /* 0 in these 3 bits is a POP instruction. */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000570 insn->vectorExtensionType = TYPE_XOP;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000571 else
Craig Topper9e3e38a2013-10-03 05:17:48 +0000572 unconsumeByte(insn);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000573
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000574 if (insn->vectorExtensionType == TYPE_XOP) {
575 insn->vectorExtensionPrefix[0] = byte;
576 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
577 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000578
579 /* We simulate the REX prefix for simplicity's sake */
580
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000581 if (insn->mode == MODE_64BIT)
Craig Topper9e3e38a2013-10-03 05:17:48 +0000582 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000583 | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3)
584 | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2)
585 | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1)
586 | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000587
Craig Topper273515e2014-10-07 07:29:48 +0000588 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000589 default:
590 break;
591 case VEX_PREFIX_66:
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000592 insn->hasOpSize = true;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000593 break;
594 }
595
596 dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000597 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
598 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000599 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000600 } else if (isREX(insn, byte)) {
601 if (lookAtByte(insn, &nextByte))
602 return -1;
603 insn->rexPrefix = byte;
604 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
605 } else
606 unconsumeByte(insn);
Sean Callananc3fd5232011-03-15 01:23:15 +0000607
Sean Callanan04cc3072009-12-19 02:59:52 +0000608 if (insn->mode == MODE_16BIT) {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000609 insn->registerSize = (insn->hasOpSize ? 4 : 2);
610 insn->addressSize = (insn->hasAdSize ? 4 : 2);
611 insn->displacementSize = (insn->hasAdSize ? 4 : 2);
612 insn->immediateSize = (insn->hasOpSize ? 4 : 2);
Sean Callanan04cc3072009-12-19 02:59:52 +0000613 } else if (insn->mode == MODE_32BIT) {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000614 insn->registerSize = (insn->hasOpSize ? 2 : 4);
615 insn->addressSize = (insn->hasAdSize ? 2 : 4);
616 insn->displacementSize = (insn->hasAdSize ? 2 : 4);
617 insn->immediateSize = (insn->hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000618 } else if (insn->mode == MODE_64BIT) {
619 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
620 insn->registerSize = 8;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000621 insn->addressSize = (insn->hasAdSize ? 4 : 8);
Sean Callanan04cc3072009-12-19 02:59:52 +0000622 insn->displacementSize = 4;
623 insn->immediateSize = 4;
Sean Callanan04cc3072009-12-19 02:59:52 +0000624 } else {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000625 insn->registerSize = (insn->hasOpSize ? 2 : 4);
626 insn->addressSize = (insn->hasAdSize ? 4 : 8);
627 insn->displacementSize = (insn->hasOpSize ? 2 : 4);
628 insn->immediateSize = (insn->hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000629 }
630 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000631
Sean Callanan04cc3072009-12-19 02:59:52 +0000632 return 0;
633}
634
Rafael Aulerde9ad4b2018-02-15 21:20:31 +0000635static int readModRM(struct InternalInstruction* insn);
636
Sean Callanan04cc3072009-12-19 02:59:52 +0000637/*
638 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
639 * extended or escape opcodes).
640 *
641 * @param insn - The instruction whose opcode is to be read.
642 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
643 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000644static int readOpcode(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000645 /* Determine the length of the primary opcode */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000646
Sean Callanan04cc3072009-12-19 02:59:52 +0000647 uint8_t current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000648
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000649 dbgprintf(insn, "readOpcode()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000650
Sean Callanan04cc3072009-12-19 02:59:52 +0000651 insn->opcodeType = ONEBYTE;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000652
Craig Topper273515e2014-10-07 07:29:48 +0000653 if (insn->vectorExtensionType == TYPE_EVEX) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000654 switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000655 default:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000656 dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
657 mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000658 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +0000659 case VEX_LOB_0F:
Sean Callananc3fd5232011-03-15 01:23:15 +0000660 insn->opcodeType = TWOBYTE;
661 return consumeByte(insn, &insn->opcode);
662 case VEX_LOB_0F38:
Sean Callananc3fd5232011-03-15 01:23:15 +0000663 insn->opcodeType = THREEBYTE_38;
664 return consumeByte(insn, &insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000665 case VEX_LOB_0F3A:
Sean Callananc3fd5232011-03-15 01:23:15 +0000666 insn->opcodeType = THREEBYTE_3A;
667 return consumeByte(insn, &insn->opcode);
668 }
Craig Topper273515e2014-10-07 07:29:48 +0000669 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000670 switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
671 default:
672 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
673 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
674 return -1;
675 case VEX_LOB_0F:
676 insn->opcodeType = TWOBYTE;
677 return consumeByte(insn, &insn->opcode);
678 case VEX_LOB_0F38:
679 insn->opcodeType = THREEBYTE_38;
680 return consumeByte(insn, &insn->opcode);
681 case VEX_LOB_0F3A:
682 insn->opcodeType = THREEBYTE_3A;
683 return consumeByte(insn, &insn->opcode);
684 }
Craig Topper273515e2014-10-07 07:29:48 +0000685 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000686 insn->opcodeType = TWOBYTE;
687 return consumeByte(insn, &insn->opcode);
Craig Topper273515e2014-10-07 07:29:48 +0000688 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000689 switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000690 default:
691 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000692 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
Craig Topper9e3e38a2013-10-03 05:17:48 +0000693 return -1;
694 case XOP_MAP_SELECT_8:
695 insn->opcodeType = XOP8_MAP;
696 return consumeByte(insn, &insn->opcode);
697 case XOP_MAP_SELECT_9:
698 insn->opcodeType = XOP9_MAP;
699 return consumeByte(insn, &insn->opcode);
700 case XOP_MAP_SELECT_A:
701 insn->opcodeType = XOPA_MAP;
702 return consumeByte(insn, &insn->opcode);
703 }
704 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000705
Sean Callanan04cc3072009-12-19 02:59:52 +0000706 if (consumeByte(insn, &current))
707 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000708
Sean Callanan04cc3072009-12-19 02:59:52 +0000709 if (current == 0x0f) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000710 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000711
Sean Callanan04cc3072009-12-19 02:59:52 +0000712 if (consumeByte(insn, &current))
713 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000714
Sean Callanan04cc3072009-12-19 02:59:52 +0000715 if (current == 0x38) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000716 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000717
Sean Callanan04cc3072009-12-19 02:59:52 +0000718 if (consumeByte(insn, &current))
719 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000720
Sean Callanan04cc3072009-12-19 02:59:52 +0000721 insn->opcodeType = THREEBYTE_38;
722 } else if (current == 0x3a) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000723 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000724
Sean Callanan04cc3072009-12-19 02:59:52 +0000725 if (consumeByte(insn, &current))
726 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000727
Sean Callanan04cc3072009-12-19 02:59:52 +0000728 insn->opcodeType = THREEBYTE_3A;
Craig Topper097b47a2018-03-24 07:48:54 +0000729 } else if (current == 0x0f) {
730 dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current);
731
732 // Consume operands before the opcode to comply with the 3DNow encoding
733 if (readModRM(insn))
734 return -1;
735
736 if (consumeByte(insn, &current))
737 return -1;
738
739 insn->opcodeType = THREEDNOW_MAP;
Sean Callanan04cc3072009-12-19 02:59:52 +0000740 } else {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000741 dbgprintf(insn, "Didn't find a three-byte escape prefix");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000742
Sean Callanan04cc3072009-12-19 02:59:52 +0000743 insn->opcodeType = TWOBYTE;
744 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000745 } else if (insn->mandatoryPrefix)
746 // The opcode with mandatory prefix must start with opcode escape.
747 // If not it's legacy repeat prefix
748 insn->mandatoryPrefix = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000749
Sean Callanan04cc3072009-12-19 02:59:52 +0000750 /*
751 * At this point we have consumed the full opcode.
752 * Anything we consume from here on must be unconsumed.
753 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000754
Sean Callanan04cc3072009-12-19 02:59:52 +0000755 insn->opcode = current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000756
Sean Callanan04cc3072009-12-19 02:59:52 +0000757 return 0;
758}
759
Sean Callanan04cc3072009-12-19 02:59:52 +0000760/*
761 * getIDWithAttrMask - Determines the ID of an instruction, consuming
762 * the ModR/M byte as appropriate for extended and escape opcodes,
763 * and using a supplied attribute mask.
764 *
765 * @param instructionID - A pointer whose target is filled in with the ID of the
766 * instruction.
767 * @param insn - The instruction whose ID is to be determined.
768 * @param attrMask - The attribute mask to search.
769 * @return - 0 if the ModR/M could be read when needed or was not
770 * needed; nonzero otherwise.
771 */
772static int getIDWithAttrMask(uint16_t* instructionID,
773 struct InternalInstruction* insn,
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000774 uint16_t attrMask) {
Richard Smith5d5061032014-04-20 22:15:37 +0000775 bool hasModRMExtension;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000776
Richard Smith89ee75d2014-04-20 21:07:34 +0000777 InstructionContext instructionClass = contextForAttrs(attrMask);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000778
Sean Callanan04cc3072009-12-19 02:59:52 +0000779 hasModRMExtension = modRMRequired(insn->opcodeType,
780 instructionClass,
781 insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000782
Sean Callanan04cc3072009-12-19 02:59:52 +0000783 if (hasModRMExtension) {
Rafael Espindola9f9a1062011-01-06 16:48:42 +0000784 if (readModRM(insn))
785 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000786
Sean Callanan04cc3072009-12-19 02:59:52 +0000787 *instructionID = decode(insn->opcodeType,
788 instructionClass,
789 insn->opcode,
790 insn->modRM);
791 } else {
792 *instructionID = decode(insn->opcodeType,
793 instructionClass,
794 insn->opcode,
795 0);
796 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000797
Sean Callanan04cc3072009-12-19 02:59:52 +0000798 return 0;
799}
800
801/*
802 * is16BitEquivalent - Determines whether two instruction names refer to
803 * equivalent instructions but one is 16-bit whereas the other is not.
804 *
805 * @param orig - The instruction that is not 16-bit
806 * @param equiv - The instruction that is 16-bit
807 */
Mehdi Amini36d33fc2016-10-01 06:46:33 +0000808static bool is16BitEquivalent(const char *orig, const char *equiv) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000809 off_t i;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000810
Sean Callanan010b3732010-04-02 21:23:51 +0000811 for (i = 0;; i++) {
812 if (orig[i] == '\0' && equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000813 return true;
Sean Callanan010b3732010-04-02 21:23:51 +0000814 if (orig[i] == '\0' || equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000815 return false;
Sean Callanan010b3732010-04-02 21:23:51 +0000816 if (orig[i] != equiv[i]) {
817 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan04cc3072009-12-19 02:59:52 +0000818 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000819 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan04cc3072009-12-19 02:59:52 +0000820 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000821 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan04cc3072009-12-19 02:59:52 +0000822 continue;
Richard Smith5d5061032014-04-20 22:15:37 +0000823 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000824 }
825 }
826}
827
828/*
Craig Topper0676b902014-10-07 07:29:50 +0000829 * is64Bit - Determines whether this instruction is a 64-bit instruction.
830 *
831 * @param name - The instruction that is not 16-bit
832 */
Mehdi Amini36d33fc2016-10-01 06:46:33 +0000833static bool is64Bit(const char *name) {
Craig Topper0676b902014-10-07 07:29:50 +0000834 off_t i;
835
836 for (i = 0;; ++i) {
837 if (name[i] == '\0')
838 return false;
839 if (name[i] == '6' && name[i+1] == '4')
840 return true;
841 }
842}
843
844/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000845 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
846 * appropriate for extended and escape opcodes. Determines the attributes and
Sean Callanan04cc3072009-12-19 02:59:52 +0000847 * context for the instruction before doing so.
848 *
849 * @param insn - The instruction whose ID is to be determined.
850 * @return - 0 if the ModR/M could be read when needed or was not needed;
851 * nonzero otherwise.
852 */
Roman Divacky67923802012-09-05 21:17:34 +0000853static int getID(struct InternalInstruction* insn, const void *miiArg) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000854 uint16_t attrMask;
Sean Callanan04cc3072009-12-19 02:59:52 +0000855 uint16_t instructionID;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000856
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000857 dbgprintf(insn, "getID()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000858
Sean Callanan04cc3072009-12-19 02:59:52 +0000859 attrMask = ATTR_NONE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000860
Sean Callanan04cc3072009-12-19 02:59:52 +0000861 if (insn->mode == MODE_64BIT)
862 attrMask |= ATTR_64BIT;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000863
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000864 if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
865 attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX;
Sean Callananc3fd5232011-03-15 01:23:15 +0000866
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000867 if (insn->vectorExtensionType == TYPE_EVEX) {
868 switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000869 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000870 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000871 break;
872 case VEX_PREFIX_F3:
873 attrMask |= ATTR_XS;
874 break;
875 case VEX_PREFIX_F2:
876 attrMask |= ATTR_XD;
877 break;
878 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000879
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000880 if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
881 attrMask |= ATTR_EVEXKZ;
882 if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
883 attrMask |= ATTR_EVEXB;
884 if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
885 attrMask |= ATTR_EVEXK;
886 if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
887 attrMask |= ATTR_EVEXL;
888 if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
889 attrMask |= ATTR_EVEXL2;
Craig Topper273515e2014-10-07 07:29:48 +0000890 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000891 switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
892 case VEX_PREFIX_66:
893 attrMask |= ATTR_OPSIZE;
894 break;
895 case VEX_PREFIX_F3:
896 attrMask |= ATTR_XS;
897 break;
898 case VEX_PREFIX_F2:
899 attrMask |= ATTR_XD;
900 break;
901 }
902
903 if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000904 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000905 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000906 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000907 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000908 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000909 break;
910 case VEX_PREFIX_F3:
911 attrMask |= ATTR_XS;
912 break;
913 case VEX_PREFIX_F2:
914 attrMask |= ATTR_XD;
915 break;
916 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000917
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000918 if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
Craig Topper9e3e38a2013-10-03 05:17:48 +0000919 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000920 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000921 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000922 case VEX_PREFIX_66:
923 attrMask |= ATTR_OPSIZE;
924 break;
925 case VEX_PREFIX_F3:
926 attrMask |= ATTR_XS;
927 break;
928 case VEX_PREFIX_F2:
929 attrMask |= ATTR_XD;
930 break;
931 }
932
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000933 if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000934 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000935 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000936 return -1;
937 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000938 } else if (!insn->mandatoryPrefix) {
939 // If we don't have mandatory prefix we should use legacy prefixes here
940 if (insn->hasOpSize && (insn->mode != MODE_16BIT))
Sean Callananc3fd5232011-03-15 01:23:15 +0000941 attrMask |= ATTR_OPSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000942 if (insn->hasAdSize)
Craig Topper6491c802012-02-27 01:54:29 +0000943 attrMask |= ATTR_ADSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000944 if (insn->opcodeType == ONEBYTE) {
945 if (insn->repeatPrefix == 0xf3 && (insn->opcode == 0x90))
946 // Special support for PAUSE
947 attrMask |= ATTR_XS;
948 } else {
949 if (insn->repeatPrefix == 0xf2)
950 attrMask |= ATTR_XD;
951 else if (insn->repeatPrefix == 0xf3)
952 attrMask |= ATTR_XS;
953 }
954 } else {
955 switch (insn->mandatoryPrefix) {
956 case 0xf2:
Sean Callananc3fd5232011-03-15 01:23:15 +0000957 attrMask |= ATTR_XD;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000958 break;
959 case 0xf3:
960 attrMask |= ATTR_XS;
961 break;
962 case 0x66:
963 if (insn->mode != MODE_16BIT)
964 attrMask |= ATTR_OPSIZE;
965 break;
966 case 0x67:
967 attrMask |= ATTR_ADSIZE;
968 break;
969 }
Craig Topper665f7442018-04-05 18:20:14 +0000970
Sean Callananc3fd5232011-03-15 01:23:15 +0000971 }
972
Andrew V. Tischenkof94da592017-10-30 12:02:06 +0000973 if (insn->rexPrefix & 0x08) {
Craig Topperf18c8962011-10-04 06:30:42 +0000974 attrMask |= ATTR_REXW;
Andrew V. Tischenkof94da592017-10-30 12:02:06 +0000975 attrMask &= ~ATTR_ADSIZE;
976 }
Craig Topperf01f1b52011-11-06 23:04:08 +0000977
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000978 /*
979 * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
980 * of the AdSize prefix is inverted w.r.t. 32-bit mode.
981 */
Craig Topper6e518772014-12-31 07:07:11 +0000982 if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
983 insn->opcode == 0xE3)
984 attrMask ^= ATTR_ADSIZE;
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000985
Craig Toppercade6352018-08-13 22:06:28 +0000986 // If we're in 16-bit mode and this is one of the relative jumps and opsize
987 // prefix isn't present, we need to force the opsize attribute since the
988 // prefix is inverted relative to 32-bit mode.
989 if (insn->mode == MODE_16BIT && !insn->hasOpSize &&
990 insn->opcodeType == ONEBYTE &&
991 (insn->opcode == 0xE8 || insn->opcode == 0xE9))
992 attrMask |= ATTR_OPSIZE;
Vedant Kumarbf891b12015-08-26 16:20:29 +0000993
Craig Toppercade6352018-08-13 22:06:28 +0000994 if (insn->mode == MODE_16BIT && !insn->hasOpSize &&
995 insn->opcodeType == TWOBYTE &&
996 insn->opcode >= 0x80 && insn->opcode <= 0x8F)
997 attrMask |= ATTR_OPSIZE;
Vedant Kumarbf891b12015-08-26 16:20:29 +0000998
Craig Topper6e518772014-12-31 07:07:11 +0000999 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1000 return -1;
David Woodhouse9c74fdb2014-01-20 12:02:48 +00001001
Sean Callanan04cc3072009-12-19 02:59:52 +00001002 /* The following clauses compensate for limitations of the tables. */
Craig Topperf01f1b52011-11-06 23:04:08 +00001003
Craig Topper0676b902014-10-07 07:29:50 +00001004 if (insn->mode != MODE_64BIT &&
1005 insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1006 /*
1007 * The tables can't distinquish between cases where the W-bit is used to
1008 * select register size and cases where its a required part of the opcode.
1009 */
1010 if ((insn->vectorExtensionType == TYPE_EVEX &&
1011 wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1012 (insn->vectorExtensionType == TYPE_VEX_3B &&
1013 wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1014 (insn->vectorExtensionType == TYPE_XOP &&
1015 wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1016
1017 uint16_t instructionIDWithREXW;
1018 if (getIDWithAttrMask(&instructionIDWithREXW,
1019 insn, attrMask | ATTR_REXW)) {
1020 insn->instructionID = instructionID;
1021 insn->spec = specifierForUID(instructionID);
1022 return 0;
1023 }
1024
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001025 auto SpecName = GetInstrName(instructionIDWithREXW, miiArg);
Craig Topper0676b902014-10-07 07:29:50 +00001026 // If not a 64-bit instruction. Switch the opcode.
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001027 if (!is64Bit(SpecName.data())) {
Craig Topper0676b902014-10-07 07:29:50 +00001028 insn->instructionID = instructionIDWithREXW;
1029 insn->spec = specifierForUID(instructionIDWithREXW);
1030 return 0;
1031 }
1032 }
1033 }
1034
Craig Topper99bcab72014-12-31 07:07:31 +00001035 /*
Gabor Buellac8ded042018-05-01 10:01:16 +00001036 * Absolute moves, umonitor, and movdir64b need special handling.
Craig Topper99bcab72014-12-31 07:07:31 +00001037 * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1038 * inverted w.r.t.
1039 * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1040 * any position.
1041 */
Gabor Buella31fa8022018-04-20 18:42:47 +00001042 if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
Gabor Buellac8ded042018-05-01 10:01:16 +00001043 (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1044 (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
Craig Topper99bcab72014-12-31 07:07:31 +00001045 /* Make sure we observed the prefixes in any position. */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001046 if (insn->hasAdSize)
Craig Topper99bcab72014-12-31 07:07:31 +00001047 attrMask |= ATTR_ADSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001048 if (insn->hasOpSize)
Craig Topper99bcab72014-12-31 07:07:31 +00001049 attrMask |= ATTR_OPSIZE;
1050
1051 /* In 16-bit, invert the attributes. */
Gabor Buella31fa8022018-04-20 18:42:47 +00001052 if (insn->mode == MODE_16BIT) {
1053 attrMask ^= ATTR_ADSIZE;
Gabor Buellac8ded042018-05-01 10:01:16 +00001054
Gabor Buella31fa8022018-04-20 18:42:47 +00001055 /* The OpSize attribute is only valid with the absolute moves. */
1056 if (insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0))
1057 attrMask ^= ATTR_OPSIZE;
1058 }
Craig Topper99bcab72014-12-31 07:07:31 +00001059
1060 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1061 return -1;
1062
1063 insn->instructionID = instructionID;
1064 insn->spec = specifierForUID(instructionID);
1065 return 0;
1066 }
1067
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001068 if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
David Woodhouse5cf4c672014-01-20 12:02:35 +00001069 !(attrMask & ATTR_OPSIZE)) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001070 /*
1071 * The instruction tables make no distinction between instructions that
1072 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1073 * particular spot (i.e., many MMX operations). In general we're
1074 * conservative, but in the specific case where OpSize is present but not
1075 * in the right place we check if there's a 16-bit operation.
1076 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001077
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001078 const struct InstructionSpecifier *spec;
Sean Callanan04cc3072009-12-19 02:59:52 +00001079 uint16_t instructionIDWithOpsize;
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001080 llvm::StringRef specName, specWithOpSizeName;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001081
Sean Callanan04cc3072009-12-19 02:59:52 +00001082 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001083
Sean Callanan04cc3072009-12-19 02:59:52 +00001084 if (getIDWithAttrMask(&instructionIDWithOpsize,
1085 insn,
1086 attrMask | ATTR_OPSIZE)) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001087 /*
Sean Callanan04cc3072009-12-19 02:59:52 +00001088 * ModRM required with OpSize but not present; give up and return version
1089 * without OpSize set
1090 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001091
Sean Callanan04cc3072009-12-19 02:59:52 +00001092 insn->instructionID = instructionID;
1093 insn->spec = spec;
1094 return 0;
1095 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001096
Richard Smith89ee75d2014-04-20 21:07:34 +00001097 specName = GetInstrName(instructionID, miiArg);
1098 specWithOpSizeName = GetInstrName(instructionIDWithOpsize, miiArg);
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001099
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001100 if (is16BitEquivalent(specName.data(), specWithOpSizeName.data()) &&
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001101 (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001102 insn->instructionID = instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001103 insn->spec = specifierForUID(instructionIDWithOpsize);
Sean Callanan04cc3072009-12-19 02:59:52 +00001104 } else {
1105 insn->instructionID = instructionID;
1106 insn->spec = spec;
1107 }
1108 return 0;
1109 }
Craig Topper21c33652011-10-02 16:56:09 +00001110
1111 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1112 insn->rexPrefix & 0x01) {
1113 /*
1114 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1115 * it should decode as XCHG %r8, %eax.
1116 */
1117
1118 const struct InstructionSpecifier *spec;
1119 uint16_t instructionIDWithNewOpcode;
1120 const struct InstructionSpecifier *specWithNewOpcode;
1121
1122 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001123
Craig Topperb58a9662011-10-05 03:29:32 +00001124 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper21c33652011-10-02 16:56:09 +00001125 insn->opcode = 0x91;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001126
Craig Topper21c33652011-10-02 16:56:09 +00001127 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
1128 insn,
1129 attrMask)) {
1130 insn->opcode = 0x90;
1131
1132 insn->instructionID = instructionID;
1133 insn->spec = spec;
1134 return 0;
1135 }
1136
1137 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1138
Craig Topperb58a9662011-10-05 03:29:32 +00001139 /* Change back */
Craig Topper21c33652011-10-02 16:56:09 +00001140 insn->opcode = 0x90;
1141
1142 insn->instructionID = instructionIDWithNewOpcode;
1143 insn->spec = specWithNewOpcode;
1144
1145 return 0;
1146 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001147
Sean Callanan04cc3072009-12-19 02:59:52 +00001148 insn->instructionID = instructionID;
1149 insn->spec = specifierForUID(insn->instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001150
Sean Callanan04cc3072009-12-19 02:59:52 +00001151 return 0;
1152}
1153
1154/*
1155 * readSIB - Consumes the SIB byte to determine addressing information for an
1156 * instruction.
1157 *
1158 * @param insn - The instruction whose SIB byte is to be read.
1159 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
1160 */
1161static int readSIB(struct InternalInstruction* insn) {
Richard Smith89ee75d2014-04-20 21:07:34 +00001162 SIBBase sibBaseBase = SIB_BASE_NONE;
Sean Callanan04cc3072009-12-19 02:59:52 +00001163 uint8_t index, base;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001164
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001165 dbgprintf(insn, "readSIB()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001166
Sean Callanan04cc3072009-12-19 02:59:52 +00001167 if (insn->consumedSIB)
1168 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001169
Richard Smith5d5061032014-04-20 22:15:37 +00001170 insn->consumedSIB = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001171
Sean Callanan04cc3072009-12-19 02:59:52 +00001172 switch (insn->addressSize) {
1173 case 2:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001174 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan04cc3072009-12-19 02:59:52 +00001175 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001176 case 4:
Craig Topperca2382d2017-10-21 20:03:20 +00001177 insn->sibIndexBase = SIB_INDEX_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001178 sibBaseBase = SIB_BASE_EAX;
1179 break;
1180 case 8:
Craig Topperca2382d2017-10-21 20:03:20 +00001181 insn->sibIndexBase = SIB_INDEX_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001182 sibBaseBase = SIB_BASE_RAX;
1183 break;
1184 }
1185
1186 if (consumeByte(insn, &insn->sib))
1187 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001188
Sean Callanan04cc3072009-12-19 02:59:52 +00001189 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001190
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001191 if (index == 0x4) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001192 insn->sibIndex = SIB_INDEX_NONE;
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001193 } else {
Craig Topperca2382d2017-10-21 20:03:20 +00001194 insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
Sean Callanan04cc3072009-12-19 02:59:52 +00001195 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001196
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001197 insn->sibScale = 1 << scaleFromSIB(insn->sib);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001198
Sean Callanan04cc3072009-12-19 02:59:52 +00001199 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001200
Sean Callanan04cc3072009-12-19 02:59:52 +00001201 switch (base) {
1202 case 0x5:
Craig Topperfae5ac22014-02-17 10:03:43 +00001203 case 0xd:
Sean Callanan04cc3072009-12-19 02:59:52 +00001204 switch (modFromModRM(insn->modRM)) {
1205 case 0x0:
1206 insn->eaDisplacement = EA_DISP_32;
1207 insn->sibBase = SIB_BASE_NONE;
1208 break;
1209 case 0x1:
1210 insn->eaDisplacement = EA_DISP_8;
Craig Topperfae5ac22014-02-17 10:03:43 +00001211 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001212 break;
1213 case 0x2:
1214 insn->eaDisplacement = EA_DISP_32;
Craig Topperfae5ac22014-02-17 10:03:43 +00001215 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001216 break;
1217 case 0x3:
Sean Callanan010b3732010-04-02 21:23:51 +00001218 debug("Cannot have Mod = 0b11 and a SIB byte");
1219 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001220 }
1221 break;
1222 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001223 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001224 break;
1225 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001226
Sean Callanan04cc3072009-12-19 02:59:52 +00001227 return 0;
1228}
1229
1230/*
1231 * readDisplacement - Consumes the displacement of an instruction.
1232 *
1233 * @param insn - The instruction whose displacement is to be read.
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001234 * @return - 0 if the displacement byte was successfully read; nonzero
Sean Callanan04cc3072009-12-19 02:59:52 +00001235 * otherwise.
1236 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001237static int readDisplacement(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001238 int8_t d8;
1239 int16_t d16;
1240 int32_t d32;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001241
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001242 dbgprintf(insn, "readDisplacement()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001243
Sean Callanan04cc3072009-12-19 02:59:52 +00001244 if (insn->consumedDisplacement)
1245 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001246
Richard Smith5d5061032014-04-20 22:15:37 +00001247 insn->consumedDisplacement = true;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001248 insn->displacementOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001249
Sean Callanan04cc3072009-12-19 02:59:52 +00001250 switch (insn->eaDisplacement) {
1251 case EA_DISP_NONE:
Richard Smith5d5061032014-04-20 22:15:37 +00001252 insn->consumedDisplacement = false;
Sean Callanan04cc3072009-12-19 02:59:52 +00001253 break;
1254 case EA_DISP_8:
1255 if (consumeInt8(insn, &d8))
1256 return -1;
1257 insn->displacement = d8;
1258 break;
1259 case EA_DISP_16:
1260 if (consumeInt16(insn, &d16))
1261 return -1;
1262 insn->displacement = d16;
1263 break;
1264 case EA_DISP_32:
1265 if (consumeInt32(insn, &d32))
1266 return -1;
1267 insn->displacement = d32;
1268 break;
1269 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001270
Richard Smith5d5061032014-04-20 22:15:37 +00001271 insn->consumedDisplacement = true;
Sean Callanan04cc3072009-12-19 02:59:52 +00001272 return 0;
1273}
1274
1275/*
1276 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1277 * displacement) for an instruction and interprets it.
1278 *
1279 * @param insn - The instruction whose addressing information is to be read.
1280 * @return - 0 if the information was successfully read; nonzero otherwise.
1281 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001282static int readModRM(struct InternalInstruction* insn) {
Craig Topper5b1dd012018-06-01 04:29:34 +00001283 uint8_t mod, rm, reg, evexrm;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001284
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001285 dbgprintf(insn, "readModRM()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001286
Sean Callanan04cc3072009-12-19 02:59:52 +00001287 if (insn->consumedModRM)
1288 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001289
Rafael Espindola9f9a1062011-01-06 16:48:42 +00001290 if (consumeByte(insn, &insn->modRM))
1291 return -1;
Richard Smith5d5061032014-04-20 22:15:37 +00001292 insn->consumedModRM = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001293
Sean Callanan04cc3072009-12-19 02:59:52 +00001294 mod = modFromModRM(insn->modRM);
1295 rm = rmFromModRM(insn->modRM);
1296 reg = regFromModRM(insn->modRM);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001297
Sean Callanan04cc3072009-12-19 02:59:52 +00001298 /*
1299 * This goes by insn->registerSize to pick the correct register, which messes
1300 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1301 * fixupReg().
1302 */
1303 switch (insn->registerSize) {
1304 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001305 insn->regBase = MODRM_REG_AX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001306 insn->eaRegBase = EA_REG_AX;
1307 break;
1308 case 4:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001309 insn->regBase = MODRM_REG_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001310 insn->eaRegBase = EA_REG_EAX;
1311 break;
1312 case 8:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001313 insn->regBase = MODRM_REG_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001314 insn->eaRegBase = EA_REG_RAX;
1315 break;
1316 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001317
Sean Callanan04cc3072009-12-19 02:59:52 +00001318 reg |= rFromREX(insn->rexPrefix) << 3;
1319 rm |= bFromREX(insn->rexPrefix) << 3;
Craig Topper5b1dd012018-06-01 04:29:34 +00001320
1321 evexrm = 0;
Craig Topper0179c6d2018-06-01 00:10:36 +00001322 if (insn->vectorExtensionType == TYPE_EVEX && insn->mode == MODE_64BIT) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001323 reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
Craig Topper5b1dd012018-06-01 04:29:34 +00001324 evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001325 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001326
Sean Callanan04cc3072009-12-19 02:59:52 +00001327 insn->reg = (Reg)(insn->regBase + reg);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001328
Sean Callanan04cc3072009-12-19 02:59:52 +00001329 switch (insn->addressSize) {
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001330 case 2: {
1331 EABase eaBaseBase = EA_BASE_BX_SI;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001332
Sean Callanan04cc3072009-12-19 02:59:52 +00001333 switch (mod) {
1334 case 0x0:
1335 if (rm == 0x6) {
1336 insn->eaBase = EA_BASE_NONE;
1337 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001338 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001339 return -1;
1340 } else {
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001341 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001342 insn->eaDisplacement = EA_DISP_NONE;
1343 }
1344 break;
1345 case 0x1:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001346 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001347 insn->eaDisplacement = EA_DISP_8;
Craig Topper399e39e2014-01-25 22:48:43 +00001348 insn->displacementSize = 1;
Sean Callanan010b3732010-04-02 21:23:51 +00001349 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001350 return -1;
1351 break;
1352 case 0x2:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001353 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001354 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001355 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001356 return -1;
1357 break;
1358 case 0x3:
1359 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001360 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001361 return -1;
1362 break;
1363 }
1364 break;
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001365 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001366 case 4:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001367 case 8: {
1368 EABase eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001369
Sean Callanan04cc3072009-12-19 02:59:52 +00001370 switch (mod) {
1371 case 0x0:
1372 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
Douglas Katzman6dc13972015-05-13 22:44:52 +00001373 // In determining whether RIP-relative mode is used (rm=5),
1374 // or whether a SIB byte is present (rm=4),
1375 // the extension bits (REX.b and EVEX.x) are ignored.
1376 switch (rm & 7) {
1377 case 0x4: // SIB byte is present
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001378 insn->eaBase = (insn->addressSize == 4 ?
Sean Callanan04cc3072009-12-19 02:59:52 +00001379 EA_BASE_sib : EA_BASE_sib64);
Craig Topper38afbfd2014-03-20 05:56:00 +00001380 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001381 return -1;
1382 break;
Douglas Katzman6dc13972015-05-13 22:44:52 +00001383 case 0x5: // RIP-relative
Sean Callanan04cc3072009-12-19 02:59:52 +00001384 insn->eaBase = EA_BASE_NONE;
1385 insn->eaDisplacement = EA_DISP_32;
Sean Callanan010b3732010-04-02 21:23:51 +00001386 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001387 return -1;
1388 break;
1389 default:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001390 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001391 break;
1392 }
1393 break;
1394 case 0x1:
Craig Topper399e39e2014-01-25 22:48:43 +00001395 insn->displacementSize = 1;
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +00001396 LLVM_FALLTHROUGH;
Sean Callanan04cc3072009-12-19 02:59:52 +00001397 case 0x2:
1398 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
Douglas Katzman6dc13972015-05-13 22:44:52 +00001399 switch (rm & 7) {
1400 case 0x4: // SIB byte is present
Sean Callanan04cc3072009-12-19 02:59:52 +00001401 insn->eaBase = EA_BASE_sib;
Craig Topper38afbfd2014-03-20 05:56:00 +00001402 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001403 return -1;
1404 break;
1405 default:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001406 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001407 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001408 return -1;
1409 break;
1410 }
1411 break;
1412 case 0x3:
1413 insn->eaDisplacement = EA_DISP_NONE;
Craig Topper5b1dd012018-06-01 04:29:34 +00001414 insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001415 break;
1416 }
1417 break;
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001418 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001419 } /* switch (insn->addressSize) */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001420
Sean Callanan04cc3072009-12-19 02:59:52 +00001421 return 0;
1422}
1423
Craig Topperc3cf55b2018-06-01 06:11:29 +00001424#define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
Ahmed Bougacha85dc93c2016-07-14 14:53:21 +00001425 static uint16_t name(struct InternalInstruction *insn, \
1426 OperandType type, \
1427 uint8_t index, \
1428 uint8_t *valid) { \
Sean Callanan04cc3072009-12-19 02:59:52 +00001429 *valid = 1; \
1430 switch (type) { \
1431 default: \
Sean Callanan010b3732010-04-02 21:23:51 +00001432 debug("Unhandled register type"); \
1433 *valid = 0; \
1434 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001435 case TYPE_Rv: \
1436 return base + index; \
1437 case TYPE_R8: \
Craig Topperc3cf55b2018-06-01 06:11:29 +00001438 index &= mask; \
1439 if (index > 0xf) \
1440 *valid = 0; \
Sean Callanan010b3732010-04-02 21:23:51 +00001441 if (insn->rexPrefix && \
Sean Callanan04cc3072009-12-19 02:59:52 +00001442 index >= 4 && index <= 7) { \
1443 return prefix##_SPL + (index - 4); \
1444 } else { \
1445 return prefix##_AL + index; \
1446 } \
1447 case TYPE_R16: \
Craig Topperc3cf55b2018-06-01 06:11:29 +00001448 index &= mask; \
1449 if (index > 0xf) \
1450 *valid = 0; \
1451 return prefix##_AX + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001452 case TYPE_R32: \
Craig Topperc3cf55b2018-06-01 06:11:29 +00001453 index &= mask; \
1454 if (index > 0xf) \
1455 *valid = 0; \
1456 return prefix##_EAX + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001457 case TYPE_R64: \
Craig Topperc3cf55b2018-06-01 06:11:29 +00001458 index &= mask; \
1459 if (index > 0xf) \
1460 *valid = 0; \
1461 return prefix##_RAX + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001462 case TYPE_ZMM: \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001463 return prefix##_ZMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001464 case TYPE_YMM: \
Sean Callananc3fd5232011-03-15 01:23:15 +00001465 return prefix##_YMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001466 case TYPE_XMM: \
Sean Callanan04cc3072009-12-19 02:59:52 +00001467 return prefix##_XMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001468 case TYPE_VK: \
Craig Topper0838c4d2018-06-01 05:36:08 +00001469 index &= 0xf; \
Craig Topper9c26bcc2015-03-02 03:33:11 +00001470 if (index > 7) \
1471 *valid = 0; \
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001472 return prefix##_K0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001473 case TYPE_MM64: \
Craig Topperd5b39232014-12-26 18:19:44 +00001474 return prefix##_MM0 + (index & 0x7); \
Sean Callanan04cc3072009-12-19 02:59:52 +00001475 case TYPE_SEGMENTREG: \
Andrew V. Tischenkoeff4fc02017-10-23 09:36:33 +00001476 if ((index & 7) > 5) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001477 *valid = 0; \
Andrew V. Tischenkoeff4fc02017-10-23 09:36:33 +00001478 return prefix##_ES + (index & 7); \
Sean Callanan04cc3072009-12-19 02:59:52 +00001479 case TYPE_DEBUGREG: \
Sean Callanan04cc3072009-12-19 02:59:52 +00001480 return prefix##_DR0 + index; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001481 case TYPE_CONTROLREG: \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001482 return prefix##_CR0 + index; \
Ahmed Bougacha85dc93c2016-07-14 14:53:21 +00001483 case TYPE_BNDR: \
1484 if (index > 3) \
1485 *valid = 0; \
1486 return prefix##_BND0 + index; \
Craig Topperca2382d2017-10-21 20:03:20 +00001487 case TYPE_MVSIBX: \
1488 return prefix##_XMM0 + index; \
1489 case TYPE_MVSIBY: \
1490 return prefix##_YMM0 + index; \
1491 case TYPE_MVSIBZ: \
1492 return prefix##_ZMM0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001493 } \
1494 }
1495
1496/*
1497 * fixup*Value - Consults an operand type to determine the meaning of the
1498 * reg or R/M field. If the operand is an XMM operand, for example, an
1499 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1500 * misinterpret it as.
1501 *
1502 * @param insn - The instruction containing the operand.
1503 * @param type - The operand type.
1504 * @param index - The existing value of the field as reported by readModRM().
1505 * @param valid - The address of a uint8_t. The target is set to 1 if the
1506 * field is valid for the register class; 0 if not.
Sean Callanan010b3732010-04-02 21:23:51 +00001507 * @return - The proper value.
Sean Callanan04cc3072009-12-19 02:59:52 +00001508 */
Craig Topperc3cf55b2018-06-01 06:11:29 +00001509GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1510GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
Sean Callanan04cc3072009-12-19 02:59:52 +00001511
1512/*
1513 * fixupReg - Consults an operand specifier to determine which of the
1514 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1515 *
1516 * @param insn - See fixup*Value().
1517 * @param op - The operand specifier.
1518 * @return - 0 if fixup was successful; -1 if the register returned was
1519 * invalid for its class.
1520 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001521static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001522 const struct OperandSpecifier *op) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001523 uint8_t valid;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001524
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001525 dbgprintf(insn, "fixupReg()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001526
Sean Callanan04cc3072009-12-19 02:59:52 +00001527 switch ((OperandEncoding)op->encoding) {
1528 default:
Sean Callanan010b3732010-04-02 21:23:51 +00001529 debug("Expected a REG or R/M encoding in fixupReg");
1530 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +00001531 case ENCODING_VVVV:
1532 insn->vvvv = (Reg)fixupRegValue(insn,
1533 (OperandType)op->type,
1534 insn->vvvv,
1535 &valid);
1536 if (!valid)
1537 return -1;
1538 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001539 case ENCODING_REG:
1540 insn->reg = (Reg)fixupRegValue(insn,
1541 (OperandType)op->type,
1542 insn->reg - insn->regBase,
1543 &valid);
1544 if (!valid)
1545 return -1;
1546 break;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001547 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001548 if (insn->eaBase >= insn->eaRegBase) {
1549 insn->eaBase = (EABase)fixupRMValue(insn,
1550 (OperandType)op->type,
1551 insn->eaBase - insn->eaRegBase,
1552 &valid);
1553 if (!valid)
1554 return -1;
1555 }
1556 break;
1557 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001558
Sean Callanan04cc3072009-12-19 02:59:52 +00001559 return 0;
1560}
1561
1562/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001563 * readOpcodeRegister - Reads an operand from the opcode field of an
Sean Callanan04cc3072009-12-19 02:59:52 +00001564 * instruction and interprets it appropriately given the operand width.
1565 * Handles AddRegFrm instructions.
1566 *
Craig Topper91551182014-01-01 15:29:32 +00001567 * @param insn - the instruction whose opcode field is to be read.
Sean Callanan04cc3072009-12-19 02:59:52 +00001568 * @param size - The width (in bytes) of the register being specified.
1569 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1570 * RAX.
Sean Callanan010b3732010-04-02 21:23:51 +00001571 * @return - 0 on success; nonzero otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +00001572 */
Sean Callanan010b3732010-04-02 21:23:51 +00001573static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001574 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001575
Sean Callanan04cc3072009-12-19 02:59:52 +00001576 if (size == 0)
1577 size = insn->registerSize;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001578
Sean Callanan04cc3072009-12-19 02:59:52 +00001579 switch (size) {
1580 case 1:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001581 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001582 | (insn->opcode & 7)));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001583 if (insn->rexPrefix &&
Sean Callanan010b3732010-04-02 21:23:51 +00001584 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1585 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan2f9443f2009-12-22 02:07:42 +00001586 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1587 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan04cc3072009-12-19 02:59:52 +00001588 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001589
Sean Callanan04cc3072009-12-19 02:59:52 +00001590 break;
1591 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001592 insn->opcodeRegister = (Reg)(MODRM_REG_AX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001593 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001594 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001595 break;
1596 case 4:
Sean Callanan010b3732010-04-02 21:23:51 +00001597 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001598 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001599 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001600 break;
1601 case 8:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001602 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1603 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001604 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001605 break;
1606 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001607
Sean Callanan010b3732010-04-02 21:23:51 +00001608 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001609}
1610
1611/*
1612 * readImmediate - Consumes an immediate operand from an instruction, given the
1613 * desired operand size.
1614 *
1615 * @param insn - The instruction whose operand is to be read.
1616 * @param size - The width (in bytes) of the operand.
1617 * @return - 0 if the immediate was successfully consumed; nonzero
1618 * otherwise.
1619 */
1620static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1621 uint8_t imm8;
1622 uint16_t imm16;
1623 uint32_t imm32;
1624 uint64_t imm64;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001625
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001626 dbgprintf(insn, "readImmediate()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001627
Sean Callanan010b3732010-04-02 21:23:51 +00001628 if (insn->numImmediatesConsumed == 2) {
1629 debug("Already consumed two immediates");
1630 return -1;
1631 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001632
Sean Callanan04cc3072009-12-19 02:59:52 +00001633 if (size == 0)
1634 size = insn->immediateSize;
1635 else
1636 insn->immediateSize = size;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001637 insn->immediateOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001638
Sean Callanan04cc3072009-12-19 02:59:52 +00001639 switch (size) {
1640 case 1:
1641 if (consumeByte(insn, &imm8))
1642 return -1;
1643 insn->immediates[insn->numImmediatesConsumed] = imm8;
1644 break;
1645 case 2:
1646 if (consumeUInt16(insn, &imm16))
1647 return -1;
1648 insn->immediates[insn->numImmediatesConsumed] = imm16;
1649 break;
1650 case 4:
1651 if (consumeUInt32(insn, &imm32))
1652 return -1;
1653 insn->immediates[insn->numImmediatesConsumed] = imm32;
1654 break;
1655 case 8:
1656 if (consumeUInt64(insn, &imm64))
1657 return -1;
1658 insn->immediates[insn->numImmediatesConsumed] = imm64;
1659 break;
1660 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001661
Sean Callanan04cc3072009-12-19 02:59:52 +00001662 insn->numImmediatesConsumed++;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001663
Sean Callanan04cc3072009-12-19 02:59:52 +00001664 return 0;
1665}
1666
1667/*
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001668 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callananc3fd5232011-03-15 01:23:15 +00001669 *
1670 * @param insn - The instruction whose operand is to be read.
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001671 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callananc3fd5232011-03-15 01:23:15 +00001672 * otherwise.
1673 */
1674static int readVVVV(struct InternalInstruction* insn) {
1675 dbgprintf(insn, "readVVVV()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001676
Richard Smith89ee75d2014-04-20 21:07:34 +00001677 int vvvv;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001678 if (insn->vectorExtensionType == TYPE_EVEX)
Adam Nemet8ae70502014-06-24 01:42:32 +00001679 vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1680 vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001681 else if (insn->vectorExtensionType == TYPE_VEX_3B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001682 vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001683 else if (insn->vectorExtensionType == TYPE_VEX_2B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001684 vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001685 else if (insn->vectorExtensionType == TYPE_XOP)
Richard Smith89ee75d2014-04-20 21:07:34 +00001686 vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +00001687 else
1688 return -1;
1689
Craig Topper0d0be472011-10-03 08:14:29 +00001690 if (insn->mode != MODE_64BIT)
Craig Topperdc5ba1e2018-06-01 01:23:52 +00001691 vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
Craig Topper0d0be472011-10-03 08:14:29 +00001692
Richard Smith89ee75d2014-04-20 21:07:34 +00001693 insn->vvvv = static_cast<Reg>(vvvv);
Sean Callananc3fd5232011-03-15 01:23:15 +00001694 return 0;
1695}
1696
1697/*
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001698 * readMaskRegister - Reads an mask register from the opcode field of an
1699 * instruction.
1700 *
1701 * @param insn - The instruction whose opcode field is to be read.
1702 * @return - 0 on success; nonzero otherwise.
1703 */
1704static int readMaskRegister(struct InternalInstruction* insn) {
1705 dbgprintf(insn, "readMaskRegister()");
1706
1707 if (insn->vectorExtensionType != TYPE_EVEX)
1708 return -1;
1709
Richard Smith89ee75d2014-04-20 21:07:34 +00001710 insn->writemask =
1711 static_cast<Reg>(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001712 return 0;
1713}
1714
1715/*
Sean Callanan04cc3072009-12-19 02:59:52 +00001716 * readOperands - Consults the specifier for an instruction and consumes all
1717 * operands for that instruction, interpreting them as it goes.
1718 *
1719 * @param insn - The instruction whose operands are to be read and interpreted.
1720 * @return - 0 if all operands could be read; nonzero otherwise.
1721 */
1722static int readOperands(struct InternalInstruction* insn) {
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001723 int hasVVVV, needVVVV;
Craig Topper2ba766a2011-12-30 06:23:39 +00001724 int sawRegImm = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001725
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001726 dbgprintf(insn, "readOperands()");
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001727
1728 /* If non-zero vvvv specified, need to make sure one of the operands
1729 uses it. */
1730 hasVVVV = !readVVVV(insn);
1731 needVVVV = hasVVVV && (insn->vvvv != 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001732
Patrik Hagglund31998382014-04-28 12:12:27 +00001733 for (const auto &Op : x86OperandSets[insn->spec->operands]) {
1734 switch (Op.encoding) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001735 case ENCODING_NONE:
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001736 case ENCODING_SI:
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001737 case ENCODING_DI:
Sean Callanan04cc3072009-12-19 02:59:52 +00001738 break;
Craig Topper33ac0642017-01-16 05:44:25 +00001739 CASE_ENCODING_VSIB:
1740 // VSIB can use the V2 bit so check only the other bits.
1741 if (needVVVV)
1742 needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
Craig Topper3173a1f2017-01-16 05:44:33 +00001743 if (readModRM(insn))
1744 return -1;
Craig Topperca2382d2017-10-21 20:03:20 +00001745
Craig Topper158bc642017-10-22 04:32:30 +00001746 // Reject if SIB wasn't used.
1747 if (insn->eaBase != EA_BASE_sib && insn->eaBase != EA_BASE_sib64)
1748 return -1;
1749
Craig Topperca2382d2017-10-21 20:03:20 +00001750 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1751 if (insn->sibIndex == SIB_INDEX_NONE)
Craig Topperef813a52018-06-06 19:15:15 +00001752 insn->sibIndex = (SIBIndex)(insn->sibIndexBase + 4);
Craig Topperca2382d2017-10-21 20:03:20 +00001753
1754 // If EVEX.v2 is set this is one of the 16-31 registers.
Craig Topper0179c6d2018-06-01 00:10:36 +00001755 if (insn->vectorExtensionType == TYPE_EVEX && insn->mode == MODE_64BIT &&
Craig Topperca2382d2017-10-21 20:03:20 +00001756 v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1757 insn->sibIndex = (SIBIndex)(insn->sibIndex + 16);
1758
1759 // Adjust the index register to the correct size.
1760 switch ((OperandType)Op.type) {
1761 default:
1762 debug("Unhandled VSIB index type");
Craig Topper3173a1f2017-01-16 05:44:33 +00001763 return -1;
Craig Topperca2382d2017-10-21 20:03:20 +00001764 case TYPE_MVSIBX:
1765 insn->sibIndex = (SIBIndex)(SIB_INDEX_XMM0 +
1766 (insn->sibIndex - insn->sibIndexBase));
1767 break;
1768 case TYPE_MVSIBY:
1769 insn->sibIndex = (SIBIndex)(SIB_INDEX_YMM0 +
1770 (insn->sibIndex - insn->sibIndexBase));
1771 break;
1772 case TYPE_MVSIBZ:
1773 insn->sibIndex = (SIBIndex)(SIB_INDEX_ZMM0 +
1774 (insn->sibIndex - insn->sibIndexBase));
1775 break;
1776 }
1777
Craig Topper3173a1f2017-01-16 05:44:33 +00001778 // Apply the AVX512 compressed displacement scaling factor.
1779 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1780 insn->displacement *= 1 << (Op.encoding - ENCODING_VSIB);
1781 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001782 case ENCODING_REG:
Adam Nemet5933c2f2014-07-17 17:04:56 +00001783 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001784 if (readModRM(insn))
1785 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001786 if (fixupReg(insn, &Op))
Sean Callanan04cc3072009-12-19 02:59:52 +00001787 return -1;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001788 // Apply the AVX512 compressed displacement scaling factor.
1789 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1790 insn->displacement *= 1 << (Op.encoding - ENCODING_RM);
Sean Callanan04cc3072009-12-19 02:59:52 +00001791 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001792 case ENCODING_IB:
Craig Topper2ba766a2011-12-30 06:23:39 +00001793 if (sawRegImm) {
Benjamin Kramer9c48f262012-01-04 22:06:45 +00001794 /* Saw a register immediate so don't read again and instead split the
1795 previous immediate. FIXME: This is a hack. */
Benjamin Kramer47aecca2012-01-01 17:55:36 +00001796 insn->immediates[insn->numImmediatesConsumed] =
1797 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1798 ++insn->numImmediatesConsumed;
Craig Topper2ba766a2011-12-30 06:23:39 +00001799 break;
1800 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001801 if (readImmediate(insn, 1))
1802 return -1;
Craig Topperad944a12017-01-16 06:49:03 +00001803 if (Op.type == TYPE_XMM || Op.type == TYPE_YMM)
Craig Topper2ba766a2011-12-30 06:23:39 +00001804 sawRegImm = 1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001805 break;
1806 case ENCODING_IW:
1807 if (readImmediate(insn, 2))
1808 return -1;
1809 break;
1810 case ENCODING_ID:
1811 if (readImmediate(insn, 4))
1812 return -1;
1813 break;
1814 case ENCODING_IO:
1815 if (readImmediate(insn, 8))
1816 return -1;
1817 break;
1818 case ENCODING_Iv:
Sean Callanan010b3732010-04-02 21:23:51 +00001819 if (readImmediate(insn, insn->immediateSize))
1820 return -1;
Chris Lattnerd4758fc2010-04-16 21:15:15 +00001821 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001822 case ENCODING_Ia:
Sean Callanan010b3732010-04-02 21:23:51 +00001823 if (readImmediate(insn, insn->addressSize))
1824 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001825 break;
Craig Topper326008c2017-10-23 02:26:24 +00001826 case ENCODING_IRC:
1827 insn->RC = (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 1) |
1828 lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
1829 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001830 case ENCODING_RB:
Sean Callanan010b3732010-04-02 21:23:51 +00001831 if (readOpcodeRegister(insn, 1))
1832 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001833 break;
1834 case ENCODING_RW:
Sean Callanan010b3732010-04-02 21:23:51 +00001835 if (readOpcodeRegister(insn, 2))
1836 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001837 break;
1838 case ENCODING_RD:
Sean Callanan010b3732010-04-02 21:23:51 +00001839 if (readOpcodeRegister(insn, 4))
1840 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001841 break;
1842 case ENCODING_RO:
Sean Callanan010b3732010-04-02 21:23:51 +00001843 if (readOpcodeRegister(insn, 8))
1844 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001845 break;
1846 case ENCODING_Rv:
Sean Callanan010b3732010-04-02 21:23:51 +00001847 if (readOpcodeRegister(insn, 0))
1848 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001849 break;
Craig Topper623b0d62014-01-01 14:22:37 +00001850 case ENCODING_FP:
Sean Callananc3fd5232011-03-15 01:23:15 +00001851 break;
1852 case ENCODING_VVVV:
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001853 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1854 if (!hasVVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001855 return -1;
Craig Topperdc5ba1e2018-06-01 01:23:52 +00001856 if (insn->mode != MODE_64BIT)
1857 insn->vvvv = static_cast<Reg>(insn->vvvv & 0x7);
Patrik Hagglund31998382014-04-28 12:12:27 +00001858 if (fixupReg(insn, &Op))
Sean Callananc3fd5232011-03-15 01:23:15 +00001859 return -1;
1860 break;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001861 case ENCODING_WRITEMASK:
1862 if (readMaskRegister(insn))
1863 return -1;
1864 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001865 case ENCODING_DUP:
1866 break;
1867 default:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001868 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan04cc3072009-12-19 02:59:52 +00001869 return -1;
1870 }
1871 }
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001872
1873 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1874 if (needVVVV) return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001875
Sean Callanan04cc3072009-12-19 02:59:52 +00001876 return 0;
1877}
1878
1879/*
1880 * decodeInstruction - Reads and interprets a full instruction provided by the
1881 * user.
1882 *
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001883 * @param insn - A pointer to the instruction to be populated. Must be
Sean Callanan04cc3072009-12-19 02:59:52 +00001884 * pre-allocated.
1885 * @param reader - The function to be used to read the instruction's bytes.
1886 * @param readerArg - A generic argument to be passed to the reader to store
1887 * any internal state.
1888 * @param logger - If non-NULL, the function to be used to write log messages
1889 * and warnings.
1890 * @param loggerArg - A generic argument to be passed to the logger to store
1891 * any internal state.
1892 * @param startLoc - The address (in the reader's address space) of the first
1893 * byte in the instruction.
1894 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1895 * decode the instruction in.
1896 * @return - 0 if the instruction's memory could be read; nonzero if
1897 * not.
1898 */
Richard Smith89ee75d2014-04-20 21:07:34 +00001899int llvm::X86Disassembler::decodeInstruction(
1900 struct InternalInstruction *insn, byteReader_t reader,
1901 const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg,
1902 uint64_t startLoc, DisassemblerMode mode) {
Daniel Dunbarc745a622009-12-19 03:31:50 +00001903 memset(insn, 0, sizeof(struct InternalInstruction));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001904
Sean Callanan04cc3072009-12-19 02:59:52 +00001905 insn->reader = reader;
1906 insn->readerArg = readerArg;
1907 insn->dlog = logger;
1908 insn->dlogArg = loggerArg;
1909 insn->startLocation = startLoc;
1910 insn->readerCursor = startLoc;
1911 insn->mode = mode;
1912 insn->numImmediatesConsumed = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001913
Sean Callanan04cc3072009-12-19 02:59:52 +00001914 if (readPrefixes(insn) ||
1915 readOpcode(insn) ||
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001916 getID(insn, miiArg) ||
Sean Callanan04cc3072009-12-19 02:59:52 +00001917 insn->instructionID == 0 ||
1918 readOperands(insn))
1919 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001920
Patrik Hagglund31998382014-04-28 12:12:27 +00001921 insn->operands = x86OperandSets[insn->spec->operands];
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001922
Sean Callanan04cc3072009-12-19 02:59:52 +00001923 insn->length = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001924
Benjamin Kramer4f672272010-03-18 12:18:36 +00001925 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1926 startLoc, insn->readerCursor, insn->length);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001927
Sean Callanan04cc3072009-12-19 02:59:52 +00001928 if (insn->length > 15)
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001929 dbgprintf(insn, "Instruction exceeds 15-byte limit");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001930
Sean Callanan04cc3072009-12-19 02:59:52 +00001931 return 0;
1932}