blob: e08c1a6e194cb8e5cb4c85178ca805ec61b3f2a8 [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) {
301 case 0xf2:
302 case 0xf3:
303 if (lookAtByte(insn, &nextByte))
304 break;
305 // TODO:
306 // 1. There could be several 0x66
307 // 2. if (nextByte == 0x66) and nextNextByte != 0x0f then
308 // it's not mandatory prefix
309 // 3. if (nextByte >= 0x40 && nextByte <= 0x4f) it's REX and we need
310 // 0x0f exactly after it to be mandatory prefix
311 if (isREX(insn, nextByte) || nextByte == 0x0f || nextByte == 0x66)
312 // The last of 0xf2 /0xf3 is mandatory prefix
313 insn->mandatoryPrefix = prefix;
314 insn->repeatPrefix = prefix;
315 break;
316 case 0x66:
317 if (lookAtByte(insn, &nextByte))
318 break;
319 // 0x66 can't overwrite existing mandatory prefix and should be ignored
320 if (!insn->mandatoryPrefix && (nextByte == 0x0f || isREX(insn, nextByte)))
321 insn->mandatoryPrefix = prefix;
322 break;
323 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000324}
325
326/*
327 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
328 * instruction as having them. Also sets the instruction's default operand,
329 * address, and other relevant data sizes to report operands correctly.
330 *
331 * @param insn - The instruction whose prefixes are to be read.
332 * @return - 0 if the instruction could be read until the end of the prefix
333 * bytes, and no prefixes conflicted; nonzero otherwise.
334 */
335static int readPrefixes(struct InternalInstruction* insn) {
Richard Smith5d5061032014-04-20 22:15:37 +0000336 bool isPrefix = true;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000337 uint8_t byte = 0;
Richard Mitton79917a92013-08-30 21:32:42 +0000338 uint8_t nextByte;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000339
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000340 dbgprintf(insn, "readPrefixes()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000341
Sean Callanan04cc3072009-12-19 02:59:52 +0000342 while (isPrefix) {
Richard Mitton576ee002013-08-30 21:19:48 +0000343 /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
Sean Callanan04cc3072009-12-19 02:59:52 +0000344 if (consumeByte(insn, &byte))
Richard Mitton576ee002013-08-30 21:19:48 +0000345 break;
Kevin Enderby014e1cd2012-03-09 17:52:49 +0000346
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000347 /*
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000348 * If the byte is a LOCK/REP/REPNE prefix and not a part of the opcode, then
349 * break and let it be disassembled as a normal "instruction".
Benjamin Krameradfc73d2012-03-10 15:10:06 +0000350 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000351 if (insn->readerCursor - 1 == insn->startLocation && byte == 0xf0) // LOCK
Richard Mitton576ee002013-08-30 21:19:48 +0000352 break;
353
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000354 if ((byte == 0xf2 || byte == 0xf3) && !lookAtByte(insn, &nextByte)) {
Kevin Enderby35fd7922013-06-20 22:32:18 +0000355 /*
356 * If the byte is 0xf2 or 0xf3, and any of the following conditions are
357 * met:
358 * - it is followed by a LOCK (0xf0) prefix
359 * - it is followed by an xchg instruction
360 * then it should be disassembled as a xacquire/xrelease not repne/rep.
361 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000362 if (((nextByte == 0xf0) ||
363 ((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90))) {
Richard Smith5d5061032014-04-20 22:15:37 +0000364 insn->xAcquireRelease = true;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000365 if (!(byte == 0xf3 && nextByte == 0x90)) // PAUSE instruction support
366 break;
367 }
Kevin Enderby35fd7922013-06-20 22:32:18 +0000368 /*
369 * Also if the byte is 0xf3, and the following condition is met:
370 * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
371 * "mov mem, imm" (opcode 0xc6/0xc7) instructions.
372 * then it should be disassembled as an xrelease not rep.
373 */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000374 if (byte == 0xf3 && (nextByte == 0x88 || nextByte == 0x89 ||
375 nextByte == 0xc6 || nextByte == 0xc7)) {
Richard Smith5d5061032014-04-20 22:15:37 +0000376 insn->xAcquireRelease = true;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000377 if (nextByte != 0x90) // PAUSE instruction support
378 break;
379 }
380 if (isREX(insn, nextByte)) {
381 uint8_t nnextByte;
382 // Go to REX prefix after the current one
383 if (consumeByte(insn, &nnextByte))
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000384 return -1;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000385 // We should be able to read next byte after REX prefix
386 if (lookAtByte(insn, &nnextByte))
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000387 return -1;
388 unconsumeByte(insn);
389 }
Dave Zarzycki07fabee2013-03-25 18:59:38 +0000390 }
391
Sean Callanan04cc3072009-12-19 02:59:52 +0000392 switch (byte) {
393 case 0xf0: /* LOCK */
394 case 0xf2: /* REPNE/REPNZ */
395 case 0xf3: /* REP or REPE/REPZ */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000396 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000397 break;
398 case 0x2e: /* CS segment override -OR- Branch not taken */
399 case 0x36: /* SS segment override -OR- Branch taken */
400 case 0x3e: /* DS segment override */
401 case 0x26: /* ES segment override */
402 case 0x64: /* FS segment override */
403 case 0x65: /* GS segment override */
404 switch (byte) {
405 case 0x2e:
406 insn->segmentOverride = SEG_OVERRIDE_CS;
407 break;
408 case 0x36:
409 insn->segmentOverride = SEG_OVERRIDE_SS;
410 break;
411 case 0x3e:
412 insn->segmentOverride = SEG_OVERRIDE_DS;
413 break;
414 case 0x26:
415 insn->segmentOverride = SEG_OVERRIDE_ES;
416 break;
417 case 0x64:
418 insn->segmentOverride = SEG_OVERRIDE_FS;
419 break;
420 case 0x65:
421 insn->segmentOverride = SEG_OVERRIDE_GS;
422 break;
423 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000424 debug("Unhandled override");
425 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000426 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000427 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000428 break;
429 case 0x66: /* Operand-size override */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000430 insn->hasOpSize = true;
431 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000432 break;
433 case 0x67: /* Address-size override */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000434 insn->hasAdSize = true;
435 setPrefixPresent(insn, byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000436 break;
437 default: /* Not a prefix byte */
Richard Smith5d5061032014-04-20 22:15:37 +0000438 isPrefix = false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000439 break;
440 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000441
Sean Callanan04cc3072009-12-19 02:59:52 +0000442 if (isPrefix)
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000443 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000444 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000445
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000446 insn->vectorExtensionType = TYPE_NO_VEX_XOP;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000447
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000448 if (byte == 0x62) {
449 uint8_t byte1, byte2;
450
451 if (consumeByte(insn, &byte1)) {
452 dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
453 return -1;
454 }
455
456 if (lookAtByte(insn, &byte2)) {
457 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
458 return -1;
459 }
460
461 if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
462 ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
463 insn->vectorExtensionType = TYPE_EVEX;
Craig Topper273515e2014-10-07 07:29:48 +0000464 } else {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000465 unconsumeByte(insn); /* unconsume byte1 */
466 unconsumeByte(insn); /* unconsume byte */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000467 }
468
469 if (insn->vectorExtensionType == TYPE_EVEX) {
470 insn->vectorExtensionPrefix[0] = byte;
471 insn->vectorExtensionPrefix[1] = byte1;
472 if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) {
473 dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
474 return -1;
475 }
476 if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) {
477 dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
478 return -1;
479 }
480
481 /* We simulate the REX prefix for simplicity's sake */
482 if (insn->mode == MODE_64BIT) {
483 insn->rexPrefix = 0x40
484 | (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3)
485 | (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2)
486 | (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1)
487 | (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
488 }
489
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000490 dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
491 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
492 insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
493 }
Craig Topper273515e2014-10-07 07:29:48 +0000494 } else if (byte == 0xc4) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000495 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000496
Sean Callananc3fd5232011-03-15 01:23:15 +0000497 if (lookAtByte(insn, &byte1)) {
498 dbgprintf(insn, "Couldn't read second byte of VEX");
499 return -1;
500 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000501
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000502 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000503 insn->vectorExtensionType = TYPE_VEX_3B;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000504 else
Sean Callanan04cc3072009-12-19 02:59:52 +0000505 unconsumeByte(insn);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000506
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000507 if (insn->vectorExtensionType == TYPE_VEX_3B) {
508 insn->vectorExtensionPrefix[0] = byte;
509 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
510 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000511
512 /* We simulate the REX prefix for simplicity's sake */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000513
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000514 if (insn->mode == MODE_64BIT)
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000515 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000516 | (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3)
517 | (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2)
518 | (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1)
519 | (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000520
Craig Topper9e3e38a2013-10-03 05:17:48 +0000521 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000522 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
523 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000524 }
Craig Topper273515e2014-10-07 07:29:48 +0000525 } else if (byte == 0xc5) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000526 uint8_t byte1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000527
Sean Callananc3fd5232011-03-15 01:23:15 +0000528 if (lookAtByte(insn, &byte1)) {
529 dbgprintf(insn, "Couldn't read second byte of VEX");
530 return -1;
531 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000532
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000533 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000534 insn->vectorExtensionType = TYPE_VEX_2B;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000535 else
Sean Callananc3fd5232011-03-15 01:23:15 +0000536 unconsumeByte(insn);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000537
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000538 if (insn->vectorExtensionType == TYPE_VEX_2B) {
539 insn->vectorExtensionPrefix[0] = byte;
540 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000541
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000542 if (insn->mode == MODE_64BIT)
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000543 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000544 | (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000545
Craig Topper273515e2014-10-07 07:29:48 +0000546 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000547 default:
548 break;
549 case VEX_PREFIX_66:
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000550 insn->hasOpSize = true;
Sean Callananc3fd5232011-03-15 01:23:15 +0000551 break;
552 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000553
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000554 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
555 insn->vectorExtensionPrefix[0],
556 insn->vectorExtensionPrefix[1]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000557 }
Craig Topper273515e2014-10-07 07:29:48 +0000558 } else if (byte == 0x8f) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000559 uint8_t byte1;
560
561 if (lookAtByte(insn, &byte1)) {
562 dbgprintf(insn, "Couldn't read second byte of XOP");
563 return -1;
564 }
565
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000566 if ((byte1 & 0x38) != 0x0) /* 0 in these 3 bits is a POP instruction. */
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000567 insn->vectorExtensionType = TYPE_XOP;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000568 else
Craig Topper9e3e38a2013-10-03 05:17:48 +0000569 unconsumeByte(insn);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000570
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000571 if (insn->vectorExtensionType == TYPE_XOP) {
572 insn->vectorExtensionPrefix[0] = byte;
573 consumeByte(insn, &insn->vectorExtensionPrefix[1]);
574 consumeByte(insn, &insn->vectorExtensionPrefix[2]);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000575
576 /* We simulate the REX prefix for simplicity's sake */
577
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000578 if (insn->mode == MODE_64BIT)
Craig Topper9e3e38a2013-10-03 05:17:48 +0000579 insn->rexPrefix = 0x40
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000580 | (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3)
581 | (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2)
582 | (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1)
583 | (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
Craig Topper9e3e38a2013-10-03 05:17:48 +0000584
Craig Topper273515e2014-10-07 07:29:48 +0000585 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000586 default:
587 break;
588 case VEX_PREFIX_66:
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000589 insn->hasOpSize = true;
Craig Topper9e3e38a2013-10-03 05:17:48 +0000590 break;
591 }
592
593 dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000594 insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
595 insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +0000596 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000597 } else if (isREX(insn, byte)) {
598 if (lookAtByte(insn, &nextByte))
599 return -1;
600 insn->rexPrefix = byte;
601 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
602 } else
603 unconsumeByte(insn);
Sean Callananc3fd5232011-03-15 01:23:15 +0000604
Sean Callanan04cc3072009-12-19 02:59:52 +0000605 if (insn->mode == MODE_16BIT) {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000606 insn->registerSize = (insn->hasOpSize ? 4 : 2);
607 insn->addressSize = (insn->hasAdSize ? 4 : 2);
608 insn->displacementSize = (insn->hasAdSize ? 4 : 2);
609 insn->immediateSize = (insn->hasOpSize ? 4 : 2);
Sean Callanan04cc3072009-12-19 02:59:52 +0000610 } else if (insn->mode == MODE_32BIT) {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000611 insn->registerSize = (insn->hasOpSize ? 2 : 4);
612 insn->addressSize = (insn->hasAdSize ? 2 : 4);
613 insn->displacementSize = (insn->hasAdSize ? 2 : 4);
614 insn->immediateSize = (insn->hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000615 } else if (insn->mode == MODE_64BIT) {
616 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
617 insn->registerSize = 8;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000618 insn->addressSize = (insn->hasAdSize ? 4 : 8);
Sean Callanan04cc3072009-12-19 02:59:52 +0000619 insn->displacementSize = 4;
620 insn->immediateSize = 4;
Sean Callanan04cc3072009-12-19 02:59:52 +0000621 } else {
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000622 insn->registerSize = (insn->hasOpSize ? 2 : 4);
623 insn->addressSize = (insn->hasAdSize ? 4 : 8);
624 insn->displacementSize = (insn->hasOpSize ? 2 : 4);
625 insn->immediateSize = (insn->hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000626 }
627 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000628
Sean Callanan04cc3072009-12-19 02:59:52 +0000629 return 0;
630}
631
Rafael Aulerde9ad4b2018-02-15 21:20:31 +0000632static int readModRM(struct InternalInstruction* insn);
633
Sean Callanan04cc3072009-12-19 02:59:52 +0000634/*
635 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
636 * extended or escape opcodes).
637 *
638 * @param insn - The instruction whose opcode is to be read.
639 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
640 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000641static int readOpcode(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000642 /* Determine the length of the primary opcode */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000643
Sean Callanan04cc3072009-12-19 02:59:52 +0000644 uint8_t current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000645
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000646 dbgprintf(insn, "readOpcode()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000647
Sean Callanan04cc3072009-12-19 02:59:52 +0000648 insn->opcodeType = ONEBYTE;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000649
Craig Topper273515e2014-10-07 07:29:48 +0000650 if (insn->vectorExtensionType == TYPE_EVEX) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000651 switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000652 default:
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000653 dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
654 mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000655 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +0000656 case VEX_LOB_0F:
Sean Callananc3fd5232011-03-15 01:23:15 +0000657 insn->opcodeType = TWOBYTE;
658 return consumeByte(insn, &insn->opcode);
659 case VEX_LOB_0F38:
Sean Callananc3fd5232011-03-15 01:23:15 +0000660 insn->opcodeType = THREEBYTE_38;
661 return consumeByte(insn, &insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000662 case VEX_LOB_0F3A:
Sean Callananc3fd5232011-03-15 01:23:15 +0000663 insn->opcodeType = THREEBYTE_3A;
664 return consumeByte(insn, &insn->opcode);
665 }
Craig Topper273515e2014-10-07 07:29:48 +0000666 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000667 switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
668 default:
669 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
670 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
671 return -1;
672 case VEX_LOB_0F:
673 insn->opcodeType = TWOBYTE;
674 return consumeByte(insn, &insn->opcode);
675 case VEX_LOB_0F38:
676 insn->opcodeType = THREEBYTE_38;
677 return consumeByte(insn, &insn->opcode);
678 case VEX_LOB_0F3A:
679 insn->opcodeType = THREEBYTE_3A;
680 return consumeByte(insn, &insn->opcode);
681 }
Craig Topper273515e2014-10-07 07:29:48 +0000682 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000683 insn->opcodeType = TWOBYTE;
684 return consumeByte(insn, &insn->opcode);
Craig Topper273515e2014-10-07 07:29:48 +0000685 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000686 switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000687 default:
688 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000689 mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
Craig Topper9e3e38a2013-10-03 05:17:48 +0000690 return -1;
691 case XOP_MAP_SELECT_8:
692 insn->opcodeType = XOP8_MAP;
693 return consumeByte(insn, &insn->opcode);
694 case XOP_MAP_SELECT_9:
695 insn->opcodeType = XOP9_MAP;
696 return consumeByte(insn, &insn->opcode);
697 case XOP_MAP_SELECT_A:
698 insn->opcodeType = XOPA_MAP;
699 return consumeByte(insn, &insn->opcode);
700 }
701 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000702
Sean Callanan04cc3072009-12-19 02:59:52 +0000703 if (consumeByte(insn, &current))
704 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000705
Sean Callanan04cc3072009-12-19 02:59:52 +0000706 if (current == 0x0f) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000707 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000708
Sean Callanan04cc3072009-12-19 02:59:52 +0000709 if (consumeByte(insn, &current))
710 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000711
Sean Callanan04cc3072009-12-19 02:59:52 +0000712 if (current == 0x38) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000713 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000714
Sean Callanan04cc3072009-12-19 02:59:52 +0000715 if (consumeByte(insn, &current))
716 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000717
Sean Callanan04cc3072009-12-19 02:59:52 +0000718 insn->opcodeType = THREEBYTE_38;
719 } else if (current == 0x3a) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000720 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000721
Sean Callanan04cc3072009-12-19 02:59:52 +0000722 if (consumeByte(insn, &current))
723 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000724
Sean Callanan04cc3072009-12-19 02:59:52 +0000725 insn->opcodeType = THREEBYTE_3A;
Craig Topper097b47a2018-03-24 07:48:54 +0000726 } else if (current == 0x0f) {
727 dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current);
728
729 // Consume operands before the opcode to comply with the 3DNow encoding
730 if (readModRM(insn))
731 return -1;
732
733 if (consumeByte(insn, &current))
734 return -1;
735
736 insn->opcodeType = THREEDNOW_MAP;
Sean Callanan04cc3072009-12-19 02:59:52 +0000737 } else {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000738 dbgprintf(insn, "Didn't find a three-byte escape prefix");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000739
Sean Callanan04cc3072009-12-19 02:59:52 +0000740 insn->opcodeType = TWOBYTE;
741 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000742 } else if (insn->mandatoryPrefix)
743 // The opcode with mandatory prefix must start with opcode escape.
744 // If not it's legacy repeat prefix
745 insn->mandatoryPrefix = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000746
Sean Callanan04cc3072009-12-19 02:59:52 +0000747 /*
748 * At this point we have consumed the full opcode.
749 * Anything we consume from here on must be unconsumed.
750 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000751
Sean Callanan04cc3072009-12-19 02:59:52 +0000752 insn->opcode = current;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000753
Sean Callanan04cc3072009-12-19 02:59:52 +0000754 return 0;
755}
756
Sean Callanan04cc3072009-12-19 02:59:52 +0000757/*
758 * getIDWithAttrMask - Determines the ID of an instruction, consuming
759 * the ModR/M byte as appropriate for extended and escape opcodes,
760 * and using a supplied attribute mask.
761 *
762 * @param instructionID - A pointer whose target is filled in with the ID of the
763 * instruction.
764 * @param insn - The instruction whose ID is to be determined.
765 * @param attrMask - The attribute mask to search.
766 * @return - 0 if the ModR/M could be read when needed or was not
767 * needed; nonzero otherwise.
768 */
769static int getIDWithAttrMask(uint16_t* instructionID,
770 struct InternalInstruction* insn,
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000771 uint16_t attrMask) {
Richard Smith5d5061032014-04-20 22:15:37 +0000772 bool hasModRMExtension;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000773
Richard Smith89ee75d2014-04-20 21:07:34 +0000774 InstructionContext instructionClass = contextForAttrs(attrMask);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000775
Sean Callanan04cc3072009-12-19 02:59:52 +0000776 hasModRMExtension = modRMRequired(insn->opcodeType,
777 instructionClass,
778 insn->opcode);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000779
Sean Callanan04cc3072009-12-19 02:59:52 +0000780 if (hasModRMExtension) {
Rafael Espindola9f9a1062011-01-06 16:48:42 +0000781 if (readModRM(insn))
782 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000783
Sean Callanan04cc3072009-12-19 02:59:52 +0000784 *instructionID = decode(insn->opcodeType,
785 instructionClass,
786 insn->opcode,
787 insn->modRM);
788 } else {
789 *instructionID = decode(insn->opcodeType,
790 instructionClass,
791 insn->opcode,
792 0);
793 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000794
Sean Callanan04cc3072009-12-19 02:59:52 +0000795 return 0;
796}
797
798/*
799 * is16BitEquivalent - Determines whether two instruction names refer to
800 * equivalent instructions but one is 16-bit whereas the other is not.
801 *
802 * @param orig - The instruction that is not 16-bit
803 * @param equiv - The instruction that is 16-bit
804 */
Mehdi Amini36d33fc2016-10-01 06:46:33 +0000805static bool is16BitEquivalent(const char *orig, const char *equiv) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000806 off_t i;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000807
Sean Callanan010b3732010-04-02 21:23:51 +0000808 for (i = 0;; i++) {
809 if (orig[i] == '\0' && equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000810 return true;
Sean Callanan010b3732010-04-02 21:23:51 +0000811 if (orig[i] == '\0' || equiv[i] == '\0')
Richard Smith5d5061032014-04-20 22:15:37 +0000812 return false;
Sean Callanan010b3732010-04-02 21:23:51 +0000813 if (orig[i] != equiv[i]) {
814 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan04cc3072009-12-19 02:59:52 +0000815 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000816 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan04cc3072009-12-19 02:59:52 +0000817 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000818 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan04cc3072009-12-19 02:59:52 +0000819 continue;
Richard Smith5d5061032014-04-20 22:15:37 +0000820 return false;
Sean Callanan04cc3072009-12-19 02:59:52 +0000821 }
822 }
823}
824
825/*
Craig Topper0676b902014-10-07 07:29:50 +0000826 * is64Bit - Determines whether this instruction is a 64-bit instruction.
827 *
828 * @param name - The instruction that is not 16-bit
829 */
Mehdi Amini36d33fc2016-10-01 06:46:33 +0000830static bool is64Bit(const char *name) {
Craig Topper0676b902014-10-07 07:29:50 +0000831 off_t i;
832
833 for (i = 0;; ++i) {
834 if (name[i] == '\0')
835 return false;
836 if (name[i] == '6' && name[i+1] == '4')
837 return true;
838 }
839}
840
841/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000842 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
843 * appropriate for extended and escape opcodes. Determines the attributes and
Sean Callanan04cc3072009-12-19 02:59:52 +0000844 * context for the instruction before doing so.
845 *
846 * @param insn - The instruction whose ID is to be determined.
847 * @return - 0 if the ModR/M could be read when needed or was not needed;
848 * nonzero otherwise.
849 */
Roman Divacky67923802012-09-05 21:17:34 +0000850static int getID(struct InternalInstruction* insn, const void *miiArg) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000851 uint16_t attrMask;
Sean Callanan04cc3072009-12-19 02:59:52 +0000852 uint16_t instructionID;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000853
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000854 dbgprintf(insn, "getID()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000855
Sean Callanan04cc3072009-12-19 02:59:52 +0000856 attrMask = ATTR_NONE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000857
Sean Callanan04cc3072009-12-19 02:59:52 +0000858 if (insn->mode == MODE_64BIT)
859 attrMask |= ATTR_64BIT;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000860
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000861 if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
862 attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ? ATTR_EVEX : ATTR_VEX;
Sean Callananc3fd5232011-03-15 01:23:15 +0000863
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000864 if (insn->vectorExtensionType == TYPE_EVEX) {
865 switch (ppFromEVEX3of4(insn->vectorExtensionPrefix[2])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000866 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000867 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000868 break;
869 case VEX_PREFIX_F3:
870 attrMask |= ATTR_XS;
871 break;
872 case VEX_PREFIX_F2:
873 attrMask |= ATTR_XD;
874 break;
875 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000876
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000877 if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
878 attrMask |= ATTR_EVEXKZ;
879 if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
880 attrMask |= ATTR_EVEXB;
881 if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
882 attrMask |= ATTR_EVEXK;
883 if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
884 attrMask |= ATTR_EVEXL;
885 if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
886 attrMask |= ATTR_EVEXL2;
Craig Topper273515e2014-10-07 07:29:48 +0000887 } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000888 switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
889 case VEX_PREFIX_66:
890 attrMask |= ATTR_OPSIZE;
891 break;
892 case VEX_PREFIX_F3:
893 attrMask |= ATTR_XS;
894 break;
895 case VEX_PREFIX_F2:
896 attrMask |= ATTR_XD;
897 break;
898 }
899
900 if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000901 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000902 } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000903 switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000904 case VEX_PREFIX_66:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000905 attrMask |= ATTR_OPSIZE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000906 break;
907 case VEX_PREFIX_F3:
908 attrMask |= ATTR_XS;
909 break;
910 case VEX_PREFIX_F2:
911 attrMask |= ATTR_XD;
912 break;
913 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +0000914
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000915 if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
Craig Topper9e3e38a2013-10-03 05:17:48 +0000916 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000917 } else if (insn->vectorExtensionType == TYPE_XOP) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000918 switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
Craig Topper9e3e38a2013-10-03 05:17:48 +0000919 case VEX_PREFIX_66:
920 attrMask |= ATTR_OPSIZE;
921 break;
922 case VEX_PREFIX_F3:
923 attrMask |= ATTR_XS;
924 break;
925 case VEX_PREFIX_F2:
926 attrMask |= ATTR_XD;
927 break;
928 }
929
Elena Demikhovsky371e3632013-12-25 11:40:51 +0000930 if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
Sean Callananc3fd5232011-03-15 01:23:15 +0000931 attrMask |= ATTR_VEXL;
Craig Topper273515e2014-10-07 07:29:48 +0000932 } else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000933 return -1;
934 }
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000935 } else if (!insn->mandatoryPrefix) {
936 // If we don't have mandatory prefix we should use legacy prefixes here
937 if (insn->hasOpSize && (insn->mode != MODE_16BIT))
Sean Callananc3fd5232011-03-15 01:23:15 +0000938 attrMask |= ATTR_OPSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000939 if (insn->hasAdSize)
Craig Topper6491c802012-02-27 01:54:29 +0000940 attrMask |= ATTR_ADSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000941 if (insn->opcodeType == ONEBYTE) {
942 if (insn->repeatPrefix == 0xf3 && (insn->opcode == 0x90))
943 // Special support for PAUSE
944 attrMask |= ATTR_XS;
945 } else {
946 if (insn->repeatPrefix == 0xf2)
947 attrMask |= ATTR_XD;
948 else if (insn->repeatPrefix == 0xf3)
949 attrMask |= ATTR_XS;
950 }
951 } else {
952 switch (insn->mandatoryPrefix) {
953 case 0xf2:
Sean Callananc3fd5232011-03-15 01:23:15 +0000954 attrMask |= ATTR_XD;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000955 break;
956 case 0xf3:
957 attrMask |= ATTR_XS;
958 break;
959 case 0x66:
960 if (insn->mode != MODE_16BIT)
961 attrMask |= ATTR_OPSIZE;
962 break;
963 case 0x67:
964 attrMask |= ATTR_ADSIZE;
965 break;
966 }
Craig Topper665f7442018-04-05 18:20:14 +0000967
Sean Callananc3fd5232011-03-15 01:23:15 +0000968 }
969
Andrew V. Tischenkof94da592017-10-30 12:02:06 +0000970 if (insn->rexPrefix & 0x08) {
Craig Topperf18c8962011-10-04 06:30:42 +0000971 attrMask |= ATTR_REXW;
Andrew V. Tischenkof94da592017-10-30 12:02:06 +0000972 attrMask &= ~ATTR_ADSIZE;
973 }
Craig Topperf01f1b52011-11-06 23:04:08 +0000974
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000975 /*
976 * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
977 * of the AdSize prefix is inverted w.r.t. 32-bit mode.
978 */
Craig Topper6e518772014-12-31 07:07:11 +0000979 if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
980 insn->opcode == 0xE3)
981 attrMask ^= ATTR_ADSIZE;
David Woodhouse9c74fdb2014-01-20 12:02:48 +0000982
Vedant Kumarbf891b12015-08-26 16:20:29 +0000983 /*
984 * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix
985 * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes
986 */
987
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +0000988 if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
Vedant Kumarbf891b12015-08-26 16:20:29 +0000989 switch (insn->opcode) {
990 case 0xE8:
991 case 0xE9:
Vedant Kumar44fccb72015-08-28 21:59:00 +0000992 // Take care of psubsb and other mmx instructions.
993 if (insn->opcodeType == ONEBYTE) {
Vedant Kumarbf891b12015-08-26 16:20:29 +0000994 attrMask ^= ATTR_OPSIZE;
995 insn->immediateSize = 4;
996 insn->displacementSize = 4;
997 }
998 break;
999 case 0x82:
1000 case 0x83:
1001 case 0x84:
1002 case 0x85:
1003 case 0x86:
1004 case 0x87:
1005 case 0x88:
1006 case 0x89:
1007 case 0x8A:
1008 case 0x8B:
1009 case 0x8C:
1010 case 0x8D:
1011 case 0x8E:
1012 case 0x8F:
Vedant Kumar44fccb72015-08-28 21:59:00 +00001013 // Take care of lea and three byte ops.
1014 if (insn->opcodeType == TWOBYTE) {
Vedant Kumarbf891b12015-08-26 16:20:29 +00001015 attrMask ^= ATTR_OPSIZE;
1016 insn->immediateSize = 4;
Vedant Kumar44fccb72015-08-28 21:59:00 +00001017 insn->displacementSize = 4;
Vedant Kumarbf891b12015-08-26 16:20:29 +00001018 }
1019 break;
1020 }
1021 }
1022
Craig Topper6e518772014-12-31 07:07:11 +00001023 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1024 return -1;
David Woodhouse9c74fdb2014-01-20 12:02:48 +00001025
Sean Callanan04cc3072009-12-19 02:59:52 +00001026 /* The following clauses compensate for limitations of the tables. */
Craig Topperf01f1b52011-11-06 23:04:08 +00001027
Craig Topper0676b902014-10-07 07:29:50 +00001028 if (insn->mode != MODE_64BIT &&
1029 insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1030 /*
1031 * The tables can't distinquish between cases where the W-bit is used to
1032 * select register size and cases where its a required part of the opcode.
1033 */
1034 if ((insn->vectorExtensionType == TYPE_EVEX &&
1035 wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1036 (insn->vectorExtensionType == TYPE_VEX_3B &&
1037 wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1038 (insn->vectorExtensionType == TYPE_XOP &&
1039 wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1040
1041 uint16_t instructionIDWithREXW;
1042 if (getIDWithAttrMask(&instructionIDWithREXW,
1043 insn, attrMask | ATTR_REXW)) {
1044 insn->instructionID = instructionID;
1045 insn->spec = specifierForUID(instructionID);
1046 return 0;
1047 }
1048
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001049 auto SpecName = GetInstrName(instructionIDWithREXW, miiArg);
Craig Topper0676b902014-10-07 07:29:50 +00001050 // If not a 64-bit instruction. Switch the opcode.
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001051 if (!is64Bit(SpecName.data())) {
Craig Topper0676b902014-10-07 07:29:50 +00001052 insn->instructionID = instructionIDWithREXW;
1053 insn->spec = specifierForUID(instructionIDWithREXW);
1054 return 0;
1055 }
1056 }
1057 }
1058
Craig Topper99bcab72014-12-31 07:07:31 +00001059 /*
Gabor Buellac8ded042018-05-01 10:01:16 +00001060 * Absolute moves, umonitor, and movdir64b need special handling.
Craig Topper99bcab72014-12-31 07:07:31 +00001061 * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1062 * inverted w.r.t.
1063 * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1064 * any position.
1065 */
Gabor Buella31fa8022018-04-20 18:42:47 +00001066 if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
Gabor Buellac8ded042018-05-01 10:01:16 +00001067 (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1068 (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
Craig Topper99bcab72014-12-31 07:07:31 +00001069 /* Make sure we observed the prefixes in any position. */
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001070 if (insn->hasAdSize)
Craig Topper99bcab72014-12-31 07:07:31 +00001071 attrMask |= ATTR_ADSIZE;
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001072 if (insn->hasOpSize)
Craig Topper99bcab72014-12-31 07:07:31 +00001073 attrMask |= ATTR_OPSIZE;
1074
1075 /* In 16-bit, invert the attributes. */
Gabor Buella31fa8022018-04-20 18:42:47 +00001076 if (insn->mode == MODE_16BIT) {
1077 attrMask ^= ATTR_ADSIZE;
Gabor Buellac8ded042018-05-01 10:01:16 +00001078
Gabor Buella31fa8022018-04-20 18:42:47 +00001079 /* The OpSize attribute is only valid with the absolute moves. */
1080 if (insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0))
1081 attrMask ^= ATTR_OPSIZE;
1082 }
Craig Topper99bcab72014-12-31 07:07:31 +00001083
1084 if (getIDWithAttrMask(&instructionID, insn, attrMask))
1085 return -1;
1086
1087 insn->instructionID = instructionID;
1088 insn->spec = specifierForUID(instructionID);
1089 return 0;
1090 }
1091
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001092 if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
David Woodhouse5cf4c672014-01-20 12:02:35 +00001093 !(attrMask & ATTR_OPSIZE)) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001094 /*
1095 * The instruction tables make no distinction between instructions that
1096 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1097 * particular spot (i.e., many MMX operations). In general we're
1098 * conservative, but in the specific case where OpSize is present but not
1099 * in the right place we check if there's a 16-bit operation.
1100 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001101
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001102 const struct InstructionSpecifier *spec;
Sean Callanan04cc3072009-12-19 02:59:52 +00001103 uint16_t instructionIDWithOpsize;
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001104 llvm::StringRef specName, specWithOpSizeName;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001105
Sean Callanan04cc3072009-12-19 02:59:52 +00001106 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001107
Sean Callanan04cc3072009-12-19 02:59:52 +00001108 if (getIDWithAttrMask(&instructionIDWithOpsize,
1109 insn,
1110 attrMask | ATTR_OPSIZE)) {
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001111 /*
Sean Callanan04cc3072009-12-19 02:59:52 +00001112 * ModRM required with OpSize but not present; give up and return version
1113 * without OpSize set
1114 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001115
Sean Callanan04cc3072009-12-19 02:59:52 +00001116 insn->instructionID = instructionID;
1117 insn->spec = spec;
1118 return 0;
1119 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001120
Richard Smith89ee75d2014-04-20 21:07:34 +00001121 specName = GetInstrName(instructionID, miiArg);
1122 specWithOpSizeName = GetInstrName(instructionIDWithOpsize, miiArg);
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001123
Mehdi Amini36d33fc2016-10-01 06:46:33 +00001124 if (is16BitEquivalent(specName.data(), specWithOpSizeName.data()) &&
Andrew V. Tischenkobfc90612017-10-16 11:14:29 +00001125 (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001126 insn->instructionID = instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +00001127 insn->spec = specifierForUID(instructionIDWithOpsize);
Sean Callanan04cc3072009-12-19 02:59:52 +00001128 } else {
1129 insn->instructionID = instructionID;
1130 insn->spec = spec;
1131 }
1132 return 0;
1133 }
Craig Topper21c33652011-10-02 16:56:09 +00001134
1135 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1136 insn->rexPrefix & 0x01) {
1137 /*
1138 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1139 * it should decode as XCHG %r8, %eax.
1140 */
1141
1142 const struct InstructionSpecifier *spec;
1143 uint16_t instructionIDWithNewOpcode;
1144 const struct InstructionSpecifier *specWithNewOpcode;
1145
1146 spec = specifierForUID(instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001147
Craig Topperb58a9662011-10-05 03:29:32 +00001148 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper21c33652011-10-02 16:56:09 +00001149 insn->opcode = 0x91;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001150
Craig Topper21c33652011-10-02 16:56:09 +00001151 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
1152 insn,
1153 attrMask)) {
1154 insn->opcode = 0x90;
1155
1156 insn->instructionID = instructionID;
1157 insn->spec = spec;
1158 return 0;
1159 }
1160
1161 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1162
Craig Topperb58a9662011-10-05 03:29:32 +00001163 /* Change back */
Craig Topper21c33652011-10-02 16:56:09 +00001164 insn->opcode = 0x90;
1165
1166 insn->instructionID = instructionIDWithNewOpcode;
1167 insn->spec = specWithNewOpcode;
1168
1169 return 0;
1170 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001171
Sean Callanan04cc3072009-12-19 02:59:52 +00001172 insn->instructionID = instructionID;
1173 insn->spec = specifierForUID(insn->instructionID);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001174
Sean Callanan04cc3072009-12-19 02:59:52 +00001175 return 0;
1176}
1177
1178/*
1179 * readSIB - Consumes the SIB byte to determine addressing information for an
1180 * instruction.
1181 *
1182 * @param insn - The instruction whose SIB byte is to be read.
1183 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
1184 */
1185static int readSIB(struct InternalInstruction* insn) {
Richard Smith89ee75d2014-04-20 21:07:34 +00001186 SIBBase sibBaseBase = SIB_BASE_NONE;
Sean Callanan04cc3072009-12-19 02:59:52 +00001187 uint8_t index, base;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001188
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001189 dbgprintf(insn, "readSIB()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001190
Sean Callanan04cc3072009-12-19 02:59:52 +00001191 if (insn->consumedSIB)
1192 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001193
Richard Smith5d5061032014-04-20 22:15:37 +00001194 insn->consumedSIB = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001195
Sean Callanan04cc3072009-12-19 02:59:52 +00001196 switch (insn->addressSize) {
1197 case 2:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001198 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan04cc3072009-12-19 02:59:52 +00001199 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001200 case 4:
Craig Topperca2382d2017-10-21 20:03:20 +00001201 insn->sibIndexBase = SIB_INDEX_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001202 sibBaseBase = SIB_BASE_EAX;
1203 break;
1204 case 8:
Craig Topperca2382d2017-10-21 20:03:20 +00001205 insn->sibIndexBase = SIB_INDEX_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001206 sibBaseBase = SIB_BASE_RAX;
1207 break;
1208 }
1209
1210 if (consumeByte(insn, &insn->sib))
1211 return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001212
Sean Callanan04cc3072009-12-19 02:59:52 +00001213 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001214
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001215 if (index == 0x4) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001216 insn->sibIndex = SIB_INDEX_NONE;
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001217 } else {
Craig Topperca2382d2017-10-21 20:03:20 +00001218 insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
Sean Callanan04cc3072009-12-19 02:59:52 +00001219 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001220
Douglas Katzmanfcda6f82015-06-24 22:04:55 +00001221 insn->sibScale = 1 << scaleFromSIB(insn->sib);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001222
Sean Callanan04cc3072009-12-19 02:59:52 +00001223 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001224
Sean Callanan04cc3072009-12-19 02:59:52 +00001225 switch (base) {
1226 case 0x5:
Craig Topperfae5ac22014-02-17 10:03:43 +00001227 case 0xd:
Sean Callanan04cc3072009-12-19 02:59:52 +00001228 switch (modFromModRM(insn->modRM)) {
1229 case 0x0:
1230 insn->eaDisplacement = EA_DISP_32;
1231 insn->sibBase = SIB_BASE_NONE;
1232 break;
1233 case 0x1:
1234 insn->eaDisplacement = EA_DISP_8;
Craig Topperfae5ac22014-02-17 10:03:43 +00001235 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001236 break;
1237 case 0x2:
1238 insn->eaDisplacement = EA_DISP_32;
Craig Topperfae5ac22014-02-17 10:03:43 +00001239 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001240 break;
1241 case 0x3:
Sean Callanan010b3732010-04-02 21:23:51 +00001242 debug("Cannot have Mod = 0b11 and a SIB byte");
1243 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001244 }
1245 break;
1246 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +00001247 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +00001248 break;
1249 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001250
Sean Callanan04cc3072009-12-19 02:59:52 +00001251 return 0;
1252}
1253
1254/*
1255 * readDisplacement - Consumes the displacement of an instruction.
1256 *
1257 * @param insn - The instruction whose displacement is to be read.
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001258 * @return - 0 if the displacement byte was successfully read; nonzero
Sean Callanan04cc3072009-12-19 02:59:52 +00001259 * otherwise.
1260 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001261static int readDisplacement(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001262 int8_t d8;
1263 int16_t d16;
1264 int32_t d32;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001265
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001266 dbgprintf(insn, "readDisplacement()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001267
Sean Callanan04cc3072009-12-19 02:59:52 +00001268 if (insn->consumedDisplacement)
1269 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001270
Richard Smith5d5061032014-04-20 22:15:37 +00001271 insn->consumedDisplacement = true;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001272 insn->displacementOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001273
Sean Callanan04cc3072009-12-19 02:59:52 +00001274 switch (insn->eaDisplacement) {
1275 case EA_DISP_NONE:
Richard Smith5d5061032014-04-20 22:15:37 +00001276 insn->consumedDisplacement = false;
Sean Callanan04cc3072009-12-19 02:59:52 +00001277 break;
1278 case EA_DISP_8:
1279 if (consumeInt8(insn, &d8))
1280 return -1;
1281 insn->displacement = d8;
1282 break;
1283 case EA_DISP_16:
1284 if (consumeInt16(insn, &d16))
1285 return -1;
1286 insn->displacement = d16;
1287 break;
1288 case EA_DISP_32:
1289 if (consumeInt32(insn, &d32))
1290 return -1;
1291 insn->displacement = d32;
1292 break;
1293 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001294
Richard Smith5d5061032014-04-20 22:15:37 +00001295 insn->consumedDisplacement = true;
Sean Callanan04cc3072009-12-19 02:59:52 +00001296 return 0;
1297}
1298
1299/*
1300 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1301 * displacement) for an instruction and interprets it.
1302 *
1303 * @param insn - The instruction whose addressing information is to be read.
1304 * @return - 0 if the information was successfully read; nonzero otherwise.
1305 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001306static int readModRM(struct InternalInstruction* insn) {
Craig Topper5b1dd012018-06-01 04:29:34 +00001307 uint8_t mod, rm, reg, evexrm;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001308
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001309 dbgprintf(insn, "readModRM()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001310
Sean Callanan04cc3072009-12-19 02:59:52 +00001311 if (insn->consumedModRM)
1312 return 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001313
Rafael Espindola9f9a1062011-01-06 16:48:42 +00001314 if (consumeByte(insn, &insn->modRM))
1315 return -1;
Richard Smith5d5061032014-04-20 22:15:37 +00001316 insn->consumedModRM = true;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001317
Sean Callanan04cc3072009-12-19 02:59:52 +00001318 mod = modFromModRM(insn->modRM);
1319 rm = rmFromModRM(insn->modRM);
1320 reg = regFromModRM(insn->modRM);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001321
Sean Callanan04cc3072009-12-19 02:59:52 +00001322 /*
1323 * This goes by insn->registerSize to pick the correct register, which messes
1324 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1325 * fixupReg().
1326 */
1327 switch (insn->registerSize) {
1328 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001329 insn->regBase = MODRM_REG_AX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001330 insn->eaRegBase = EA_REG_AX;
1331 break;
1332 case 4:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001333 insn->regBase = MODRM_REG_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001334 insn->eaRegBase = EA_REG_EAX;
1335 break;
1336 case 8:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001337 insn->regBase = MODRM_REG_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001338 insn->eaRegBase = EA_REG_RAX;
1339 break;
1340 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001341
Sean Callanan04cc3072009-12-19 02:59:52 +00001342 reg |= rFromREX(insn->rexPrefix) << 3;
1343 rm |= bFromREX(insn->rexPrefix) << 3;
Craig Topper5b1dd012018-06-01 04:29:34 +00001344
1345 evexrm = 0;
Craig Topper0179c6d2018-06-01 00:10:36 +00001346 if (insn->vectorExtensionType == TYPE_EVEX && insn->mode == MODE_64BIT) {
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001347 reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
Craig Topper5b1dd012018-06-01 04:29:34 +00001348 evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001349 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001350
Sean Callanan04cc3072009-12-19 02:59:52 +00001351 insn->reg = (Reg)(insn->regBase + reg);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001352
Sean Callanan04cc3072009-12-19 02:59:52 +00001353 switch (insn->addressSize) {
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001354 case 2: {
1355 EABase eaBaseBase = EA_BASE_BX_SI;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001356
Sean Callanan04cc3072009-12-19 02:59:52 +00001357 switch (mod) {
1358 case 0x0:
1359 if (rm == 0x6) {
1360 insn->eaBase = EA_BASE_NONE;
1361 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001362 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001363 return -1;
1364 } else {
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001365 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001366 insn->eaDisplacement = EA_DISP_NONE;
1367 }
1368 break;
1369 case 0x1:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001370 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001371 insn->eaDisplacement = EA_DISP_8;
Craig Topper399e39e2014-01-25 22:48:43 +00001372 insn->displacementSize = 1;
Sean Callanan010b3732010-04-02 21:23:51 +00001373 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001374 return -1;
1375 break;
1376 case 0x2:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001377 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001378 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001379 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001380 return -1;
1381 break;
1382 case 0x3:
1383 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001384 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001385 return -1;
1386 break;
1387 }
1388 break;
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001389 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001390 case 4:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001391 case 8: {
1392 EABase eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001393
Sean Callanan04cc3072009-12-19 02:59:52 +00001394 switch (mod) {
1395 case 0x0:
1396 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
Douglas Katzman6dc13972015-05-13 22:44:52 +00001397 // In determining whether RIP-relative mode is used (rm=5),
1398 // or whether a SIB byte is present (rm=4),
1399 // the extension bits (REX.b and EVEX.x) are ignored.
1400 switch (rm & 7) {
1401 case 0x4: // SIB byte is present
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001402 insn->eaBase = (insn->addressSize == 4 ?
Sean Callanan04cc3072009-12-19 02:59:52 +00001403 EA_BASE_sib : EA_BASE_sib64);
Craig Topper38afbfd2014-03-20 05:56:00 +00001404 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001405 return -1;
1406 break;
Douglas Katzman6dc13972015-05-13 22:44:52 +00001407 case 0x5: // RIP-relative
Sean Callanan04cc3072009-12-19 02:59:52 +00001408 insn->eaBase = EA_BASE_NONE;
1409 insn->eaDisplacement = EA_DISP_32;
Sean Callanan010b3732010-04-02 21:23:51 +00001410 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001411 return -1;
1412 break;
1413 default:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001414 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001415 break;
1416 }
1417 break;
1418 case 0x1:
Craig Topper399e39e2014-01-25 22:48:43 +00001419 insn->displacementSize = 1;
Alp Toker771f7652014-01-26 18:44:34 +00001420 /* FALLTHROUGH */
Sean Callanan04cc3072009-12-19 02:59:52 +00001421 case 0x2:
1422 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
Douglas Katzman6dc13972015-05-13 22:44:52 +00001423 switch (rm & 7) {
1424 case 0x4: // SIB byte is present
Sean Callanan04cc3072009-12-19 02:59:52 +00001425 insn->eaBase = EA_BASE_sib;
Craig Topper38afbfd2014-03-20 05:56:00 +00001426 if (readSIB(insn) || readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001427 return -1;
1428 break;
1429 default:
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001430 insn->eaBase = (EABase)(eaBaseBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001431 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001432 return -1;
1433 break;
1434 }
1435 break;
1436 case 0x3:
1437 insn->eaDisplacement = EA_DISP_NONE;
Craig Topper5b1dd012018-06-01 04:29:34 +00001438 insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
Sean Callanan04cc3072009-12-19 02:59:52 +00001439 break;
1440 }
1441 break;
Craig Topperc6b2c2b2018-06-01 04:29:30 +00001442 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001443 } /* switch (insn->addressSize) */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001444
Sean Callanan04cc3072009-12-19 02:59:52 +00001445 return 0;
1446}
1447
Craig Topperc3cf55b2018-06-01 06:11:29 +00001448#define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
Ahmed Bougacha85dc93c2016-07-14 14:53:21 +00001449 static uint16_t name(struct InternalInstruction *insn, \
1450 OperandType type, \
1451 uint8_t index, \
1452 uint8_t *valid) { \
Sean Callanan04cc3072009-12-19 02:59:52 +00001453 *valid = 1; \
1454 switch (type) { \
1455 default: \
Sean Callanan010b3732010-04-02 21:23:51 +00001456 debug("Unhandled register type"); \
1457 *valid = 0; \
1458 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001459 case TYPE_Rv: \
1460 return base + index; \
1461 case TYPE_R8: \
Craig Topperc3cf55b2018-06-01 06:11:29 +00001462 index &= mask; \
1463 if (index > 0xf) \
1464 *valid = 0; \
Sean Callanan010b3732010-04-02 21:23:51 +00001465 if (insn->rexPrefix && \
Sean Callanan04cc3072009-12-19 02:59:52 +00001466 index >= 4 && index <= 7) { \
1467 return prefix##_SPL + (index - 4); \
1468 } else { \
1469 return prefix##_AL + index; \
1470 } \
1471 case TYPE_R16: \
Craig Topperc3cf55b2018-06-01 06:11:29 +00001472 index &= mask; \
1473 if (index > 0xf) \
1474 *valid = 0; \
1475 return prefix##_AX + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001476 case TYPE_R32: \
Craig Topperc3cf55b2018-06-01 06:11:29 +00001477 index &= mask; \
1478 if (index > 0xf) \
1479 *valid = 0; \
1480 return prefix##_EAX + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001481 case TYPE_R64: \
Craig Topperc3cf55b2018-06-01 06:11:29 +00001482 index &= mask; \
1483 if (index > 0xf) \
1484 *valid = 0; \
1485 return prefix##_RAX + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001486 case TYPE_ZMM: \
Elena Demikhovsky003e7d72013-07-28 08:28:38 +00001487 return prefix##_ZMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001488 case TYPE_YMM: \
Sean Callananc3fd5232011-03-15 01:23:15 +00001489 return prefix##_YMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001490 case TYPE_XMM: \
Sean Callanan04cc3072009-12-19 02:59:52 +00001491 return prefix##_XMM0 + index; \
Craig Topperad944a12017-01-16 06:49:03 +00001492 case TYPE_VK: \
Craig Topper0838c4d2018-06-01 05:36:08 +00001493 index &= 0xf; \
Craig Topper9c26bcc2015-03-02 03:33:11 +00001494 if (index > 7) \
1495 *valid = 0; \
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001496 return prefix##_K0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001497 case TYPE_MM64: \
Craig Topperd5b39232014-12-26 18:19:44 +00001498 return prefix##_MM0 + (index & 0x7); \
Sean Callanan04cc3072009-12-19 02:59:52 +00001499 case TYPE_SEGMENTREG: \
Andrew V. Tischenkoeff4fc02017-10-23 09:36:33 +00001500 if ((index & 7) > 5) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001501 *valid = 0; \
Andrew V. Tischenkoeff4fc02017-10-23 09:36:33 +00001502 return prefix##_ES + (index & 7); \
Sean Callanan04cc3072009-12-19 02:59:52 +00001503 case TYPE_DEBUGREG: \
Sean Callanan04cc3072009-12-19 02:59:52 +00001504 return prefix##_DR0 + index; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001505 case TYPE_CONTROLREG: \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001506 return prefix##_CR0 + index; \
Ahmed Bougacha85dc93c2016-07-14 14:53:21 +00001507 case TYPE_BNDR: \
1508 if (index > 3) \
1509 *valid = 0; \
1510 return prefix##_BND0 + index; \
Craig Topperca2382d2017-10-21 20:03:20 +00001511 case TYPE_MVSIBX: \
1512 return prefix##_XMM0 + index; \
1513 case TYPE_MVSIBY: \
1514 return prefix##_YMM0 + index; \
1515 case TYPE_MVSIBZ: \
1516 return prefix##_ZMM0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001517 } \
1518 }
1519
1520/*
1521 * fixup*Value - Consults an operand type to determine the meaning of the
1522 * reg or R/M field. If the operand is an XMM operand, for example, an
1523 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1524 * misinterpret it as.
1525 *
1526 * @param insn - The instruction containing the operand.
1527 * @param type - The operand type.
1528 * @param index - The existing value of the field as reported by readModRM().
1529 * @param valid - The address of a uint8_t. The target is set to 1 if the
1530 * field is valid for the register class; 0 if not.
Sean Callanan010b3732010-04-02 21:23:51 +00001531 * @return - The proper value.
Sean Callanan04cc3072009-12-19 02:59:52 +00001532 */
Craig Topperc3cf55b2018-06-01 06:11:29 +00001533GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1534GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
Sean Callanan04cc3072009-12-19 02:59:52 +00001535
1536/*
1537 * fixupReg - Consults an operand specifier to determine which of the
1538 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1539 *
1540 * @param insn - See fixup*Value().
1541 * @param op - The operand specifier.
1542 * @return - 0 if fixup was successful; -1 if the register returned was
1543 * invalid for its class.
1544 */
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001545static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001546 const struct OperandSpecifier *op) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001547 uint8_t valid;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001548
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001549 dbgprintf(insn, "fixupReg()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001550
Sean Callanan04cc3072009-12-19 02:59:52 +00001551 switch ((OperandEncoding)op->encoding) {
1552 default:
Sean Callanan010b3732010-04-02 21:23:51 +00001553 debug("Expected a REG or R/M encoding in fixupReg");
1554 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +00001555 case ENCODING_VVVV:
1556 insn->vvvv = (Reg)fixupRegValue(insn,
1557 (OperandType)op->type,
1558 insn->vvvv,
1559 &valid);
1560 if (!valid)
1561 return -1;
1562 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001563 case ENCODING_REG:
1564 insn->reg = (Reg)fixupRegValue(insn,
1565 (OperandType)op->type,
1566 insn->reg - insn->regBase,
1567 &valid);
1568 if (!valid)
1569 return -1;
1570 break;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001571 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001572 if (insn->eaBase >= insn->eaRegBase) {
1573 insn->eaBase = (EABase)fixupRMValue(insn,
1574 (OperandType)op->type,
1575 insn->eaBase - insn->eaRegBase,
1576 &valid);
1577 if (!valid)
1578 return -1;
1579 }
1580 break;
1581 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001582
Sean Callanan04cc3072009-12-19 02:59:52 +00001583 return 0;
1584}
1585
1586/*
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001587 * readOpcodeRegister - Reads an operand from the opcode field of an
Sean Callanan04cc3072009-12-19 02:59:52 +00001588 * instruction and interprets it appropriately given the operand width.
1589 * Handles AddRegFrm instructions.
1590 *
Craig Topper91551182014-01-01 15:29:32 +00001591 * @param insn - the instruction whose opcode field is to be read.
Sean Callanan04cc3072009-12-19 02:59:52 +00001592 * @param size - The width (in bytes) of the register being specified.
1593 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1594 * RAX.
Sean Callanan010b3732010-04-02 21:23:51 +00001595 * @return - 0 on success; nonzero otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +00001596 */
Sean Callanan010b3732010-04-02 21:23:51 +00001597static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001598 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001599
Sean Callanan04cc3072009-12-19 02:59:52 +00001600 if (size == 0)
1601 size = insn->registerSize;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001602
Sean Callanan04cc3072009-12-19 02:59:52 +00001603 switch (size) {
1604 case 1:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001605 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001606 | (insn->opcode & 7)));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001607 if (insn->rexPrefix &&
Sean Callanan010b3732010-04-02 21:23:51 +00001608 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1609 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan2f9443f2009-12-22 02:07:42 +00001610 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1611 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan04cc3072009-12-19 02:59:52 +00001612 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001613
Sean Callanan04cc3072009-12-19 02:59:52 +00001614 break;
1615 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001616 insn->opcodeRegister = (Reg)(MODRM_REG_AX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001617 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001618 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001619 break;
1620 case 4:
Sean Callanan010b3732010-04-02 21:23:51 +00001621 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001622 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001623 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001624 break;
1625 case 8:
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001626 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1627 + ((bFromREX(insn->rexPrefix) << 3)
Craig Topper91551182014-01-01 15:29:32 +00001628 | (insn->opcode & 7)));
Sean Callanan04cc3072009-12-19 02:59:52 +00001629 break;
1630 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001631
Sean Callanan010b3732010-04-02 21:23:51 +00001632 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001633}
1634
1635/*
1636 * readImmediate - Consumes an immediate operand from an instruction, given the
1637 * desired operand size.
1638 *
1639 * @param insn - The instruction whose operand is to be read.
1640 * @param size - The width (in bytes) of the operand.
1641 * @return - 0 if the immediate was successfully consumed; nonzero
1642 * otherwise.
1643 */
1644static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1645 uint8_t imm8;
1646 uint16_t imm16;
1647 uint32_t imm32;
1648 uint64_t imm64;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001649
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001650 dbgprintf(insn, "readImmediate()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001651
Sean Callanan010b3732010-04-02 21:23:51 +00001652 if (insn->numImmediatesConsumed == 2) {
1653 debug("Already consumed two immediates");
1654 return -1;
1655 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001656
Sean Callanan04cc3072009-12-19 02:59:52 +00001657 if (size == 0)
1658 size = insn->immediateSize;
1659 else
1660 insn->immediateSize = size;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001661 insn->immediateOffset = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001662
Sean Callanan04cc3072009-12-19 02:59:52 +00001663 switch (size) {
1664 case 1:
1665 if (consumeByte(insn, &imm8))
1666 return -1;
1667 insn->immediates[insn->numImmediatesConsumed] = imm8;
1668 break;
1669 case 2:
1670 if (consumeUInt16(insn, &imm16))
1671 return -1;
1672 insn->immediates[insn->numImmediatesConsumed] = imm16;
1673 break;
1674 case 4:
1675 if (consumeUInt32(insn, &imm32))
1676 return -1;
1677 insn->immediates[insn->numImmediatesConsumed] = imm32;
1678 break;
1679 case 8:
1680 if (consumeUInt64(insn, &imm64))
1681 return -1;
1682 insn->immediates[insn->numImmediatesConsumed] = imm64;
1683 break;
1684 }
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001685
Sean Callanan04cc3072009-12-19 02:59:52 +00001686 insn->numImmediatesConsumed++;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001687
Sean Callanan04cc3072009-12-19 02:59:52 +00001688 return 0;
1689}
1690
1691/*
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001692 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callananc3fd5232011-03-15 01:23:15 +00001693 *
1694 * @param insn - The instruction whose operand is to be read.
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001695 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callananc3fd5232011-03-15 01:23:15 +00001696 * otherwise.
1697 */
1698static int readVVVV(struct InternalInstruction* insn) {
1699 dbgprintf(insn, "readVVVV()");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001700
Richard Smith89ee75d2014-04-20 21:07:34 +00001701 int vvvv;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001702 if (insn->vectorExtensionType == TYPE_EVEX)
Adam Nemet8ae70502014-06-24 01:42:32 +00001703 vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
1704 vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001705 else if (insn->vectorExtensionType == TYPE_VEX_3B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001706 vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001707 else if (insn->vectorExtensionType == TYPE_VEX_2B)
Richard Smith89ee75d2014-04-20 21:07:34 +00001708 vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001709 else if (insn->vectorExtensionType == TYPE_XOP)
Richard Smith89ee75d2014-04-20 21:07:34 +00001710 vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
Sean Callananc3fd5232011-03-15 01:23:15 +00001711 else
1712 return -1;
1713
Craig Topper0d0be472011-10-03 08:14:29 +00001714 if (insn->mode != MODE_64BIT)
Craig Topperdc5ba1e2018-06-01 01:23:52 +00001715 vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
Craig Topper0d0be472011-10-03 08:14:29 +00001716
Richard Smith89ee75d2014-04-20 21:07:34 +00001717 insn->vvvv = static_cast<Reg>(vvvv);
Sean Callananc3fd5232011-03-15 01:23:15 +00001718 return 0;
1719}
1720
1721/*
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001722 * readMaskRegister - Reads an mask register from the opcode field of an
1723 * instruction.
1724 *
1725 * @param insn - The instruction whose opcode field is to be read.
1726 * @return - 0 on success; nonzero otherwise.
1727 */
1728static int readMaskRegister(struct InternalInstruction* insn) {
1729 dbgprintf(insn, "readMaskRegister()");
1730
1731 if (insn->vectorExtensionType != TYPE_EVEX)
1732 return -1;
1733
Richard Smith89ee75d2014-04-20 21:07:34 +00001734 insn->writemask =
1735 static_cast<Reg>(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001736 return 0;
1737}
1738
1739/*
Sean Callanan04cc3072009-12-19 02:59:52 +00001740 * readOperands - Consults the specifier for an instruction and consumes all
1741 * operands for that instruction, interpreting them as it goes.
1742 *
1743 * @param insn - The instruction whose operands are to be read and interpreted.
1744 * @return - 0 if all operands could be read; nonzero otherwise.
1745 */
1746static int readOperands(struct InternalInstruction* insn) {
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001747 int hasVVVV, needVVVV;
Craig Topper2ba766a2011-12-30 06:23:39 +00001748 int sawRegImm = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001749
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001750 dbgprintf(insn, "readOperands()");
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001751
1752 /* If non-zero vvvv specified, need to make sure one of the operands
1753 uses it. */
1754 hasVVVV = !readVVVV(insn);
1755 needVVVV = hasVVVV && (insn->vvvv != 0);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001756
Patrik Hagglund31998382014-04-28 12:12:27 +00001757 for (const auto &Op : x86OperandSets[insn->spec->operands]) {
1758 switch (Op.encoding) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001759 case ENCODING_NONE:
David Woodhouse2ef8d9c2014-01-22 15:08:08 +00001760 case ENCODING_SI:
David Woodhouseb33c2ef2014-01-22 15:08:21 +00001761 case ENCODING_DI:
Sean Callanan04cc3072009-12-19 02:59:52 +00001762 break;
Craig Topper33ac0642017-01-16 05:44:25 +00001763 CASE_ENCODING_VSIB:
1764 // VSIB can use the V2 bit so check only the other bits.
1765 if (needVVVV)
1766 needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
Craig Topper3173a1f2017-01-16 05:44:33 +00001767 if (readModRM(insn))
1768 return -1;
Craig Topperca2382d2017-10-21 20:03:20 +00001769
Craig Topper158bc642017-10-22 04:32:30 +00001770 // Reject if SIB wasn't used.
1771 if (insn->eaBase != EA_BASE_sib && insn->eaBase != EA_BASE_sib64)
1772 return -1;
1773
Craig Topperca2382d2017-10-21 20:03:20 +00001774 // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
1775 if (insn->sibIndex == SIB_INDEX_NONE)
1776 insn->sibIndex = (SIBIndex)4;
1777
1778 // If EVEX.v2 is set this is one of the 16-31 registers.
Craig Topper0179c6d2018-06-01 00:10:36 +00001779 if (insn->vectorExtensionType == TYPE_EVEX && insn->mode == MODE_64BIT &&
Craig Topperca2382d2017-10-21 20:03:20 +00001780 v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1781 insn->sibIndex = (SIBIndex)(insn->sibIndex + 16);
1782
1783 // Adjust the index register to the correct size.
1784 switch ((OperandType)Op.type) {
1785 default:
1786 debug("Unhandled VSIB index type");
Craig Topper3173a1f2017-01-16 05:44:33 +00001787 return -1;
Craig Topperca2382d2017-10-21 20:03:20 +00001788 case TYPE_MVSIBX:
1789 insn->sibIndex = (SIBIndex)(SIB_INDEX_XMM0 +
1790 (insn->sibIndex - insn->sibIndexBase));
1791 break;
1792 case TYPE_MVSIBY:
1793 insn->sibIndex = (SIBIndex)(SIB_INDEX_YMM0 +
1794 (insn->sibIndex - insn->sibIndexBase));
1795 break;
1796 case TYPE_MVSIBZ:
1797 insn->sibIndex = (SIBIndex)(SIB_INDEX_ZMM0 +
1798 (insn->sibIndex - insn->sibIndexBase));
1799 break;
1800 }
1801
Craig Topper3173a1f2017-01-16 05:44:33 +00001802 // Apply the AVX512 compressed displacement scaling factor.
1803 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1804 insn->displacement *= 1 << (Op.encoding - ENCODING_VSIB);
1805 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001806 case ENCODING_REG:
Adam Nemet5933c2f2014-07-17 17:04:56 +00001807 CASE_ENCODING_RM:
Sean Callanan04cc3072009-12-19 02:59:52 +00001808 if (readModRM(insn))
1809 return -1;
Patrik Hagglund31998382014-04-28 12:12:27 +00001810 if (fixupReg(insn, &Op))
Sean Callanan04cc3072009-12-19 02:59:52 +00001811 return -1;
Adam Nemet5933c2f2014-07-17 17:04:56 +00001812 // Apply the AVX512 compressed displacement scaling factor.
1813 if (Op.encoding != ENCODING_REG && insn->eaDisplacement == EA_DISP_8)
1814 insn->displacement *= 1 << (Op.encoding - ENCODING_RM);
Sean Callanan04cc3072009-12-19 02:59:52 +00001815 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001816 case ENCODING_IB:
Craig Topper2ba766a2011-12-30 06:23:39 +00001817 if (sawRegImm) {
Benjamin Kramer9c48f262012-01-04 22:06:45 +00001818 /* Saw a register immediate so don't read again and instead split the
1819 previous immediate. FIXME: This is a hack. */
Benjamin Kramer47aecca2012-01-01 17:55:36 +00001820 insn->immediates[insn->numImmediatesConsumed] =
1821 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1822 ++insn->numImmediatesConsumed;
Craig Topper2ba766a2011-12-30 06:23:39 +00001823 break;
1824 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001825 if (readImmediate(insn, 1))
1826 return -1;
Craig Topperad944a12017-01-16 06:49:03 +00001827 if (Op.type == TYPE_XMM || Op.type == TYPE_YMM)
Craig Topper2ba766a2011-12-30 06:23:39 +00001828 sawRegImm = 1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001829 break;
1830 case ENCODING_IW:
1831 if (readImmediate(insn, 2))
1832 return -1;
1833 break;
1834 case ENCODING_ID:
1835 if (readImmediate(insn, 4))
1836 return -1;
1837 break;
1838 case ENCODING_IO:
1839 if (readImmediate(insn, 8))
1840 return -1;
1841 break;
1842 case ENCODING_Iv:
Sean Callanan010b3732010-04-02 21:23:51 +00001843 if (readImmediate(insn, insn->immediateSize))
1844 return -1;
Chris Lattnerd4758fc2010-04-16 21:15:15 +00001845 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001846 case ENCODING_Ia:
Sean Callanan010b3732010-04-02 21:23:51 +00001847 if (readImmediate(insn, insn->addressSize))
1848 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001849 break;
Craig Topper326008c2017-10-23 02:26:24 +00001850 case ENCODING_IRC:
1851 insn->RC = (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 1) |
1852 lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
1853 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001854 case ENCODING_RB:
Sean Callanan010b3732010-04-02 21:23:51 +00001855 if (readOpcodeRegister(insn, 1))
1856 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001857 break;
1858 case ENCODING_RW:
Sean Callanan010b3732010-04-02 21:23:51 +00001859 if (readOpcodeRegister(insn, 2))
1860 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001861 break;
1862 case ENCODING_RD:
Sean Callanan010b3732010-04-02 21:23:51 +00001863 if (readOpcodeRegister(insn, 4))
1864 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001865 break;
1866 case ENCODING_RO:
Sean Callanan010b3732010-04-02 21:23:51 +00001867 if (readOpcodeRegister(insn, 8))
1868 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001869 break;
1870 case ENCODING_Rv:
Sean Callanan010b3732010-04-02 21:23:51 +00001871 if (readOpcodeRegister(insn, 0))
1872 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001873 break;
Craig Topper623b0d62014-01-01 14:22:37 +00001874 case ENCODING_FP:
Sean Callananc3fd5232011-03-15 01:23:15 +00001875 break;
1876 case ENCODING_VVVV:
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001877 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1878 if (!hasVVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001879 return -1;
Craig Topperdc5ba1e2018-06-01 01:23:52 +00001880 if (insn->mode != MODE_64BIT)
1881 insn->vvvv = static_cast<Reg>(insn->vvvv & 0x7);
Patrik Hagglund31998382014-04-28 12:12:27 +00001882 if (fixupReg(insn, &Op))
Sean Callananc3fd5232011-03-15 01:23:15 +00001883 return -1;
1884 break;
Elena Demikhovsky371e3632013-12-25 11:40:51 +00001885 case ENCODING_WRITEMASK:
1886 if (readMaskRegister(insn))
1887 return -1;
1888 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001889 case ENCODING_DUP:
1890 break;
1891 default:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001892 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan04cc3072009-12-19 02:59:52 +00001893 return -1;
1894 }
1895 }
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001896
1897 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1898 if (needVVVV) return -1;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001899
Sean Callanan04cc3072009-12-19 02:59:52 +00001900 return 0;
1901}
1902
1903/*
1904 * decodeInstruction - Reads and interprets a full instruction provided by the
1905 * user.
1906 *
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001907 * @param insn - A pointer to the instruction to be populated. Must be
Sean Callanan04cc3072009-12-19 02:59:52 +00001908 * pre-allocated.
1909 * @param reader - The function to be used to read the instruction's bytes.
1910 * @param readerArg - A generic argument to be passed to the reader to store
1911 * any internal state.
1912 * @param logger - If non-NULL, the function to be used to write log messages
1913 * and warnings.
1914 * @param loggerArg - A generic argument to be passed to the logger to store
1915 * any internal state.
1916 * @param startLoc - The address (in the reader's address space) of the first
1917 * byte in the instruction.
1918 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1919 * decode the instruction in.
1920 * @return - 0 if the instruction's memory could be read; nonzero if
1921 * not.
1922 */
Richard Smith89ee75d2014-04-20 21:07:34 +00001923int llvm::X86Disassembler::decodeInstruction(
1924 struct InternalInstruction *insn, byteReader_t reader,
1925 const void *readerArg, dlog_t logger, void *loggerArg, const void *miiArg,
1926 uint64_t startLoc, DisassemblerMode mode) {
Daniel Dunbarc745a622009-12-19 03:31:50 +00001927 memset(insn, 0, sizeof(struct InternalInstruction));
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001928
Sean Callanan04cc3072009-12-19 02:59:52 +00001929 insn->reader = reader;
1930 insn->readerArg = readerArg;
1931 insn->dlog = logger;
1932 insn->dlogArg = loggerArg;
1933 insn->startLocation = startLoc;
1934 insn->readerCursor = startLoc;
1935 insn->mode = mode;
1936 insn->numImmediatesConsumed = 0;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001937
Sean Callanan04cc3072009-12-19 02:59:52 +00001938 if (readPrefixes(insn) ||
1939 readOpcode(insn) ||
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001940 getID(insn, miiArg) ||
Sean Callanan04cc3072009-12-19 02:59:52 +00001941 insn->instructionID == 0 ||
1942 readOperands(insn))
1943 return -1;
Craig Topperb8aec082012-08-01 07:39:18 +00001944
Patrik Hagglund31998382014-04-28 12:12:27 +00001945 insn->operands = x86OperandSets[insn->spec->operands];
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001946
Sean Callanan04cc3072009-12-19 02:59:52 +00001947 insn->length = insn->readerCursor - insn->startLocation;
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001948
Benjamin Kramer4f672272010-03-18 12:18:36 +00001949 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1950 startLoc, insn->readerCursor, insn->length);
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001951
Sean Callanan04cc3072009-12-19 02:59:52 +00001952 if (insn->length > 15)
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001953 dbgprintf(insn, "Instruction exceeds 15-byte limit");
NAKAMURA Takumidde7fa82013-03-25 20:55:43 +00001954
Sean Callanan04cc3072009-12-19 02:59:52 +00001955 return 0;
1956}