blob: e2c76b7f08db9628428f3033be26b0f4e3b4d7fe [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001/*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===*
Sean Callanan04cc3072009-12-19 02:59:52 +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 *===----------------------------------------------------------------------===*/
15
Sean Callanan04cc3072009-12-19 02:59:52 +000016#include <stdarg.h> /* for va_*() */
17#include <stdio.h> /* for vsnprintf() */
18#include <stdlib.h> /* for exit() */
Daniel Dunbarc745a622009-12-19 03:31:50 +000019#include <string.h> /* for memset() */
Sean Callanan04cc3072009-12-19 02:59:52 +000020
21#include "X86DisassemblerDecoder.h"
22
23#include "X86GenDisassemblerTables.inc"
24
25#define TRUE 1
26#define FALSE 0
27
Sean Callanan010b3732010-04-02 21:23:51 +000028typedef int8_t bool;
29
Sean Callanan010b3732010-04-02 21:23:51 +000030#ifndef NDEBUG
31#define debug(s) do { x86DisassemblerDebug(__FILE__, __LINE__, s); } while (0)
32#else
33#define debug(s) do { } while (0)
34#endif
35
Sean Callanan04cc3072009-12-19 02:59:52 +000036
37/*
38 * contextForAttrs - Client for the instruction context table. Takes a set of
39 * attributes and returns the appropriate decode context.
40 *
41 * @param attrMask - Attributes, from the enumeration attributeBits.
42 * @return - The InstructionContext to use when looking up an
43 * an instruction with these attributes.
44 */
Sean Callanan588785c2009-12-22 22:51:40 +000045static InstructionContext contextForAttrs(uint8_t attrMask) {
Sean Callanan04cc3072009-12-19 02:59:52 +000046 return CONTEXTS_SYM[attrMask];
47}
48
49/*
50 * modRMRequired - Reads the appropriate instruction table to determine whether
51 * the ModR/M byte is required to decode a particular instruction.
52 *
53 * @param type - The opcode type (i.e., how many bytes it has).
54 * @param insnContext - The context for the instruction, as returned by
55 * contextForAttrs.
56 * @param opcode - The last byte of the instruction's opcode, not counting
57 * ModR/M extensions and escapes.
58 * @return - TRUE if the ModR/M byte is required, FALSE otherwise.
59 */
Sean Callanan588785c2009-12-22 22:51:40 +000060static int modRMRequired(OpcodeType type,
Craig Topper21c33652011-10-02 16:56:09 +000061 InstructionContext insnContext,
62 uint8_t opcode) {
Daniel Dunbar8b532de2009-12-22 01:41:37 +000063 const struct ContextDecision* decision = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +000064
65 switch (type) {
66 case ONEBYTE:
67 decision = &ONEBYTE_SYM;
68 break;
69 case TWOBYTE:
70 decision = &TWOBYTE_SYM;
71 break;
72 case THREEBYTE_38:
73 decision = &THREEBYTE38_SYM;
74 break;
75 case THREEBYTE_3A:
76 decision = &THREEBYTE3A_SYM;
77 break;
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +000078 case THREEBYTE_A6:
79 decision = &THREEBYTEA6_SYM;
80 break;
81 case THREEBYTE_A7:
82 decision = &THREEBYTEA7_SYM;
83 break;
Sean Callanan04cc3072009-12-19 02:59:52 +000084 }
Ahmed Charles636a3d62012-02-19 11:37:01 +000085
Sean Callanan04cc3072009-12-19 02:59:52 +000086 return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].
87 modrm_type != MODRM_ONEENTRY;
Sean Callanan04cc3072009-12-19 02:59:52 +000088}
89
90/*
91 * decode - Reads the appropriate instruction table to obtain the unique ID of
92 * an instruction.
93 *
94 * @param type - See modRMRequired().
95 * @param insnContext - See modRMRequired().
96 * @param opcode - See modRMRequired().
97 * @param modRM - The ModR/M byte if required, or any value if not.
Sean Callanan010b3732010-04-02 21:23:51 +000098 * @return - The UID of the instruction, or 0 on failure.
Sean Callanan04cc3072009-12-19 02:59:52 +000099 */
Sean Callanan588785c2009-12-22 22:51:40 +0000100static InstrUID decode(OpcodeType type,
Sean Callanan010b3732010-04-02 21:23:51 +0000101 InstructionContext insnContext,
102 uint8_t opcode,
103 uint8_t modRM) {
Duncan Sandsae22c602012-02-05 14:20:11 +0000104 const struct ModRMDecision* dec = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000105
106 switch (type) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000107 case ONEBYTE:
108 dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
109 break;
110 case TWOBYTE:
111 dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
112 break;
113 case THREEBYTE_38:
114 dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
115 break;
116 case THREEBYTE_3A:
117 dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
118 break;
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000119 case THREEBYTE_A6:
120 dec = &THREEBYTEA6_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
121 break;
122 case THREEBYTE_A7:
123 dec = &THREEBYTEA7_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
124 break;
Sean Callanan04cc3072009-12-19 02:59:52 +0000125 }
126
127 switch (dec->modrm_type) {
128 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000129 debug("Corrupt table! Unknown modrm_type");
130 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000131 case MODRM_ONEENTRY:
Craig Topper487e7442012-02-09 07:45:30 +0000132 return modRMTable[dec->instructionIDs];
Sean Callanan04cc3072009-12-19 02:59:52 +0000133 case MODRM_SPLITRM:
134 if (modFromModRM(modRM) == 0x3)
Craig Topper487e7442012-02-09 07:45:30 +0000135 return modRMTable[dec->instructionIDs+1];
136 return modRMTable[dec->instructionIDs];
Craig Toppera0cd9702012-02-09 08:58:07 +0000137 case MODRM_SPLITREG:
138 if (modFromModRM(modRM) == 0x3)
139 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8];
140 return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
Sean Callanan04cc3072009-12-19 02:59:52 +0000141 case MODRM_FULL:
Craig Topper487e7442012-02-09 07:45:30 +0000142 return modRMTable[dec->instructionIDs+modRM];
Sean Callanan04cc3072009-12-19 02:59:52 +0000143 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000144}
145
146/*
147 * specifierForUID - Given a UID, returns the name and operand specification for
148 * that instruction.
149 *
150 * @param uid - The unique ID for the instruction. This should be returned by
151 * decode(); specifierForUID will not check bounds.
152 * @return - A pointer to the specification for that instruction.
153 */
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000154static const struct InstructionSpecifier *specifierForUID(InstrUID uid) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000155 return &INSTRUCTIONS_SYM[uid];
156}
157
158/*
159 * consumeByte - Uses the reader function provided by the user to consume one
160 * byte from the instruction's memory and advance the cursor.
161 *
162 * @param insn - The instruction with the reader function to use. The cursor
163 * for this instruction is advanced.
164 * @param byte - A pointer to a pre-allocated memory buffer to be populated
165 * with the data read.
166 * @return - 0 if the read was successful; nonzero otherwise.
167 */
Sean Callanan588785c2009-12-22 22:51:40 +0000168static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000169 int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
170
171 if (!ret)
172 ++(insn->readerCursor);
173
174 return ret;
175}
176
177/*
178 * lookAtByte - Like consumeByte, but does not advance the cursor.
179 *
180 * @param insn - See consumeByte().
181 * @param byte - See consumeByte().
182 * @return - See consumeByte().
183 */
Sean Callanan588785c2009-12-22 22:51:40 +0000184static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000185 return insn->reader(insn->readerArg, byte, insn->readerCursor);
186}
187
Sean Callanan588785c2009-12-22 22:51:40 +0000188static void unconsumeByte(struct InternalInstruction* insn) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000189 insn->readerCursor--;
190}
191
Sean Callanan588785c2009-12-22 22:51:40 +0000192#define CONSUME_FUNC(name, type) \
193 static int name(struct InternalInstruction* insn, type* ptr) { \
194 type combined = 0; \
195 unsigned offset; \
196 for (offset = 0; offset < sizeof(type); ++offset) { \
197 uint8_t byte; \
198 int ret = insn->reader(insn->readerArg, \
199 &byte, \
200 insn->readerCursor + offset); \
201 if (ret) \
202 return ret; \
203 combined = combined | ((type)byte << ((type)offset * 8)); \
204 } \
205 *ptr = combined; \
206 insn->readerCursor += sizeof(type); \
207 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +0000208 }
209
210/*
211 * consume* - Use the reader function provided by the user to consume data
212 * values of various sizes from the instruction's memory and advance the
213 * cursor appropriately. These readers perform endian conversion.
214 *
215 * @param insn - See consumeByte().
216 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
217 * be populated with the data read.
218 * @return - See consumeByte().
219 */
220CONSUME_FUNC(consumeInt8, int8_t)
221CONSUME_FUNC(consumeInt16, int16_t)
222CONSUME_FUNC(consumeInt32, int32_t)
223CONSUME_FUNC(consumeUInt16, uint16_t)
224CONSUME_FUNC(consumeUInt32, uint32_t)
225CONSUME_FUNC(consumeUInt64, uint64_t)
226
227/*
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000228 * dbgprintf - Uses the logging function provided by the user to log a single
Sean Callanan04cc3072009-12-19 02:59:52 +0000229 * message, typically without a carriage-return.
230 *
231 * @param insn - The instruction containing the logging function.
232 * @param format - See printf().
233 * @param ... - See printf().
234 */
Sean Callanan588785c2009-12-22 22:51:40 +0000235static void dbgprintf(struct InternalInstruction* insn,
236 const char* format,
237 ...) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000238 char buffer[256];
239 va_list ap;
240
241 if (!insn->dlog)
242 return;
243
244 va_start(ap, format);
245 (void)vsnprintf(buffer, sizeof(buffer), format, ap);
246 va_end(ap);
247
248 insn->dlog(insn->dlogArg, buffer);
249
250 return;
251}
252
253/*
254 * setPrefixPresent - Marks that a particular prefix is present at a particular
255 * location.
256 *
257 * @param insn - The instruction to be marked as having the prefix.
258 * @param prefix - The prefix that is present.
259 * @param location - The location where the prefix is located (in the address
260 * space of the instruction's reader).
261 */
Sean Callanan588785c2009-12-22 22:51:40 +0000262static void setPrefixPresent(struct InternalInstruction* insn,
Sean Callanan04cc3072009-12-19 02:59:52 +0000263 uint8_t prefix,
264 uint64_t location)
265{
266 insn->prefixPresent[prefix] = 1;
267 insn->prefixLocations[prefix] = location;
268}
269
270/*
271 * isPrefixAtLocation - Queries an instruction to determine whether a prefix is
272 * present at a given location.
273 *
274 * @param insn - The instruction to be queried.
275 * @param prefix - The prefix.
276 * @param location - The location to query.
277 * @return - Whether the prefix is at that location.
278 */
Sean Callanan588785c2009-12-22 22:51:40 +0000279static BOOL isPrefixAtLocation(struct InternalInstruction* insn,
280 uint8_t prefix,
281 uint64_t location)
Sean Callanan04cc3072009-12-19 02:59:52 +0000282{
283 if (insn->prefixPresent[prefix] == 1 &&
284 insn->prefixLocations[prefix] == location)
285 return TRUE;
286 else
287 return FALSE;
288}
289
290/*
291 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
292 * instruction as having them. Also sets the instruction's default operand,
293 * address, and other relevant data sizes to report operands correctly.
294 *
295 * @param insn - The instruction whose prefixes are to be read.
296 * @return - 0 if the instruction could be read until the end of the prefix
297 * bytes, and no prefixes conflicted; nonzero otherwise.
298 */
299static int readPrefixes(struct InternalInstruction* insn) {
300 BOOL isPrefix = TRUE;
301 BOOL prefixGroups[4] = { FALSE };
302 uint64_t prefixLocation;
Ted Kremenek3c4408c2011-01-23 17:05:06 +0000303 uint8_t byte = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000304
305 BOOL hasAdSize = FALSE;
306 BOOL hasOpSize = FALSE;
307
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000308 dbgprintf(insn, "readPrefixes()");
Sean Callanan04cc3072009-12-19 02:59:52 +0000309
310 while (isPrefix) {
311 prefixLocation = insn->readerCursor;
312
313 if (consumeByte(insn, &byte))
314 return -1;
315
316 switch (byte) {
317 case 0xf0: /* LOCK */
318 case 0xf2: /* REPNE/REPNZ */
319 case 0xf3: /* REP or REPE/REPZ */
320 if (prefixGroups[0])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000321 dbgprintf(insn, "Redundant Group 1 prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000322 prefixGroups[0] = TRUE;
323 setPrefixPresent(insn, byte, prefixLocation);
324 break;
325 case 0x2e: /* CS segment override -OR- Branch not taken */
326 case 0x36: /* SS segment override -OR- Branch taken */
327 case 0x3e: /* DS segment override */
328 case 0x26: /* ES segment override */
329 case 0x64: /* FS segment override */
330 case 0x65: /* GS segment override */
331 switch (byte) {
332 case 0x2e:
333 insn->segmentOverride = SEG_OVERRIDE_CS;
334 break;
335 case 0x36:
336 insn->segmentOverride = SEG_OVERRIDE_SS;
337 break;
338 case 0x3e:
339 insn->segmentOverride = SEG_OVERRIDE_DS;
340 break;
341 case 0x26:
342 insn->segmentOverride = SEG_OVERRIDE_ES;
343 break;
344 case 0x64:
345 insn->segmentOverride = SEG_OVERRIDE_FS;
346 break;
347 case 0x65:
348 insn->segmentOverride = SEG_OVERRIDE_GS;
349 break;
350 default:
Sean Callanan010b3732010-04-02 21:23:51 +0000351 debug("Unhandled override");
352 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000353 }
354 if (prefixGroups[1])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000355 dbgprintf(insn, "Redundant Group 2 prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000356 prefixGroups[1] = TRUE;
357 setPrefixPresent(insn, byte, prefixLocation);
358 break;
359 case 0x66: /* Operand-size override */
360 if (prefixGroups[2])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000361 dbgprintf(insn, "Redundant Group 3 prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000362 prefixGroups[2] = TRUE;
363 hasOpSize = TRUE;
364 setPrefixPresent(insn, byte, prefixLocation);
365 break;
366 case 0x67: /* Address-size override */
367 if (prefixGroups[3])
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000368 dbgprintf(insn, "Redundant Group 4 prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000369 prefixGroups[3] = TRUE;
370 hasAdSize = TRUE;
371 setPrefixPresent(insn, byte, prefixLocation);
372 break;
373 default: /* Not a prefix byte */
374 isPrefix = FALSE;
375 break;
376 }
377
378 if (isPrefix)
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000379 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan04cc3072009-12-19 02:59:52 +0000380 }
Sean Callananc3fd5232011-03-15 01:23:15 +0000381
382 insn->vexSize = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000383
Sean Callananc3fd5232011-03-15 01:23:15 +0000384 if (byte == 0xc4) {
385 uint8_t byte1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000386
Sean Callananc3fd5232011-03-15 01:23:15 +0000387 if (lookAtByte(insn, &byte1)) {
388 dbgprintf(insn, "Couldn't read second byte of VEX");
389 return -1;
390 }
391
Craig Topper45faba92011-09-26 05:12:43 +0000392 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000393 insn->vexSize = 3;
394 insn->necessaryPrefixLocation = insn->readerCursor - 1;
395 }
396 else {
Sean Callanan04cc3072009-12-19 02:59:52 +0000397 unconsumeByte(insn);
398 insn->necessaryPrefixLocation = insn->readerCursor - 1;
399 }
Sean Callananc3fd5232011-03-15 01:23:15 +0000400
401 if (insn->vexSize == 3) {
402 insn->vexPrefix[0] = byte;
403 consumeByte(insn, &insn->vexPrefix[1]);
404 consumeByte(insn, &insn->vexPrefix[2]);
405
406 /* We simulate the REX prefix for simplicity's sake */
Craig Topper31854ba2011-10-03 07:51:09 +0000407
408 if (insn->mode == MODE_64BIT) {
409 insn->rexPrefix = 0x40
410 | (wFromVEX3of3(insn->vexPrefix[2]) << 3)
411 | (rFromVEX2of3(insn->vexPrefix[1]) << 2)
412 | (xFromVEX2of3(insn->vexPrefix[1]) << 1)
413 | (bFromVEX2of3(insn->vexPrefix[1]) << 0);
414 }
Sean Callananc3fd5232011-03-15 01:23:15 +0000415
416 switch (ppFromVEX3of3(insn->vexPrefix[2]))
417 {
418 default:
419 break;
420 case VEX_PREFIX_66:
421 hasOpSize = TRUE;
422 break;
423 }
424
425 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx", insn->vexPrefix[0], insn->vexPrefix[1], insn->vexPrefix[2]);
426 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000427 }
Sean Callananc3fd5232011-03-15 01:23:15 +0000428 else if (byte == 0xc5) {
429 uint8_t byte1;
430
431 if (lookAtByte(insn, &byte1)) {
432 dbgprintf(insn, "Couldn't read second byte of VEX");
433 return -1;
434 }
435
Craig Topper45faba92011-09-26 05:12:43 +0000436 if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
Sean Callananc3fd5232011-03-15 01:23:15 +0000437 insn->vexSize = 2;
438 }
439 else {
440 unconsumeByte(insn);
441 }
442
443 if (insn->vexSize == 2) {
444 insn->vexPrefix[0] = byte;
445 consumeByte(insn, &insn->vexPrefix[1]);
446
Craig Topper31854ba2011-10-03 07:51:09 +0000447 if (insn->mode == MODE_64BIT) {
448 insn->rexPrefix = 0x40
449 | (rFromVEX2of2(insn->vexPrefix[1]) << 2);
450 }
Sean Callananc3fd5232011-03-15 01:23:15 +0000451
452 switch (ppFromVEX2of2(insn->vexPrefix[1]))
453 {
454 default:
455 break;
456 case VEX_PREFIX_66:
457 hasOpSize = TRUE;
458 break;
459 }
460
461 dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx", insn->vexPrefix[0], insn->vexPrefix[1]);
462 }
463 }
464 else {
465 if (insn->mode == MODE_64BIT) {
466 if ((byte & 0xf0) == 0x40) {
467 uint8_t opcodeByte;
468
469 if (lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) {
470 dbgprintf(insn, "Redundant REX prefix");
471 return -1;
472 }
473
474 insn->rexPrefix = byte;
475 insn->necessaryPrefixLocation = insn->readerCursor - 2;
476
477 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
478 } else {
479 unconsumeByte(insn);
480 insn->necessaryPrefixLocation = insn->readerCursor - 1;
481 }
482 } else {
483 unconsumeByte(insn);
484 insn->necessaryPrefixLocation = insn->readerCursor - 1;
485 }
486 }
487
Sean Callanan04cc3072009-12-19 02:59:52 +0000488 if (insn->mode == MODE_16BIT) {
489 insn->registerSize = (hasOpSize ? 4 : 2);
490 insn->addressSize = (hasAdSize ? 4 : 2);
491 insn->displacementSize = (hasAdSize ? 4 : 2);
492 insn->immediateSize = (hasOpSize ? 4 : 2);
493 } else if (insn->mode == MODE_32BIT) {
494 insn->registerSize = (hasOpSize ? 2 : 4);
495 insn->addressSize = (hasAdSize ? 2 : 4);
496 insn->displacementSize = (hasAdSize ? 2 : 4);
Sean Callanan9f6c6222010-10-22 01:24:11 +0000497 insn->immediateSize = (hasOpSize ? 2 : 4);
Sean Callanan04cc3072009-12-19 02:59:52 +0000498 } else if (insn->mode == MODE_64BIT) {
499 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
500 insn->registerSize = 8;
501 insn->addressSize = (hasAdSize ? 4 : 8);
502 insn->displacementSize = 4;
503 insn->immediateSize = 4;
504 } else if (insn->rexPrefix) {
505 insn->registerSize = (hasOpSize ? 2 : 4);
506 insn->addressSize = (hasAdSize ? 4 : 8);
507 insn->displacementSize = (hasOpSize ? 2 : 4);
508 insn->immediateSize = (hasOpSize ? 2 : 4);
509 } else {
510 insn->registerSize = (hasOpSize ? 2 : 4);
511 insn->addressSize = (hasAdSize ? 4 : 8);
512 insn->displacementSize = (hasOpSize ? 2 : 4);
513 insn->immediateSize = (hasOpSize ? 2 : 4);
514 }
515 }
516
517 return 0;
518}
519
520/*
521 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
522 * extended or escape opcodes).
523 *
524 * @param insn - The instruction whose opcode is to be read.
525 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
526 */
527static int readOpcode(struct InternalInstruction* insn) {
528 /* Determine the length of the primary opcode */
529
530 uint8_t current;
531
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000532 dbgprintf(insn, "readOpcode()");
Sean Callanan04cc3072009-12-19 02:59:52 +0000533
534 insn->opcodeType = ONEBYTE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000535
536 if (insn->vexSize == 3)
537 {
538 switch (mmmmmFromVEX2of3(insn->vexPrefix[1]))
539 {
540 default:
541 dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", mmmmmFromVEX2of3(insn->vexPrefix[1]));
542 return -1;
543 case 0:
544 break;
545 case VEX_LOB_0F:
546 insn->twoByteEscape = 0x0f;
547 insn->opcodeType = TWOBYTE;
548 return consumeByte(insn, &insn->opcode);
549 case VEX_LOB_0F38:
550 insn->twoByteEscape = 0x0f;
551 insn->threeByteEscape = 0x38;
552 insn->opcodeType = THREEBYTE_38;
553 return consumeByte(insn, &insn->opcode);
554 case VEX_LOB_0F3A:
555 insn->twoByteEscape = 0x0f;
556 insn->threeByteEscape = 0x3a;
557 insn->opcodeType = THREEBYTE_3A;
558 return consumeByte(insn, &insn->opcode);
559 }
560 }
561 else if (insn->vexSize == 2)
562 {
563 insn->twoByteEscape = 0x0f;
564 insn->opcodeType = TWOBYTE;
565 return consumeByte(insn, &insn->opcode);
566 }
567
Sean Callanan04cc3072009-12-19 02:59:52 +0000568 if (consumeByte(insn, &current))
569 return -1;
570
571 if (current == 0x0f) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000572 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
Sean Callanan04cc3072009-12-19 02:59:52 +0000573
574 insn->twoByteEscape = current;
575
576 if (consumeByte(insn, &current))
577 return -1;
578
579 if (current == 0x38) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000580 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
Sean Callanan04cc3072009-12-19 02:59:52 +0000581
582 insn->threeByteEscape = current;
583
584 if (consumeByte(insn, &current))
585 return -1;
586
587 insn->opcodeType = THREEBYTE_38;
588 } else if (current == 0x3a) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000589 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
Sean Callanan04cc3072009-12-19 02:59:52 +0000590
591 insn->threeByteEscape = current;
592
593 if (consumeByte(insn, &current))
594 return -1;
595
596 insn->opcodeType = THREEBYTE_3A;
Joerg Sonnenbergerfc4789d2011-04-04 16:58:13 +0000597 } else if (current == 0xa6) {
598 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
599
600 insn->threeByteEscape = current;
601
602 if (consumeByte(insn, &current))
603 return -1;
604
605 insn->opcodeType = THREEBYTE_A6;
606 } else if (current == 0xa7) {
607 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
608
609 insn->threeByteEscape = current;
610
611 if (consumeByte(insn, &current))
612 return -1;
613
614 insn->opcodeType = THREEBYTE_A7;
Sean Callanan04cc3072009-12-19 02:59:52 +0000615 } else {
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000616 dbgprintf(insn, "Didn't find a three-byte escape prefix");
Sean Callanan04cc3072009-12-19 02:59:52 +0000617
618 insn->opcodeType = TWOBYTE;
619 }
620 }
621
622 /*
623 * At this point we have consumed the full opcode.
624 * Anything we consume from here on must be unconsumed.
625 */
626
627 insn->opcode = current;
628
629 return 0;
630}
631
632static int readModRM(struct InternalInstruction* insn);
633
634/*
635 * getIDWithAttrMask - Determines the ID of an instruction, consuming
636 * the ModR/M byte as appropriate for extended and escape opcodes,
637 * and using a supplied attribute mask.
638 *
639 * @param instructionID - A pointer whose target is filled in with the ID of the
640 * instruction.
641 * @param insn - The instruction whose ID is to be determined.
642 * @param attrMask - The attribute mask to search.
643 * @return - 0 if the ModR/M could be read when needed or was not
644 * needed; nonzero otherwise.
645 */
646static int getIDWithAttrMask(uint16_t* instructionID,
647 struct InternalInstruction* insn,
648 uint8_t attrMask) {
649 BOOL hasModRMExtension;
650
651 uint8_t instructionClass;
652
653 instructionClass = contextForAttrs(attrMask);
654
655 hasModRMExtension = modRMRequired(insn->opcodeType,
656 instructionClass,
657 insn->opcode);
658
659 if (hasModRMExtension) {
Rafael Espindola9f9a1062011-01-06 16:48:42 +0000660 if (readModRM(insn))
661 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000662
663 *instructionID = decode(insn->opcodeType,
664 instructionClass,
665 insn->opcode,
666 insn->modRM);
667 } else {
668 *instructionID = decode(insn->opcodeType,
669 instructionClass,
670 insn->opcode,
671 0);
672 }
673
674 return 0;
675}
676
677/*
678 * is16BitEquivalent - Determines whether two instruction names refer to
679 * equivalent instructions but one is 16-bit whereas the other is not.
680 *
681 * @param orig - The instruction that is not 16-bit
682 * @param equiv - The instruction that is 16-bit
683 */
684static BOOL is16BitEquvalent(const char* orig, const char* equiv) {
685 off_t i;
686
Sean Callanan010b3732010-04-02 21:23:51 +0000687 for (i = 0;; i++) {
688 if (orig[i] == '\0' && equiv[i] == '\0')
Sean Callanan04cc3072009-12-19 02:59:52 +0000689 return TRUE;
Sean Callanan010b3732010-04-02 21:23:51 +0000690 if (orig[i] == '\0' || equiv[i] == '\0')
Sean Callanan04cc3072009-12-19 02:59:52 +0000691 return FALSE;
Sean Callanan010b3732010-04-02 21:23:51 +0000692 if (orig[i] != equiv[i]) {
693 if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
Sean Callanan04cc3072009-12-19 02:59:52 +0000694 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000695 if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
Sean Callanan04cc3072009-12-19 02:59:52 +0000696 continue;
Sean Callanan010b3732010-04-02 21:23:51 +0000697 if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
Sean Callanan04cc3072009-12-19 02:59:52 +0000698 continue;
699 return FALSE;
700 }
701 }
702}
703
704/*
Sean Callanan04cc3072009-12-19 02:59:52 +0000705 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
706 * appropriate for extended and escape opcodes. Determines the attributes and
707 * context for the instruction before doing so.
708 *
709 * @param insn - The instruction whose ID is to be determined.
710 * @return - 0 if the ModR/M could be read when needed or was not needed;
711 * nonzero otherwise.
712 */
Benjamin Kramer478e8de2012-02-11 14:50:54 +0000713static int getID(struct InternalInstruction* insn, void *miiArg) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000714 uint8_t attrMask;
715 uint16_t instructionID;
716
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000717 dbgprintf(insn, "getID()");
Sean Callanan04cc3072009-12-19 02:59:52 +0000718
719 attrMask = ATTR_NONE;
Sean Callananc3fd5232011-03-15 01:23:15 +0000720
Sean Callanan04cc3072009-12-19 02:59:52 +0000721 if (insn->mode == MODE_64BIT)
722 attrMask |= ATTR_64BIT;
Sean Callananc3fd5232011-03-15 01:23:15 +0000723
724 if (insn->vexSize) {
725 attrMask |= ATTR_VEX;
726
727 if (insn->vexSize == 3) {
728 switch (ppFromVEX3of3(insn->vexPrefix[2])) {
729 case VEX_PREFIX_66:
730 attrMask |= ATTR_OPSIZE;
731 break;
732 case VEX_PREFIX_F3:
733 attrMask |= ATTR_XS;
734 break;
735 case VEX_PREFIX_F2:
736 attrMask |= ATTR_XD;
737 break;
738 }
739
Sean Callananc3fd5232011-03-15 01:23:15 +0000740 if (lFromVEX3of3(insn->vexPrefix[2]))
741 attrMask |= ATTR_VEXL;
742 }
743 else if (insn->vexSize == 2) {
744 switch (ppFromVEX2of2(insn->vexPrefix[1])) {
745 case VEX_PREFIX_66:
746 attrMask |= ATTR_OPSIZE;
747 break;
748 case VEX_PREFIX_F3:
749 attrMask |= ATTR_XS;
750 break;
751 case VEX_PREFIX_F2:
752 attrMask |= ATTR_XD;
753 break;
754 }
755
756 if (lFromVEX2of2(insn->vexPrefix[1]))
757 attrMask |= ATTR_VEXL;
758 }
759 else {
760 return -1;
761 }
762 }
763 else {
Sean Callananc3fd5232011-03-15 01:23:15 +0000764 if (isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
765 attrMask |= ATTR_OPSIZE;
766 else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
767 attrMask |= ATTR_XS;
768 else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
769 attrMask |= ATTR_XD;
Sean Callananc3fd5232011-03-15 01:23:15 +0000770 }
771
Craig Topperf18c8962011-10-04 06:30:42 +0000772 if (insn->rexPrefix & 0x08)
773 attrMask |= ATTR_REXW;
Craig Topperf01f1b52011-11-06 23:04:08 +0000774
Sean Callanan010b3732010-04-02 21:23:51 +0000775 if (getIDWithAttrMask(&instructionID, insn, attrMask))
Sean Callanan04cc3072009-12-19 02:59:52 +0000776 return -1;
Craig Topperf01f1b52011-11-06 23:04:08 +0000777
Sean Callanan04cc3072009-12-19 02:59:52 +0000778 /* The following clauses compensate for limitations of the tables. */
Craig Topperf01f1b52011-11-06 23:04:08 +0000779
780 if ((attrMask & ATTR_VEXL) && (attrMask & ATTR_REXW) &&
781 !(attrMask & ATTR_OPSIZE)) {
Craig Topperf18c8962011-10-04 06:30:42 +0000782 /*
783 * Some VEX instructions ignore the L-bit, but use the W-bit. Normally L-bit
784 * has precedence since there are no L-bit with W-bit entries in the tables.
785 * So if the L-bit isn't significant we should use the W-bit instead.
Craig Topperf01f1b52011-11-06 23:04:08 +0000786 * We only need to do this if the instruction doesn't specify OpSize since
787 * there is a VEX_L_W_OPSIZE table.
Craig Topperf18c8962011-10-04 06:30:42 +0000788 */
789
790 const struct InstructionSpecifier *spec;
791 uint16_t instructionIDWithWBit;
792 const struct InstructionSpecifier *specWithWBit;
793
794 spec = specifierForUID(instructionID);
795
796 if (getIDWithAttrMask(&instructionIDWithWBit,
797 insn,
798 (attrMask & (~ATTR_VEXL)) | ATTR_REXW)) {
799 insn->instructionID = instructionID;
800 insn->spec = spec;
801 return 0;
802 }
803
804 specWithWBit = specifierForUID(instructionIDWithWBit);
805
806 if (instructionID != instructionIDWithWBit) {
807 insn->instructionID = instructionIDWithWBit;
808 insn->spec = specWithWBit;
809 } else {
810 insn->instructionID = instructionID;
811 insn->spec = spec;
812 }
813 return 0;
814 }
815
Sean Callanan04cc3072009-12-19 02:59:52 +0000816 if (insn->prefixPresent[0x66] && !(attrMask & ATTR_OPSIZE)) {
817 /*
818 * The instruction tables make no distinction between instructions that
819 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
820 * particular spot (i.e., many MMX operations). In general we're
821 * conservative, but in the specific case where OpSize is present but not
822 * in the right place we check if there's a 16-bit operation.
823 */
824
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +0000825 const struct InstructionSpecifier *spec;
Sean Callanan04cc3072009-12-19 02:59:52 +0000826 uint16_t instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +0000827 const char *specName, *specWithOpSizeName;
Sean Callanan04cc3072009-12-19 02:59:52 +0000828
829 spec = specifierForUID(instructionID);
830
831 if (getIDWithAttrMask(&instructionIDWithOpsize,
832 insn,
833 attrMask | ATTR_OPSIZE)) {
834 /*
835 * ModRM required with OpSize but not present; give up and return version
836 * without OpSize set
837 */
838
839 insn->instructionID = instructionID;
840 insn->spec = spec;
841 return 0;
842 }
843
Benjamin Kramer915e3d92012-02-11 16:01:02 +0000844 specName = x86DisassemblerGetInstrName(instructionID, miiArg);
845 specWithOpSizeName =
Benjamin Kramer478e8de2012-02-11 14:50:54 +0000846 x86DisassemblerGetInstrName(instructionIDWithOpsize, miiArg);
847
Benjamin Kramer915e3d92012-02-11 16:01:02 +0000848 if (is16BitEquvalent(specName, specWithOpSizeName)) {
Sean Callanan04cc3072009-12-19 02:59:52 +0000849 insn->instructionID = instructionIDWithOpsize;
Benjamin Kramer915e3d92012-02-11 16:01:02 +0000850 insn->spec = specifierForUID(instructionIDWithOpsize);
Sean Callanan04cc3072009-12-19 02:59:52 +0000851 } else {
852 insn->instructionID = instructionID;
853 insn->spec = spec;
854 }
855 return 0;
856 }
Craig Topper21c33652011-10-02 16:56:09 +0000857
858 if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
859 insn->rexPrefix & 0x01) {
860 /*
861 * NOOP shouldn't decode as NOOP if REX.b is set. Instead
862 * it should decode as XCHG %r8, %eax.
863 */
864
865 const struct InstructionSpecifier *spec;
866 uint16_t instructionIDWithNewOpcode;
867 const struct InstructionSpecifier *specWithNewOpcode;
868
869 spec = specifierForUID(instructionID);
870
Craig Topperb58a9662011-10-05 03:29:32 +0000871 /* Borrow opcode from one of the other XCHGar opcodes */
Craig Topper21c33652011-10-02 16:56:09 +0000872 insn->opcode = 0x91;
873
874 if (getIDWithAttrMask(&instructionIDWithNewOpcode,
875 insn,
876 attrMask)) {
877 insn->opcode = 0x90;
878
879 insn->instructionID = instructionID;
880 insn->spec = spec;
881 return 0;
882 }
883
884 specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
885
Craig Topperb58a9662011-10-05 03:29:32 +0000886 /* Change back */
Craig Topper21c33652011-10-02 16:56:09 +0000887 insn->opcode = 0x90;
888
889 insn->instructionID = instructionIDWithNewOpcode;
890 insn->spec = specWithNewOpcode;
891
892 return 0;
893 }
Sean Callanan04cc3072009-12-19 02:59:52 +0000894
895 insn->instructionID = instructionID;
896 insn->spec = specifierForUID(insn->instructionID);
897
898 return 0;
899}
900
901/*
902 * readSIB - Consumes the SIB byte to determine addressing information for an
903 * instruction.
904 *
905 * @param insn - The instruction whose SIB byte is to be read.
906 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
907 */
908static int readSIB(struct InternalInstruction* insn) {
Daniel Dunbar8b532de2009-12-22 01:41:37 +0000909 SIBIndex sibIndexBase = 0;
910 SIBBase sibBaseBase = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +0000911 uint8_t index, base;
912
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000913 dbgprintf(insn, "readSIB()");
Sean Callanan04cc3072009-12-19 02:59:52 +0000914
915 if (insn->consumedSIB)
916 return 0;
917
918 insn->consumedSIB = TRUE;
919
920 switch (insn->addressSize) {
921 case 2:
Nuno Lopes3ed6d602009-12-19 12:07:00 +0000922 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan04cc3072009-12-19 02:59:52 +0000923 return -1;
924 break;
925 case 4:
926 sibIndexBase = SIB_INDEX_EAX;
927 sibBaseBase = SIB_BASE_EAX;
928 break;
929 case 8:
930 sibIndexBase = SIB_INDEX_RAX;
931 sibBaseBase = SIB_BASE_RAX;
932 break;
933 }
934
935 if (consumeByte(insn, &insn->sib))
936 return -1;
937
938 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
939
940 switch (index) {
941 case 0x4:
942 insn->sibIndex = SIB_INDEX_NONE;
943 break;
944 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +0000945 insn->sibIndex = (SIBIndex)(sibIndexBase + index);
Sean Callanan04cc3072009-12-19 02:59:52 +0000946 if (insn->sibIndex == SIB_INDEX_sib ||
947 insn->sibIndex == SIB_INDEX_sib64)
948 insn->sibIndex = SIB_INDEX_NONE;
949 break;
950 }
951
952 switch (scaleFromSIB(insn->sib)) {
953 case 0:
954 insn->sibScale = 1;
955 break;
956 case 1:
957 insn->sibScale = 2;
958 break;
959 case 2:
960 insn->sibScale = 4;
961 break;
962 case 3:
963 insn->sibScale = 8;
964 break;
965 }
966
967 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
968
969 switch (base) {
970 case 0x5:
971 switch (modFromModRM(insn->modRM)) {
972 case 0x0:
973 insn->eaDisplacement = EA_DISP_32;
974 insn->sibBase = SIB_BASE_NONE;
975 break;
976 case 0x1:
977 insn->eaDisplacement = EA_DISP_8;
978 insn->sibBase = (insn->addressSize == 4 ?
979 SIB_BASE_EBP : SIB_BASE_RBP);
980 break;
981 case 0x2:
982 insn->eaDisplacement = EA_DISP_32;
983 insn->sibBase = (insn->addressSize == 4 ?
984 SIB_BASE_EBP : SIB_BASE_RBP);
985 break;
986 case 0x3:
Sean Callanan010b3732010-04-02 21:23:51 +0000987 debug("Cannot have Mod = 0b11 and a SIB byte");
988 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +0000989 }
990 break;
991 default:
Benjamin Kramer25bddae2011-02-27 18:13:53 +0000992 insn->sibBase = (SIBBase)(sibBaseBase + base);
Sean Callanan04cc3072009-12-19 02:59:52 +0000993 break;
994 }
995
996 return 0;
997}
998
999/*
1000 * readDisplacement - Consumes the displacement of an instruction.
1001 *
1002 * @param insn - The instruction whose displacement is to be read.
1003 * @return - 0 if the displacement byte was successfully read; nonzero
1004 * otherwise.
1005 */
1006static int readDisplacement(struct InternalInstruction* insn) {
1007 int8_t d8;
1008 int16_t d16;
1009 int32_t d32;
1010
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001011 dbgprintf(insn, "readDisplacement()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001012
1013 if (insn->consumedDisplacement)
1014 return 0;
1015
1016 insn->consumedDisplacement = TRUE;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001017 insn->displacementOffset = insn->readerCursor - insn->startLocation;
Sean Callanan04cc3072009-12-19 02:59:52 +00001018
1019 switch (insn->eaDisplacement) {
1020 case EA_DISP_NONE:
1021 insn->consumedDisplacement = FALSE;
1022 break;
1023 case EA_DISP_8:
1024 if (consumeInt8(insn, &d8))
1025 return -1;
1026 insn->displacement = d8;
1027 break;
1028 case EA_DISP_16:
1029 if (consumeInt16(insn, &d16))
1030 return -1;
1031 insn->displacement = d16;
1032 break;
1033 case EA_DISP_32:
1034 if (consumeInt32(insn, &d32))
1035 return -1;
1036 insn->displacement = d32;
1037 break;
1038 }
1039
1040 insn->consumedDisplacement = TRUE;
1041 return 0;
1042}
1043
1044/*
1045 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1046 * displacement) for an instruction and interprets it.
1047 *
1048 * @param insn - The instruction whose addressing information is to be read.
1049 * @return - 0 if the information was successfully read; nonzero otherwise.
1050 */
1051static int readModRM(struct InternalInstruction* insn) {
1052 uint8_t mod, rm, reg;
1053
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001054 dbgprintf(insn, "readModRM()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001055
1056 if (insn->consumedModRM)
1057 return 0;
1058
Rafael Espindola9f9a1062011-01-06 16:48:42 +00001059 if (consumeByte(insn, &insn->modRM))
1060 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001061 insn->consumedModRM = TRUE;
1062
1063 mod = modFromModRM(insn->modRM);
1064 rm = rmFromModRM(insn->modRM);
1065 reg = regFromModRM(insn->modRM);
1066
1067 /*
1068 * This goes by insn->registerSize to pick the correct register, which messes
1069 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
1070 * fixupReg().
1071 */
1072 switch (insn->registerSize) {
1073 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001074 insn->regBase = MODRM_REG_AX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001075 insn->eaRegBase = EA_REG_AX;
1076 break;
1077 case 4:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001078 insn->regBase = MODRM_REG_EAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001079 insn->eaRegBase = EA_REG_EAX;
1080 break;
1081 case 8:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001082 insn->regBase = MODRM_REG_RAX;
Sean Callanan04cc3072009-12-19 02:59:52 +00001083 insn->eaRegBase = EA_REG_RAX;
1084 break;
1085 }
1086
1087 reg |= rFromREX(insn->rexPrefix) << 3;
1088 rm |= bFromREX(insn->rexPrefix) << 3;
1089
1090 insn->reg = (Reg)(insn->regBase + reg);
1091
1092 switch (insn->addressSize) {
1093 case 2:
1094 insn->eaBaseBase = EA_BASE_BX_SI;
1095
1096 switch (mod) {
1097 case 0x0:
1098 if (rm == 0x6) {
1099 insn->eaBase = EA_BASE_NONE;
1100 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001101 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001102 return -1;
1103 } else {
1104 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1105 insn->eaDisplacement = EA_DISP_NONE;
1106 }
1107 break;
1108 case 0x1:
1109 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1110 insn->eaDisplacement = EA_DISP_8;
Sean Callanan010b3732010-04-02 21:23:51 +00001111 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001112 return -1;
1113 break;
1114 case 0x2:
1115 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1116 insn->eaDisplacement = EA_DISP_16;
Sean Callanan010b3732010-04-02 21:23:51 +00001117 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001118 return -1;
1119 break;
1120 case 0x3:
1121 insn->eaBase = (EABase)(insn->eaRegBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001122 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001123 return -1;
1124 break;
1125 }
1126 break;
1127 case 4:
1128 case 8:
1129 insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1130
1131 switch (mod) {
1132 case 0x0:
1133 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
1134 switch (rm) {
1135 case 0x4:
1136 case 0xc: /* in case REXW.b is set */
1137 insn->eaBase = (insn->addressSize == 4 ?
1138 EA_BASE_sib : EA_BASE_sib64);
1139 readSIB(insn);
Sean Callanan010b3732010-04-02 21:23:51 +00001140 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001141 return -1;
1142 break;
1143 case 0x5:
1144 insn->eaBase = EA_BASE_NONE;
1145 insn->eaDisplacement = EA_DISP_32;
Sean Callanan010b3732010-04-02 21:23:51 +00001146 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001147 return -1;
1148 break;
1149 default:
1150 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1151 break;
1152 }
1153 break;
1154 case 0x1:
1155 case 0x2:
1156 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1157 switch (rm) {
1158 case 0x4:
1159 case 0xc: /* in case REXW.b is set */
1160 insn->eaBase = EA_BASE_sib;
1161 readSIB(insn);
Sean Callanan010b3732010-04-02 21:23:51 +00001162 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001163 return -1;
1164 break;
1165 default:
1166 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
Sean Callanan010b3732010-04-02 21:23:51 +00001167 if (readDisplacement(insn))
Sean Callanan04cc3072009-12-19 02:59:52 +00001168 return -1;
1169 break;
1170 }
1171 break;
1172 case 0x3:
1173 insn->eaDisplacement = EA_DISP_NONE;
1174 insn->eaBase = (EABase)(insn->eaRegBase + rm);
1175 break;
1176 }
1177 break;
1178 } /* switch (insn->addressSize) */
1179
1180 return 0;
1181}
1182
1183#define GENERIC_FIXUP_FUNC(name, base, prefix) \
1184 static uint8_t name(struct InternalInstruction *insn, \
1185 OperandType type, \
1186 uint8_t index, \
1187 uint8_t *valid) { \
1188 *valid = 1; \
1189 switch (type) { \
1190 default: \
Sean Callanan010b3732010-04-02 21:23:51 +00001191 debug("Unhandled register type"); \
1192 *valid = 0; \
1193 return 0; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001194 case TYPE_Rv: \
1195 return base + index; \
1196 case TYPE_R8: \
Sean Callanan010b3732010-04-02 21:23:51 +00001197 if (insn->rexPrefix && \
Sean Callanan04cc3072009-12-19 02:59:52 +00001198 index >= 4 && index <= 7) { \
1199 return prefix##_SPL + (index - 4); \
1200 } else { \
1201 return prefix##_AL + index; \
1202 } \
1203 case TYPE_R16: \
1204 return prefix##_AX + index; \
1205 case TYPE_R32: \
1206 return prefix##_EAX + index; \
1207 case TYPE_R64: \
1208 return prefix##_RAX + index; \
Sean Callananc3fd5232011-03-15 01:23:15 +00001209 case TYPE_XMM256: \
1210 return prefix##_YMM0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001211 case TYPE_XMM128: \
1212 case TYPE_XMM64: \
1213 case TYPE_XMM32: \
1214 case TYPE_XMM: \
1215 return prefix##_XMM0 + index; \
1216 case TYPE_MM64: \
1217 case TYPE_MM32: \
1218 case TYPE_MM: \
Sean Callanan010b3732010-04-02 21:23:51 +00001219 if (index > 7) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001220 *valid = 0; \
1221 return prefix##_MM0 + index; \
1222 case TYPE_SEGMENTREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001223 if (index > 5) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001224 *valid = 0; \
1225 return prefix##_ES + index; \
1226 case TYPE_DEBUGREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001227 if (index > 7) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001228 *valid = 0; \
1229 return prefix##_DR0 + index; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001230 case TYPE_CONTROLREG: \
Sean Callanan010b3732010-04-02 21:23:51 +00001231 if (index > 8) \
Sean Callanan04cc3072009-12-19 02:59:52 +00001232 *valid = 0; \
Sean Callanane7e1cf92010-05-06 20:59:00 +00001233 return prefix##_CR0 + index; \
Sean Callanan04cc3072009-12-19 02:59:52 +00001234 } \
1235 }
1236
1237/*
1238 * fixup*Value - Consults an operand type to determine the meaning of the
1239 * reg or R/M field. If the operand is an XMM operand, for example, an
1240 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1241 * misinterpret it as.
1242 *
1243 * @param insn - The instruction containing the operand.
1244 * @param type - The operand type.
1245 * @param index - The existing value of the field as reported by readModRM().
1246 * @param valid - The address of a uint8_t. The target is set to 1 if the
1247 * field is valid for the register class; 0 if not.
Sean Callanan010b3732010-04-02 21:23:51 +00001248 * @return - The proper value.
Sean Callanan04cc3072009-12-19 02:59:52 +00001249 */
Sean Callanan2f9443f2009-12-22 02:07:42 +00001250GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG)
Sean Callanan04cc3072009-12-19 02:59:52 +00001251GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG)
1252
1253/*
1254 * fixupReg - Consults an operand specifier to determine which of the
1255 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1256 *
1257 * @param insn - See fixup*Value().
1258 * @param op - The operand specifier.
1259 * @return - 0 if fixup was successful; -1 if the register returned was
1260 * invalid for its class.
1261 */
1262static int fixupReg(struct InternalInstruction *insn,
Benjamin Kramerde0a4fb2010-10-23 09:10:44 +00001263 const struct OperandSpecifier *op) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001264 uint8_t valid;
1265
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001266 dbgprintf(insn, "fixupReg()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001267
1268 switch ((OperandEncoding)op->encoding) {
1269 default:
Sean Callanan010b3732010-04-02 21:23:51 +00001270 debug("Expected a REG or R/M encoding in fixupReg");
1271 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +00001272 case ENCODING_VVVV:
1273 insn->vvvv = (Reg)fixupRegValue(insn,
1274 (OperandType)op->type,
1275 insn->vvvv,
1276 &valid);
1277 if (!valid)
1278 return -1;
1279 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001280 case ENCODING_REG:
1281 insn->reg = (Reg)fixupRegValue(insn,
1282 (OperandType)op->type,
1283 insn->reg - insn->regBase,
1284 &valid);
1285 if (!valid)
1286 return -1;
1287 break;
1288 case ENCODING_RM:
1289 if (insn->eaBase >= insn->eaRegBase) {
1290 insn->eaBase = (EABase)fixupRMValue(insn,
1291 (OperandType)op->type,
1292 insn->eaBase - insn->eaRegBase,
1293 &valid);
1294 if (!valid)
1295 return -1;
1296 }
1297 break;
1298 }
1299
1300 return 0;
1301}
1302
1303/*
1304 * readOpcodeModifier - Reads an operand from the opcode field of an
1305 * instruction. Handles AddRegFrm instructions.
1306 *
1307 * @param insn - The instruction whose opcode field is to be read.
1308 * @param inModRM - Indicates that the opcode field is to be read from the
1309 * ModR/M extension; useful for escape opcodes
Sean Callanan010b3732010-04-02 21:23:51 +00001310 * @return - 0 on success; nonzero otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +00001311 */
Sean Callanan010b3732010-04-02 21:23:51 +00001312static int readOpcodeModifier(struct InternalInstruction* insn) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001313 dbgprintf(insn, "readOpcodeModifier()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001314
1315 if (insn->consumedOpcodeModifier)
Sean Callanan010b3732010-04-02 21:23:51 +00001316 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001317
1318 insn->consumedOpcodeModifier = TRUE;
1319
Sean Callanan010b3732010-04-02 21:23:51 +00001320 switch (insn->spec->modifierType) {
Sean Callanan04cc3072009-12-19 02:59:52 +00001321 default:
Sean Callanan010b3732010-04-02 21:23:51 +00001322 debug("Unknown modifier type.");
1323 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001324 case MODIFIER_NONE:
Sean Callanan010b3732010-04-02 21:23:51 +00001325 debug("No modifier but an operand expects one.");
1326 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001327 case MODIFIER_OPCODE:
1328 insn->opcodeModifier = insn->opcode - insn->spec->modifierBase;
Sean Callanan010b3732010-04-02 21:23:51 +00001329 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001330 case MODIFIER_MODRM:
1331 insn->opcodeModifier = insn->modRM - insn->spec->modifierBase;
Sean Callanan010b3732010-04-02 21:23:51 +00001332 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001333 }
1334}
1335
1336/*
1337 * readOpcodeRegister - Reads an operand from the opcode field of an
1338 * instruction and interprets it appropriately given the operand width.
1339 * Handles AddRegFrm instructions.
1340 *
1341 * @param insn - See readOpcodeModifier().
1342 * @param size - The width (in bytes) of the register being specified.
1343 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1344 * RAX.
Sean Callanan010b3732010-04-02 21:23:51 +00001345 * @return - 0 on success; nonzero otherwise.
Sean Callanan04cc3072009-12-19 02:59:52 +00001346 */
Sean Callanan010b3732010-04-02 21:23:51 +00001347static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001348 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001349
Sean Callanan010b3732010-04-02 21:23:51 +00001350 if (readOpcodeModifier(insn))
1351 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001352
1353 if (size == 0)
1354 size = insn->registerSize;
1355
1356 switch (size) {
1357 case 1:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001358 insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3)
1359 | insn->opcodeModifier));
Sean Callanan010b3732010-04-02 21:23:51 +00001360 if (insn->rexPrefix &&
1361 insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1362 insn->opcodeRegister < MODRM_REG_AL + 0x8) {
Sean Callanan2f9443f2009-12-22 02:07:42 +00001363 insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1364 + (insn->opcodeRegister - MODRM_REG_AL - 4));
Sean Callanan04cc3072009-12-19 02:59:52 +00001365 }
1366
1367 break;
1368 case 2:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001369 insn->opcodeRegister = (Reg)(MODRM_REG_AX
1370 + ((bFromREX(insn->rexPrefix) << 3)
1371 | insn->opcodeModifier));
Sean Callanan04cc3072009-12-19 02:59:52 +00001372 break;
1373 case 4:
Sean Callanan010b3732010-04-02 21:23:51 +00001374 insn->opcodeRegister = (Reg)(MODRM_REG_EAX
Sean Callanan2f9443f2009-12-22 02:07:42 +00001375 + ((bFromREX(insn->rexPrefix) << 3)
1376 | insn->opcodeModifier));
Sean Callanan04cc3072009-12-19 02:59:52 +00001377 break;
1378 case 8:
Sean Callanan2f9443f2009-12-22 02:07:42 +00001379 insn->opcodeRegister = (Reg)(MODRM_REG_RAX
1380 + ((bFromREX(insn->rexPrefix) << 3)
1381 | insn->opcodeModifier));
Sean Callanan04cc3072009-12-19 02:59:52 +00001382 break;
1383 }
Sean Callanan010b3732010-04-02 21:23:51 +00001384
1385 return 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001386}
1387
1388/*
1389 * readImmediate - Consumes an immediate operand from an instruction, given the
1390 * desired operand size.
1391 *
1392 * @param insn - The instruction whose operand is to be read.
1393 * @param size - The width (in bytes) of the operand.
1394 * @return - 0 if the immediate was successfully consumed; nonzero
1395 * otherwise.
1396 */
1397static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1398 uint8_t imm8;
1399 uint16_t imm16;
1400 uint32_t imm32;
1401 uint64_t imm64;
1402
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001403 dbgprintf(insn, "readImmediate()");
Sean Callanan04cc3072009-12-19 02:59:52 +00001404
Sean Callanan010b3732010-04-02 21:23:51 +00001405 if (insn->numImmediatesConsumed == 2) {
1406 debug("Already consumed two immediates");
1407 return -1;
1408 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001409
1410 if (size == 0)
1411 size = insn->immediateSize;
1412 else
1413 insn->immediateSize = size;
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +00001414 insn->immediateOffset = insn->readerCursor - insn->startLocation;
Sean Callanan04cc3072009-12-19 02:59:52 +00001415
1416 switch (size) {
1417 case 1:
1418 if (consumeByte(insn, &imm8))
1419 return -1;
1420 insn->immediates[insn->numImmediatesConsumed] = imm8;
1421 break;
1422 case 2:
1423 if (consumeUInt16(insn, &imm16))
1424 return -1;
1425 insn->immediates[insn->numImmediatesConsumed] = imm16;
1426 break;
1427 case 4:
1428 if (consumeUInt32(insn, &imm32))
1429 return -1;
1430 insn->immediates[insn->numImmediatesConsumed] = imm32;
1431 break;
1432 case 8:
1433 if (consumeUInt64(insn, &imm64))
1434 return -1;
1435 insn->immediates[insn->numImmediatesConsumed] = imm64;
1436 break;
1437 }
1438
1439 insn->numImmediatesConsumed++;
1440
1441 return 0;
1442}
1443
1444/*
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001445 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
Sean Callananc3fd5232011-03-15 01:23:15 +00001446 *
1447 * @param insn - The instruction whose operand is to be read.
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001448 * @return - 0 if the vvvv was successfully consumed; nonzero
Sean Callananc3fd5232011-03-15 01:23:15 +00001449 * otherwise.
1450 */
1451static int readVVVV(struct InternalInstruction* insn) {
1452 dbgprintf(insn, "readVVVV()");
1453
1454 if (insn->vexSize == 3)
1455 insn->vvvv = vvvvFromVEX3of3(insn->vexPrefix[2]);
1456 else if (insn->vexSize == 2)
1457 insn->vvvv = vvvvFromVEX2of2(insn->vexPrefix[1]);
1458 else
1459 return -1;
1460
Craig Topper0d0be472011-10-03 08:14:29 +00001461 if (insn->mode != MODE_64BIT)
1462 insn->vvvv &= 0x7;
1463
Sean Callananc3fd5232011-03-15 01:23:15 +00001464 return 0;
1465}
1466
1467/*
Sean Callanan04cc3072009-12-19 02:59:52 +00001468 * readOperands - Consults the specifier for an instruction and consumes all
1469 * operands for that instruction, interpreting them as it goes.
1470 *
1471 * @param insn - The instruction whose operands are to be read and interpreted.
1472 * @return - 0 if all operands could be read; nonzero otherwise.
1473 */
1474static int readOperands(struct InternalInstruction* insn) {
1475 int index;
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001476 int hasVVVV, needVVVV;
Craig Topper2ba766a2011-12-30 06:23:39 +00001477 int sawRegImm = 0;
Sean Callanan04cc3072009-12-19 02:59:52 +00001478
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001479 dbgprintf(insn, "readOperands()");
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001480
1481 /* If non-zero vvvv specified, need to make sure one of the operands
1482 uses it. */
1483 hasVVVV = !readVVVV(insn);
1484 needVVVV = hasVVVV && (insn->vvvv != 0);
Sean Callanan04cc3072009-12-19 02:59:52 +00001485
1486 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
1487 switch (insn->spec->operands[index].encoding) {
1488 case ENCODING_NONE:
1489 break;
1490 case ENCODING_REG:
1491 case ENCODING_RM:
1492 if (readModRM(insn))
1493 return -1;
1494 if (fixupReg(insn, &insn->spec->operands[index]))
1495 return -1;
1496 break;
1497 case ENCODING_CB:
1498 case ENCODING_CW:
1499 case ENCODING_CD:
1500 case ENCODING_CP:
1501 case ENCODING_CO:
1502 case ENCODING_CT:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001503 dbgprintf(insn, "We currently don't hande code-offset encodings");
Sean Callanan04cc3072009-12-19 02:59:52 +00001504 return -1;
1505 case ENCODING_IB:
Craig Topper2ba766a2011-12-30 06:23:39 +00001506 if (sawRegImm) {
Benjamin Kramer9c48f262012-01-04 22:06:45 +00001507 /* Saw a register immediate so don't read again and instead split the
1508 previous immediate. FIXME: This is a hack. */
Benjamin Kramer47aecca2012-01-01 17:55:36 +00001509 insn->immediates[insn->numImmediatesConsumed] =
1510 insn->immediates[insn->numImmediatesConsumed - 1] & 0xf;
1511 ++insn->numImmediatesConsumed;
Craig Topper2ba766a2011-12-30 06:23:39 +00001512 break;
1513 }
Sean Callanan04cc3072009-12-19 02:59:52 +00001514 if (readImmediate(insn, 1))
1515 return -1;
Sean Callanan1efe6612010-04-07 21:42:19 +00001516 if (insn->spec->operands[index].type == TYPE_IMM3 &&
1517 insn->immediates[insn->numImmediatesConsumed - 1] > 7)
1518 return -1;
Craig Topper2ba766a2011-12-30 06:23:39 +00001519 if (insn->spec->operands[index].type == TYPE_XMM128 ||
1520 insn->spec->operands[index].type == TYPE_XMM256)
1521 sawRegImm = 1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001522 break;
1523 case ENCODING_IW:
1524 if (readImmediate(insn, 2))
1525 return -1;
1526 break;
1527 case ENCODING_ID:
1528 if (readImmediate(insn, 4))
1529 return -1;
1530 break;
1531 case ENCODING_IO:
1532 if (readImmediate(insn, 8))
1533 return -1;
1534 break;
1535 case ENCODING_Iv:
Sean Callanan010b3732010-04-02 21:23:51 +00001536 if (readImmediate(insn, insn->immediateSize))
1537 return -1;
Chris Lattnerd4758fc2010-04-16 21:15:15 +00001538 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001539 case ENCODING_Ia:
Sean Callanan010b3732010-04-02 21:23:51 +00001540 if (readImmediate(insn, insn->addressSize))
1541 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001542 break;
1543 case ENCODING_RB:
Sean Callanan010b3732010-04-02 21:23:51 +00001544 if (readOpcodeRegister(insn, 1))
1545 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001546 break;
1547 case ENCODING_RW:
Sean Callanan010b3732010-04-02 21:23:51 +00001548 if (readOpcodeRegister(insn, 2))
1549 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001550 break;
1551 case ENCODING_RD:
Sean Callanan010b3732010-04-02 21:23:51 +00001552 if (readOpcodeRegister(insn, 4))
1553 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001554 break;
1555 case ENCODING_RO:
Sean Callanan010b3732010-04-02 21:23:51 +00001556 if (readOpcodeRegister(insn, 8))
1557 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001558 break;
1559 case ENCODING_Rv:
Sean Callanan010b3732010-04-02 21:23:51 +00001560 if (readOpcodeRegister(insn, 0))
1561 return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001562 break;
1563 case ENCODING_I:
Sean Callanan010b3732010-04-02 21:23:51 +00001564 if (readOpcodeModifier(insn))
1565 return -1;
Sean Callananc3fd5232011-03-15 01:23:15 +00001566 break;
1567 case ENCODING_VVVV:
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001568 needVVVV = 0; /* Mark that we have found a VVVV operand. */
1569 if (!hasVVVV)
Sean Callananc3fd5232011-03-15 01:23:15 +00001570 return -1;
1571 if (fixupReg(insn, &insn->spec->operands[index]))
1572 return -1;
1573 break;
Sean Callanan04cc3072009-12-19 02:59:52 +00001574 case ENCODING_DUP:
1575 break;
1576 default:
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001577 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan04cc3072009-12-19 02:59:52 +00001578 return -1;
1579 }
1580 }
Craig Topper8dd7bbc2011-09-13 07:37:44 +00001581
1582 /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
1583 if (needVVVV) return -1;
Sean Callanan04cc3072009-12-19 02:59:52 +00001584
1585 return 0;
1586}
1587
1588/*
1589 * decodeInstruction - Reads and interprets a full instruction provided by the
1590 * user.
1591 *
1592 * @param insn - A pointer to the instruction to be populated. Must be
1593 * pre-allocated.
1594 * @param reader - The function to be used to read the instruction's bytes.
1595 * @param readerArg - A generic argument to be passed to the reader to store
1596 * any internal state.
1597 * @param logger - If non-NULL, the function to be used to write log messages
1598 * and warnings.
1599 * @param loggerArg - A generic argument to be passed to the logger to store
1600 * any internal state.
1601 * @param startLoc - The address (in the reader's address space) of the first
1602 * byte in the instruction.
1603 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1604 * decode the instruction in.
1605 * @return - 0 if the instruction's memory could be read; nonzero if
1606 * not.
1607 */
1608int decodeInstruction(struct InternalInstruction* insn,
1609 byteReader_t reader,
1610 void* readerArg,
1611 dlog_t logger,
1612 void* loggerArg,
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001613 void* miiArg,
Sean Callanan04cc3072009-12-19 02:59:52 +00001614 uint64_t startLoc,
1615 DisassemblerMode mode) {
Daniel Dunbarc745a622009-12-19 03:31:50 +00001616 memset(insn, 0, sizeof(struct InternalInstruction));
Sean Callanan04cc3072009-12-19 02:59:52 +00001617
1618 insn->reader = reader;
1619 insn->readerArg = readerArg;
1620 insn->dlog = logger;
1621 insn->dlogArg = loggerArg;
1622 insn->startLocation = startLoc;
1623 insn->readerCursor = startLoc;
1624 insn->mode = mode;
1625 insn->numImmediatesConsumed = 0;
1626
1627 if (readPrefixes(insn) ||
1628 readOpcode(insn) ||
Benjamin Kramer478e8de2012-02-11 14:50:54 +00001629 getID(insn, miiArg) ||
Sean Callanan04cc3072009-12-19 02:59:52 +00001630 insn->instructionID == 0 ||
1631 readOperands(insn))
1632 return -1;
1633
1634 insn->length = insn->readerCursor - insn->startLocation;
1635
Benjamin Kramer4f672272010-03-18 12:18:36 +00001636 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1637 startLoc, insn->readerCursor, insn->length);
Sean Callanan04cc3072009-12-19 02:59:52 +00001638
1639 if (insn->length > 15)
Nuno Lopes3ed6d602009-12-19 12:07:00 +00001640 dbgprintf(insn, "Instruction exceeds 15-byte limit");
Sean Callanan04cc3072009-12-19 02:59:52 +00001641
1642 return 0;
1643}