blob: 118a7a389fd67452d156a9b9ec67de9be5745af2 [file] [log] [blame]
Sean Callanan8ed9f512009-12-19 02:59:52 +00001/*===- X86DisassemblerDecoder.c - Disassembler decoder -------------*- C -*-==*
2 *
3 * The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
7 *
8 *===----------------------------------------------------------------------===*
9 *
10 * This file is part of the X86 Disassembler.
11 * It contains the implementation of the instruction decoder.
12 * Documentation for the disassembler can be found in X86Disassembler.h.
13 *
14 *===----------------------------------------------------------------------===*/
15
Daniel Dunbarc8143462009-12-19 17:11:53 +000016#if 0
17
Sean Callanan8ed9f512009-12-19 02:59:52 +000018#include <assert.h> /* for assert() */
19#include <stdarg.h> /* for va_*() */
20#include <stdio.h> /* for vsnprintf() */
21#include <stdlib.h> /* for exit() */
Daniel Dunbar71f842d2009-12-19 03:31:50 +000022#include <string.h> /* for memset() */
Sean Callanan8ed9f512009-12-19 02:59:52 +000023
24#include "X86DisassemblerDecoder.h"
25
26#include "X86GenDisassemblerTables.inc"
27
28#define TRUE 1
29#define FALSE 0
30
31#ifdef __GNUC__
32#define NORETURN __attribute__((noreturn))
33#else
34#define NORETURN
35#endif
36
37#define unreachable(s) \
38 do { \
39 fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, s); \
40 exit(-1); \
41 } while (0);
42
43/*
44 * contextForAttrs - Client for the instruction context table. Takes a set of
45 * attributes and returns the appropriate decode context.
46 *
47 * @param attrMask - Attributes, from the enumeration attributeBits.
48 * @return - The InstructionContext to use when looking up an
49 * an instruction with these attributes.
50 */
51static inline InstructionContext contextForAttrs(uint8_t attrMask) {
52 return CONTEXTS_SYM[attrMask];
53}
54
55/*
56 * modRMRequired - Reads the appropriate instruction table to determine whether
57 * the ModR/M byte is required to decode a particular instruction.
58 *
59 * @param type - The opcode type (i.e., how many bytes it has).
60 * @param insnContext - The context for the instruction, as returned by
61 * contextForAttrs.
62 * @param opcode - The last byte of the instruction's opcode, not counting
63 * ModR/M extensions and escapes.
64 * @return - TRUE if the ModR/M byte is required, FALSE otherwise.
65 */
66static inline int modRMRequired(OpcodeType type,
67 InstructionContext insnContext,
68 uint8_t opcode) {
69 const struct ContextDecision* decision;
70
71 switch (type) {
72 case ONEBYTE:
73 decision = &ONEBYTE_SYM;
74 break;
75 case TWOBYTE:
76 decision = &TWOBYTE_SYM;
77 break;
78 case THREEBYTE_38:
79 decision = &THREEBYTE38_SYM;
80 break;
81 case THREEBYTE_3A:
82 decision = &THREEBYTE3A_SYM;
83 break;
84 }
85
86 return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].
87 modrm_type != MODRM_ONEENTRY;
88
89 unreachable("Unknown opcode type");
90 return 0;
91}
92
93/*
94 * decode - Reads the appropriate instruction table to obtain the unique ID of
95 * an instruction.
96 *
97 * @param type - See modRMRequired().
98 * @param insnContext - See modRMRequired().
99 * @param opcode - See modRMRequired().
100 * @param modRM - The ModR/M byte if required, or any value if not.
101 */
102static inline InstrUID decode(OpcodeType type,
103 InstructionContext insnContext,
104 uint8_t opcode,
105 uint8_t modRM) {
106 struct ModRMDecision* dec;
107
108 switch (type) {
109 default:
110 unreachable("Unknown opcode type");
111 case ONEBYTE:
112 dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
113 break;
114 case TWOBYTE:
115 dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
116 break;
117 case THREEBYTE_38:
118 dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
119 break;
120 case THREEBYTE_3A:
121 dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
122 break;
123 }
124
125 switch (dec->modrm_type) {
126 default:
127 unreachable("Corrupt table! Unknown modrm_type");
128 case MODRM_ONEENTRY:
129 return dec->instructionIDs[0];
130 case MODRM_SPLITRM:
131 if (modFromModRM(modRM) == 0x3)
132 return dec->instructionIDs[1];
133 else
134 return dec->instructionIDs[0];
135 case MODRM_FULL:
136 return dec->instructionIDs[modRM];
137 }
138
139 return 0;
140}
141
142/*
143 * specifierForUID - Given a UID, returns the name and operand specification for
144 * that instruction.
145 *
146 * @param uid - The unique ID for the instruction. This should be returned by
147 * decode(); specifierForUID will not check bounds.
148 * @return - A pointer to the specification for that instruction.
149 */
150static inline struct InstructionSpecifier* specifierForUID(InstrUID uid) {
151 return &INSTRUCTIONS_SYM[uid];
152}
153
154/*
155 * consumeByte - Uses the reader function provided by the user to consume one
156 * byte from the instruction's memory and advance the cursor.
157 *
158 * @param insn - The instruction with the reader function to use. The cursor
159 * for this instruction is advanced.
160 * @param byte - A pointer to a pre-allocated memory buffer to be populated
161 * with the data read.
162 * @return - 0 if the read was successful; nonzero otherwise.
163 */
164static inline int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
165 int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
166
167 if (!ret)
168 ++(insn->readerCursor);
169
170 return ret;
171}
172
173/*
174 * lookAtByte - Like consumeByte, but does not advance the cursor.
175 *
176 * @param insn - See consumeByte().
177 * @param byte - See consumeByte().
178 * @return - See consumeByte().
179 */
180static inline int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
181 return insn->reader(insn->readerArg, byte, insn->readerCursor);
182}
183
184static inline void unconsumeByte(struct InternalInstruction* insn) {
185 insn->readerCursor--;
186}
187
188#define CONSUME_FUNC(name, type) \
189 static inline int name(struct InternalInstruction* insn, type* ptr) { \
190 type combined = 0; \
191 unsigned offset; \
192 for (offset = 0; offset < sizeof(type); ++offset) { \
193 uint8_t byte; \
194 int ret = insn->reader(insn->readerArg, \
195 &byte, \
196 insn->readerCursor + offset); \
197 if (ret) \
198 return ret; \
199 combined = combined | ((type)byte << ((type)offset * 8)); \
200 } \
201 *ptr = combined; \
202 insn->readerCursor += sizeof(type); \
203 return 0; \
204 }
205
206/*
207 * consume* - Use the reader function provided by the user to consume data
208 * values of various sizes from the instruction's memory and advance the
209 * cursor appropriately. These readers perform endian conversion.
210 *
211 * @param insn - See consumeByte().
212 * @param ptr - A pointer to a pre-allocated memory of appropriate size to
213 * be populated with the data read.
214 * @return - See consumeByte().
215 */
216CONSUME_FUNC(consumeInt8, int8_t)
217CONSUME_FUNC(consumeInt16, int16_t)
218CONSUME_FUNC(consumeInt32, int32_t)
219CONSUME_FUNC(consumeUInt16, uint16_t)
220CONSUME_FUNC(consumeUInt32, uint32_t)
221CONSUME_FUNC(consumeUInt64, uint64_t)
222
223/*
Nuno Lopes392bbd92009-12-19 12:07:00 +0000224 * dbgprintf - Uses the logging function provided by the user to log a single
Sean Callanan8ed9f512009-12-19 02:59:52 +0000225 * message, typically without a carriage-return.
226 *
227 * @param insn - The instruction containing the logging function.
228 * @param format - See printf().
229 * @param ... - See printf().
230 */
Nuno Lopes392bbd92009-12-19 12:07:00 +0000231static inline void dbgprintf(struct InternalInstruction* insn,
Sean Callanan8ed9f512009-12-19 02:59:52 +0000232 const char* format,
233 ...) {
234 char buffer[256];
235 va_list ap;
236
237 if (!insn->dlog)
238 return;
239
240 va_start(ap, format);
241 (void)vsnprintf(buffer, sizeof(buffer), format, ap);
242 va_end(ap);
243
244 insn->dlog(insn->dlogArg, buffer);
245
246 return;
247}
248
249/*
250 * setPrefixPresent - Marks that a particular prefix is present at a particular
251 * location.
252 *
253 * @param insn - The instruction to be marked as having the prefix.
254 * @param prefix - The prefix that is present.
255 * @param location - The location where the prefix is located (in the address
256 * space of the instruction's reader).
257 */
258static inline void setPrefixPresent(struct InternalInstruction* insn,
259 uint8_t prefix,
260 uint64_t location)
261{
262 insn->prefixPresent[prefix] = 1;
263 insn->prefixLocations[prefix] = location;
264}
265
266/*
267 * isPrefixAtLocation - Queries an instruction to determine whether a prefix is
268 * present at a given location.
269 *
270 * @param insn - The instruction to be queried.
271 * @param prefix - The prefix.
272 * @param location - The location to query.
273 * @return - Whether the prefix is at that location.
274 */
275static inline BOOL isPrefixAtLocation(struct InternalInstruction* insn,
276 uint8_t prefix,
277 uint64_t location)
278{
279 if (insn->prefixPresent[prefix] == 1 &&
280 insn->prefixLocations[prefix] == location)
281 return TRUE;
282 else
283 return FALSE;
284}
285
286/*
287 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
288 * instruction as having them. Also sets the instruction's default operand,
289 * address, and other relevant data sizes to report operands correctly.
290 *
291 * @param insn - The instruction whose prefixes are to be read.
292 * @return - 0 if the instruction could be read until the end of the prefix
293 * bytes, and no prefixes conflicted; nonzero otherwise.
294 */
295static int readPrefixes(struct InternalInstruction* insn) {
296 BOOL isPrefix = TRUE;
297 BOOL prefixGroups[4] = { FALSE };
298 uint64_t prefixLocation;
299 uint8_t byte;
300
301 BOOL hasAdSize = FALSE;
302 BOOL hasOpSize = FALSE;
303
Nuno Lopes392bbd92009-12-19 12:07:00 +0000304 dbgprintf(insn, "readPrefixes()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000305
306 while (isPrefix) {
307 prefixLocation = insn->readerCursor;
308
309 if (consumeByte(insn, &byte))
310 return -1;
311
312 switch (byte) {
313 case 0xf0: /* LOCK */
314 case 0xf2: /* REPNE/REPNZ */
315 case 0xf3: /* REP or REPE/REPZ */
316 if (prefixGroups[0])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000317 dbgprintf(insn, "Redundant Group 1 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000318 prefixGroups[0] = TRUE;
319 setPrefixPresent(insn, byte, prefixLocation);
320 break;
321 case 0x2e: /* CS segment override -OR- Branch not taken */
322 case 0x36: /* SS segment override -OR- Branch taken */
323 case 0x3e: /* DS segment override */
324 case 0x26: /* ES segment override */
325 case 0x64: /* FS segment override */
326 case 0x65: /* GS segment override */
327 switch (byte) {
328 case 0x2e:
329 insn->segmentOverride = SEG_OVERRIDE_CS;
330 break;
331 case 0x36:
332 insn->segmentOverride = SEG_OVERRIDE_SS;
333 break;
334 case 0x3e:
335 insn->segmentOverride = SEG_OVERRIDE_DS;
336 break;
337 case 0x26:
338 insn->segmentOverride = SEG_OVERRIDE_ES;
339 break;
340 case 0x64:
341 insn->segmentOverride = SEG_OVERRIDE_FS;
342 break;
343 case 0x65:
344 insn->segmentOverride = SEG_OVERRIDE_GS;
345 break;
346 default:
347 unreachable("Unhandled override");
348 }
349 if (prefixGroups[1])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000350 dbgprintf(insn, "Redundant Group 2 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000351 prefixGroups[1] = TRUE;
352 setPrefixPresent(insn, byte, prefixLocation);
353 break;
354 case 0x66: /* Operand-size override */
355 if (prefixGroups[2])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000356 dbgprintf(insn, "Redundant Group 3 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000357 prefixGroups[2] = TRUE;
358 hasOpSize = TRUE;
359 setPrefixPresent(insn, byte, prefixLocation);
360 break;
361 case 0x67: /* Address-size override */
362 if (prefixGroups[3])
Nuno Lopes392bbd92009-12-19 12:07:00 +0000363 dbgprintf(insn, "Redundant Group 4 prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000364 prefixGroups[3] = TRUE;
365 hasAdSize = TRUE;
366 setPrefixPresent(insn, byte, prefixLocation);
367 break;
368 default: /* Not a prefix byte */
369 isPrefix = FALSE;
370 break;
371 }
372
373 if (isPrefix)
Nuno Lopes392bbd92009-12-19 12:07:00 +0000374 dbgprintf(insn, "Found prefix 0x%hhx", byte);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000375 }
376
377 if (insn->mode == MODE_64BIT) {
378 if ((byte & 0xf0) == 0x40) {
379 uint8_t opcodeByte;
380
381 if(lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000382 dbgprintf(insn, "Redundant REX prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000383 return -1;
384 }
385
386 insn->rexPrefix = byte;
387 insn->necessaryPrefixLocation = insn->readerCursor - 2;
388
Nuno Lopes392bbd92009-12-19 12:07:00 +0000389 dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000390 } else {
391 unconsumeByte(insn);
392 insn->necessaryPrefixLocation = insn->readerCursor - 1;
393 }
394 } else {
395 unconsumeByte(insn);
396 }
397
398 if (insn->mode == MODE_16BIT) {
399 insn->registerSize = (hasOpSize ? 4 : 2);
400 insn->addressSize = (hasAdSize ? 4 : 2);
401 insn->displacementSize = (hasAdSize ? 4 : 2);
402 insn->immediateSize = (hasOpSize ? 4 : 2);
403 } else if (insn->mode == MODE_32BIT) {
404 insn->registerSize = (hasOpSize ? 2 : 4);
405 insn->addressSize = (hasAdSize ? 2 : 4);
406 insn->displacementSize = (hasAdSize ? 2 : 4);
407 insn->immediateSize = (hasAdSize ? 2 : 4);
408 } else if (insn->mode == MODE_64BIT) {
409 if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
410 insn->registerSize = 8;
411 insn->addressSize = (hasAdSize ? 4 : 8);
412 insn->displacementSize = 4;
413 insn->immediateSize = 4;
414 } else if (insn->rexPrefix) {
415 insn->registerSize = (hasOpSize ? 2 : 4);
416 insn->addressSize = (hasAdSize ? 4 : 8);
417 insn->displacementSize = (hasOpSize ? 2 : 4);
418 insn->immediateSize = (hasOpSize ? 2 : 4);
419 } else {
420 insn->registerSize = (hasOpSize ? 2 : 4);
421 insn->addressSize = (hasAdSize ? 4 : 8);
422 insn->displacementSize = (hasOpSize ? 2 : 4);
423 insn->immediateSize = (hasOpSize ? 2 : 4);
424 }
425 }
426
427 return 0;
428}
429
430/*
431 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
432 * extended or escape opcodes).
433 *
434 * @param insn - The instruction whose opcode is to be read.
435 * @return - 0 if the opcode could be read successfully; nonzero otherwise.
436 */
437static int readOpcode(struct InternalInstruction* insn) {
438 /* Determine the length of the primary opcode */
439
440 uint8_t current;
441
Nuno Lopes392bbd92009-12-19 12:07:00 +0000442 dbgprintf(insn, "readOpcode()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000443
444 insn->opcodeType = ONEBYTE;
445 if (consumeByte(insn, &current))
446 return -1;
447
448 if (current == 0x0f) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000449 dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000450
451 insn->twoByteEscape = current;
452
453 if (consumeByte(insn, &current))
454 return -1;
455
456 if (current == 0x38) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000457 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000458
459 insn->threeByteEscape = current;
460
461 if (consumeByte(insn, &current))
462 return -1;
463
464 insn->opcodeType = THREEBYTE_38;
465 } else if (current == 0x3a) {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000466 dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
Sean Callanan8ed9f512009-12-19 02:59:52 +0000467
468 insn->threeByteEscape = current;
469
470 if (consumeByte(insn, &current))
471 return -1;
472
473 insn->opcodeType = THREEBYTE_3A;
474 } else {
Nuno Lopes392bbd92009-12-19 12:07:00 +0000475 dbgprintf(insn, "Didn't find a three-byte escape prefix");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000476
477 insn->opcodeType = TWOBYTE;
478 }
479 }
480
481 /*
482 * At this point we have consumed the full opcode.
483 * Anything we consume from here on must be unconsumed.
484 */
485
486 insn->opcode = current;
487
488 return 0;
489}
490
491static int readModRM(struct InternalInstruction* insn);
492
493/*
494 * getIDWithAttrMask - Determines the ID of an instruction, consuming
495 * the ModR/M byte as appropriate for extended and escape opcodes,
496 * and using a supplied attribute mask.
497 *
498 * @param instructionID - A pointer whose target is filled in with the ID of the
499 * instruction.
500 * @param insn - The instruction whose ID is to be determined.
501 * @param attrMask - The attribute mask to search.
502 * @return - 0 if the ModR/M could be read when needed or was not
503 * needed; nonzero otherwise.
504 */
505static int getIDWithAttrMask(uint16_t* instructionID,
506 struct InternalInstruction* insn,
507 uint8_t attrMask) {
508 BOOL hasModRMExtension;
509
510 uint8_t instructionClass;
511
512 instructionClass = contextForAttrs(attrMask);
513
514 hasModRMExtension = modRMRequired(insn->opcodeType,
515 instructionClass,
516 insn->opcode);
517
518 if (hasModRMExtension) {
519 readModRM(insn);
520
521 *instructionID = decode(insn->opcodeType,
522 instructionClass,
523 insn->opcode,
524 insn->modRM);
525 } else {
526 *instructionID = decode(insn->opcodeType,
527 instructionClass,
528 insn->opcode,
529 0);
530 }
531
532 return 0;
533}
534
535/*
536 * is16BitEquivalent - Determines whether two instruction names refer to
537 * equivalent instructions but one is 16-bit whereas the other is not.
538 *
539 * @param orig - The instruction that is not 16-bit
540 * @param equiv - The instruction that is 16-bit
541 */
542static BOOL is16BitEquvalent(const char* orig, const char* equiv) {
543 off_t i;
544
545 for(i = 0;; i++) {
546 if(orig[i] == '\0' && equiv[i] == '\0')
547 return TRUE;
548 if(orig[i] == '\0' || equiv[i] == '\0')
549 return FALSE;
550 if(orig[i] != equiv[i]) {
551 if((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
552 continue;
553 if((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
554 continue;
555 if((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
556 continue;
557 return FALSE;
558 }
559 }
560}
561
562/*
563 * is64BitEquivalent - Determines whether two instruction names refer to
564 * equivalent instructions but one is 64-bit whereas the other is not.
565 *
566 * @param orig - The instruction that is not 64-bit
567 * @param equiv - The instruction that is 64-bit
568 */
569static BOOL is64BitEquivalent(const char* orig, const char* equiv) {
570 off_t i;
571
572 for(i = 0;; i++) {
573 if(orig[i] == '\0' && equiv[i] == '\0')
574 return TRUE;
575 if(orig[i] == '\0' || equiv[i] == '\0')
576 return FALSE;
577 if(orig[i] != equiv[i]) {
578 if((orig[i] == 'W' || orig[i] == 'L') && equiv[i] == 'Q')
579 continue;
580 if((orig[i] == '1' || orig[i] == '3') && equiv[i] == '6')
581 continue;
582 if((orig[i] == '6' || orig[i] == '2') && equiv[i] == '4')
583 continue;
584 return FALSE;
585 }
586 }
587}
588
589
590/*
591 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
592 * appropriate for extended and escape opcodes. Determines the attributes and
593 * context for the instruction before doing so.
594 *
595 * @param insn - The instruction whose ID is to be determined.
596 * @return - 0 if the ModR/M could be read when needed or was not needed;
597 * nonzero otherwise.
598 */
599static int getID(struct InternalInstruction* insn) {
600 uint8_t attrMask;
601 uint16_t instructionID;
602
Nuno Lopes392bbd92009-12-19 12:07:00 +0000603 dbgprintf(insn, "getID()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000604
605 attrMask = ATTR_NONE;
606
607 if (insn->mode == MODE_64BIT)
608 attrMask |= ATTR_64BIT;
609
610 if (insn->rexPrefix & 0x08)
611 attrMask |= ATTR_REXW;
612
613 if (isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
614 attrMask |= ATTR_OPSIZE;
615 else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
616 attrMask |= ATTR_XS;
617 else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
618 attrMask |= ATTR_XD;
619
620 if(getIDWithAttrMask(&instructionID, insn, attrMask))
621 return -1;
622
623 /* The following clauses compensate for limitations of the tables. */
624
625 if ((attrMask & ATTR_XD) && (attrMask & ATTR_REXW)) {
626 /*
627 * Although for SSE instructions it is usually necessary to treat REX.W+F2
628 * as F2 for decode (in the absence of a 64BIT_REXW_XD category) there is
629 * an occasional instruction where F2 is incidental and REX.W is the more
630 * significant. If the decoded instruction is 32-bit and adding REX.W
631 * instead of F2 changes a 32 to a 64, we adopt the new encoding.
632 */
633
634 struct InstructionSpecifier* spec;
635 uint16_t instructionIDWithREXw;
636 struct InstructionSpecifier* specWithREXw;
637
638 spec = specifierForUID(instructionID);
639
640 if (getIDWithAttrMask(&instructionIDWithREXw,
641 insn,
642 attrMask & (~ATTR_XD))) {
643 /*
644 * Decoding with REX.w would yield nothing; give up and return original
645 * decode.
646 */
647
648 insn->instructionID = instructionID;
649 insn->spec = spec;
650 return 0;
651 }
652
653 specWithREXw = specifierForUID(instructionIDWithREXw);
654
655 if (is64BitEquivalent(spec->name, specWithREXw->name)) {
656 insn->instructionID = instructionIDWithREXw;
657 insn->spec = specWithREXw;
658 } else {
659 insn->instructionID = instructionID;
660 insn->spec = spec;
661 }
662 return 0;
663 }
664
665 if (insn->prefixPresent[0x66] && !(attrMask & ATTR_OPSIZE)) {
666 /*
667 * The instruction tables make no distinction between instructions that
668 * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
669 * particular spot (i.e., many MMX operations). In general we're
670 * conservative, but in the specific case where OpSize is present but not
671 * in the right place we check if there's a 16-bit operation.
672 */
673
674 struct InstructionSpecifier* spec;
675 uint16_t instructionIDWithOpsize;
676 struct InstructionSpecifier* specWithOpsize;
677
678 spec = specifierForUID(instructionID);
679
680 if (getIDWithAttrMask(&instructionIDWithOpsize,
681 insn,
682 attrMask | ATTR_OPSIZE)) {
683 /*
684 * ModRM required with OpSize but not present; give up and return version
685 * without OpSize set
686 */
687
688 insn->instructionID = instructionID;
689 insn->spec = spec;
690 return 0;
691 }
692
693 specWithOpsize = specifierForUID(instructionIDWithOpsize);
694
695 if (is16BitEquvalent(spec->name, specWithOpsize->name)) {
696 insn->instructionID = instructionIDWithOpsize;
697 insn->spec = specWithOpsize;
698 } else {
699 insn->instructionID = instructionID;
700 insn->spec = spec;
701 }
702 return 0;
703 }
704
705 insn->instructionID = instructionID;
706 insn->spec = specifierForUID(insn->instructionID);
707
708 return 0;
709}
710
711/*
712 * readSIB - Consumes the SIB byte to determine addressing information for an
713 * instruction.
714 *
715 * @param insn - The instruction whose SIB byte is to be read.
716 * @return - 0 if the SIB byte was successfully read; nonzero otherwise.
717 */
718static int readSIB(struct InternalInstruction* insn) {
719 SIBIndex sibIndexBase;
720 SIBBase sibBaseBase;
721 uint8_t index, base;
722
Nuno Lopes392bbd92009-12-19 12:07:00 +0000723 dbgprintf(insn, "readSIB()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000724
725 if (insn->consumedSIB)
726 return 0;
727
728 insn->consumedSIB = TRUE;
729
730 switch (insn->addressSize) {
731 case 2:
Nuno Lopes392bbd92009-12-19 12:07:00 +0000732 dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000733 return -1;
734 break;
735 case 4:
736 sibIndexBase = SIB_INDEX_EAX;
737 sibBaseBase = SIB_BASE_EAX;
738 break;
739 case 8:
740 sibIndexBase = SIB_INDEX_RAX;
741 sibBaseBase = SIB_BASE_RAX;
742 break;
743 }
744
745 if (consumeByte(insn, &insn->sib))
746 return -1;
747
748 index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
749
750 switch (index) {
751 case 0x4:
752 insn->sibIndex = SIB_INDEX_NONE;
753 break;
754 default:
755 insn->sibIndex = (EABase)(sibIndexBase + index);
756 if (insn->sibIndex == SIB_INDEX_sib ||
757 insn->sibIndex == SIB_INDEX_sib64)
758 insn->sibIndex = SIB_INDEX_NONE;
759 break;
760 }
761
762 switch (scaleFromSIB(insn->sib)) {
763 case 0:
764 insn->sibScale = 1;
765 break;
766 case 1:
767 insn->sibScale = 2;
768 break;
769 case 2:
770 insn->sibScale = 4;
771 break;
772 case 3:
773 insn->sibScale = 8;
774 break;
775 }
776
777 base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
778
779 switch (base) {
780 case 0x5:
781 switch (modFromModRM(insn->modRM)) {
782 case 0x0:
783 insn->eaDisplacement = EA_DISP_32;
784 insn->sibBase = SIB_BASE_NONE;
785 break;
786 case 0x1:
787 insn->eaDisplacement = EA_DISP_8;
788 insn->sibBase = (insn->addressSize == 4 ?
789 SIB_BASE_EBP : SIB_BASE_RBP);
790 break;
791 case 0x2:
792 insn->eaDisplacement = EA_DISP_32;
793 insn->sibBase = (insn->addressSize == 4 ?
794 SIB_BASE_EBP : SIB_BASE_RBP);
795 break;
796 case 0x3:
797 unreachable("Cannot have Mod = 0b11 and a SIB byte");
798 }
799 break;
800 default:
801 insn->sibBase = (EABase)(sibBaseBase + base);
802 break;
803 }
804
805 return 0;
806}
807
808/*
809 * readDisplacement - Consumes the displacement of an instruction.
810 *
811 * @param insn - The instruction whose displacement is to be read.
812 * @return - 0 if the displacement byte was successfully read; nonzero
813 * otherwise.
814 */
815static int readDisplacement(struct InternalInstruction* insn) {
816 int8_t d8;
817 int16_t d16;
818 int32_t d32;
819
Nuno Lopes392bbd92009-12-19 12:07:00 +0000820 dbgprintf(insn, "readDisplacement()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000821
822 if (insn->consumedDisplacement)
823 return 0;
824
825 insn->consumedDisplacement = TRUE;
826
827 switch (insn->eaDisplacement) {
828 case EA_DISP_NONE:
829 insn->consumedDisplacement = FALSE;
830 break;
831 case EA_DISP_8:
832 if (consumeInt8(insn, &d8))
833 return -1;
834 insn->displacement = d8;
835 break;
836 case EA_DISP_16:
837 if (consumeInt16(insn, &d16))
838 return -1;
839 insn->displacement = d16;
840 break;
841 case EA_DISP_32:
842 if (consumeInt32(insn, &d32))
843 return -1;
844 insn->displacement = d32;
845 break;
846 }
847
848 insn->consumedDisplacement = TRUE;
849 return 0;
850}
851
852/*
853 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
854 * displacement) for an instruction and interprets it.
855 *
856 * @param insn - The instruction whose addressing information is to be read.
857 * @return - 0 if the information was successfully read; nonzero otherwise.
858 */
859static int readModRM(struct InternalInstruction* insn) {
860 uint8_t mod, rm, reg;
861
Nuno Lopes392bbd92009-12-19 12:07:00 +0000862 dbgprintf(insn, "readModRM()");
Sean Callanan8ed9f512009-12-19 02:59:52 +0000863
864 if (insn->consumedModRM)
865 return 0;
866
867 consumeByte(insn, &insn->modRM);
868 insn->consumedModRM = TRUE;
869
870 mod = modFromModRM(insn->modRM);
871 rm = rmFromModRM(insn->modRM);
872 reg = regFromModRM(insn->modRM);
873
874 /*
875 * This goes by insn->registerSize to pick the correct register, which messes
876 * up if we're using (say) XMM or 8-bit register operands. That gets fixed in
877 * fixupReg().
878 */
879 switch (insn->registerSize) {
880 case 2:
881 insn->regBase = REG_AX;
882 insn->eaRegBase = EA_REG_AX;
883 break;
884 case 4:
885 insn->regBase = REG_EAX;
886 insn->eaRegBase = EA_REG_EAX;
887 break;
888 case 8:
889 insn->regBase = REG_RAX;
890 insn->eaRegBase = EA_REG_RAX;
891 break;
892 }
893
894 reg |= rFromREX(insn->rexPrefix) << 3;
895 rm |= bFromREX(insn->rexPrefix) << 3;
896
897 insn->reg = (Reg)(insn->regBase + reg);
898
899 switch (insn->addressSize) {
900 case 2:
901 insn->eaBaseBase = EA_BASE_BX_SI;
902
903 switch (mod) {
904 case 0x0:
905 if (rm == 0x6) {
906 insn->eaBase = EA_BASE_NONE;
907 insn->eaDisplacement = EA_DISP_16;
908 if(readDisplacement(insn))
909 return -1;
910 } else {
911 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
912 insn->eaDisplacement = EA_DISP_NONE;
913 }
914 break;
915 case 0x1:
916 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
917 insn->eaDisplacement = EA_DISP_8;
918 if(readDisplacement(insn))
919 return -1;
920 break;
921 case 0x2:
922 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
923 insn->eaDisplacement = EA_DISP_16;
924 if(readDisplacement(insn))
925 return -1;
926 break;
927 case 0x3:
928 insn->eaBase = (EABase)(insn->eaRegBase + rm);
929 if(readDisplacement(insn))
930 return -1;
931 break;
932 }
933 break;
934 case 4:
935 case 8:
936 insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
937
938 switch (mod) {
939 case 0x0:
940 insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
941 switch (rm) {
942 case 0x4:
943 case 0xc: /* in case REXW.b is set */
944 insn->eaBase = (insn->addressSize == 4 ?
945 EA_BASE_sib : EA_BASE_sib64);
946 readSIB(insn);
947 if(readDisplacement(insn))
948 return -1;
949 break;
950 case 0x5:
951 insn->eaBase = EA_BASE_NONE;
952 insn->eaDisplacement = EA_DISP_32;
953 if(readDisplacement(insn))
954 return -1;
955 break;
956 default:
957 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
958 break;
959 }
960 break;
961 case 0x1:
962 case 0x2:
963 insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
964 switch (rm) {
965 case 0x4:
966 case 0xc: /* in case REXW.b is set */
967 insn->eaBase = EA_BASE_sib;
968 readSIB(insn);
969 if(readDisplacement(insn))
970 return -1;
971 break;
972 default:
973 insn->eaBase = (EABase)(insn->eaBaseBase + rm);
974 if(readDisplacement(insn))
975 return -1;
976 break;
977 }
978 break;
979 case 0x3:
980 insn->eaDisplacement = EA_DISP_NONE;
981 insn->eaBase = (EABase)(insn->eaRegBase + rm);
982 break;
983 }
984 break;
985 } /* switch (insn->addressSize) */
986
987 return 0;
988}
989
990#define GENERIC_FIXUP_FUNC(name, base, prefix) \
991 static uint8_t name(struct InternalInstruction *insn, \
992 OperandType type, \
993 uint8_t index, \
994 uint8_t *valid) { \
995 *valid = 1; \
996 switch (type) { \
997 default: \
998 unreachable("Unhandled register type"); \
999 case TYPE_Rv: \
1000 return base + index; \
1001 case TYPE_R8: \
1002 if(insn->rexPrefix && \
1003 index >= 4 && index <= 7) { \
1004 return prefix##_SPL + (index - 4); \
1005 } else { \
1006 return prefix##_AL + index; \
1007 } \
1008 case TYPE_R16: \
1009 return prefix##_AX + index; \
1010 case TYPE_R32: \
1011 return prefix##_EAX + index; \
1012 case TYPE_R64: \
1013 return prefix##_RAX + index; \
1014 case TYPE_XMM128: \
1015 case TYPE_XMM64: \
1016 case TYPE_XMM32: \
1017 case TYPE_XMM: \
1018 return prefix##_XMM0 + index; \
1019 case TYPE_MM64: \
1020 case TYPE_MM32: \
1021 case TYPE_MM: \
1022 if(index > 7) \
1023 *valid = 0; \
1024 return prefix##_MM0 + index; \
1025 case TYPE_SEGMENTREG: \
1026 if(index > 5) \
1027 *valid = 0; \
1028 return prefix##_ES + index; \
1029 case TYPE_DEBUGREG: \
1030 if(index > 7) \
1031 *valid = 0; \
1032 return prefix##_DR0 + index; \
1033 case TYPE_CR32: \
1034 if(index > 7) \
1035 *valid = 0; \
1036 return prefix##_ECR0 + index; \
1037 case TYPE_CR64: \
1038 if(index > 8) \
1039 *valid = 0; \
1040 return prefix##_RCR0 + index; \
1041 } \
1042 }
1043
1044/*
1045 * fixup*Value - Consults an operand type to determine the meaning of the
1046 * reg or R/M field. If the operand is an XMM operand, for example, an
1047 * operand would be XMM0 instead of AX, which readModRM() would otherwise
1048 * misinterpret it as.
1049 *
1050 * @param insn - The instruction containing the operand.
1051 * @param type - The operand type.
1052 * @param index - The existing value of the field as reported by readModRM().
1053 * @param valid - The address of a uint8_t. The target is set to 1 if the
1054 * field is valid for the register class; 0 if not.
1055 */
1056GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, REG)
1057GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG)
1058
1059/*
1060 * fixupReg - Consults an operand specifier to determine which of the
1061 * fixup*Value functions to use in correcting readModRM()'ss interpretation.
1062 *
1063 * @param insn - See fixup*Value().
1064 * @param op - The operand specifier.
1065 * @return - 0 if fixup was successful; -1 if the register returned was
1066 * invalid for its class.
1067 */
1068static int fixupReg(struct InternalInstruction *insn,
1069 struct OperandSpecifier *op) {
1070 uint8_t valid;
1071
Nuno Lopes392bbd92009-12-19 12:07:00 +00001072 dbgprintf(insn, "fixupReg()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001073
1074 switch ((OperandEncoding)op->encoding) {
1075 default:
1076 unreachable("Expected a REG or R/M encoding in fixupReg");
1077 case ENCODING_REG:
1078 insn->reg = (Reg)fixupRegValue(insn,
1079 (OperandType)op->type,
1080 insn->reg - insn->regBase,
1081 &valid);
1082 if (!valid)
1083 return -1;
1084 break;
1085 case ENCODING_RM:
1086 if (insn->eaBase >= insn->eaRegBase) {
1087 insn->eaBase = (EABase)fixupRMValue(insn,
1088 (OperandType)op->type,
1089 insn->eaBase - insn->eaRegBase,
1090 &valid);
1091 if (!valid)
1092 return -1;
1093 }
1094 break;
1095 }
1096
1097 return 0;
1098}
1099
1100/*
1101 * readOpcodeModifier - Reads an operand from the opcode field of an
1102 * instruction. Handles AddRegFrm instructions.
1103 *
1104 * @param insn - The instruction whose opcode field is to be read.
1105 * @param inModRM - Indicates that the opcode field is to be read from the
1106 * ModR/M extension; useful for escape opcodes
1107 */
1108static void readOpcodeModifier(struct InternalInstruction* insn) {
Nuno Lopes392bbd92009-12-19 12:07:00 +00001109 dbgprintf(insn, "readOpcodeModifier()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001110
1111 if (insn->consumedOpcodeModifier)
1112 return;
1113
1114 insn->consumedOpcodeModifier = TRUE;
1115
1116 switch(insn->spec->modifierType) {
1117 default:
1118 unreachable("Unknown modifier type.");
1119 case MODIFIER_NONE:
1120 unreachable("No modifier but an operand expects one.");
1121 case MODIFIER_OPCODE:
1122 insn->opcodeModifier = insn->opcode - insn->spec->modifierBase;
1123 break;
1124 case MODIFIER_MODRM:
1125 insn->opcodeModifier = insn->modRM - insn->spec->modifierBase;
1126 break;
1127 }
1128}
1129
1130/*
1131 * readOpcodeRegister - Reads an operand from the opcode field of an
1132 * instruction and interprets it appropriately given the operand width.
1133 * Handles AddRegFrm instructions.
1134 *
1135 * @param insn - See readOpcodeModifier().
1136 * @param size - The width (in bytes) of the register being specified.
1137 * 1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1138 * RAX.
1139 */
1140static void readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
Nuno Lopes392bbd92009-12-19 12:07:00 +00001141 dbgprintf(insn, "readOpcodeRegister()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001142
1143 readOpcodeModifier(insn);
1144
1145 if (size == 0)
1146 size = insn->registerSize;
1147
1148 switch (size) {
1149 case 1:
1150 insn->opcodeRegister = (Reg)(REG_AL + ((bFromREX(insn->rexPrefix) << 3)
1151 | insn->opcodeModifier));
1152 if(insn->rexPrefix &&
1153 insn->opcodeRegister >= REG_AL + 0x4 &&
1154 insn->opcodeRegister < REG_AL + 0x8) {
1155 insn->opcodeRegister = (Reg)(REG_SPL + (insn->opcodeRegister - REG_AL - 4));
1156 }
1157
1158 break;
1159 case 2:
1160 insn->opcodeRegister = (Reg)(REG_AX + ((bFromREX(insn->rexPrefix) << 3)
1161 | insn->opcodeModifier));
1162 break;
1163 case 4:
1164 insn->opcodeRegister = (Reg)(REG_EAX + ((bFromREX(insn->rexPrefix) << 3)
1165 | insn->opcodeModifier));
1166 break;
1167 case 8:
1168 insn->opcodeRegister = (Reg)(REG_RAX + ((bFromREX(insn->rexPrefix) << 3)
1169 |insn->opcodeModifier));
1170 break;
1171 }
1172}
1173
1174/*
1175 * readImmediate - Consumes an immediate operand from an instruction, given the
1176 * desired operand size.
1177 *
1178 * @param insn - The instruction whose operand is to be read.
1179 * @param size - The width (in bytes) of the operand.
1180 * @return - 0 if the immediate was successfully consumed; nonzero
1181 * otherwise.
1182 */
1183static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1184 uint8_t imm8;
1185 uint16_t imm16;
1186 uint32_t imm32;
1187 uint64_t imm64;
1188
Nuno Lopes392bbd92009-12-19 12:07:00 +00001189 dbgprintf(insn, "readImmediate()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001190
1191 if (insn->numImmediatesConsumed == 2)
1192 unreachable("Already consumed two immediates");
1193
1194 if (size == 0)
1195 size = insn->immediateSize;
1196 else
1197 insn->immediateSize = size;
1198
1199 switch (size) {
1200 case 1:
1201 if (consumeByte(insn, &imm8))
1202 return -1;
1203 insn->immediates[insn->numImmediatesConsumed] = imm8;
1204 break;
1205 case 2:
1206 if (consumeUInt16(insn, &imm16))
1207 return -1;
1208 insn->immediates[insn->numImmediatesConsumed] = imm16;
1209 break;
1210 case 4:
1211 if (consumeUInt32(insn, &imm32))
1212 return -1;
1213 insn->immediates[insn->numImmediatesConsumed] = imm32;
1214 break;
1215 case 8:
1216 if (consumeUInt64(insn, &imm64))
1217 return -1;
1218 insn->immediates[insn->numImmediatesConsumed] = imm64;
1219 break;
1220 }
1221
1222 insn->numImmediatesConsumed++;
1223
1224 return 0;
1225}
1226
1227/*
1228 * readOperands - Consults the specifier for an instruction and consumes all
1229 * operands for that instruction, interpreting them as it goes.
1230 *
1231 * @param insn - The instruction whose operands are to be read and interpreted.
1232 * @return - 0 if all operands could be read; nonzero otherwise.
1233 */
1234static int readOperands(struct InternalInstruction* insn) {
1235 int index;
1236
Nuno Lopes392bbd92009-12-19 12:07:00 +00001237 dbgprintf(insn, "readOperands()");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001238
1239 for (index = 0; index < X86_MAX_OPERANDS; ++index) {
1240 switch (insn->spec->operands[index].encoding) {
1241 case ENCODING_NONE:
1242 break;
1243 case ENCODING_REG:
1244 case ENCODING_RM:
1245 if (readModRM(insn))
1246 return -1;
1247 if (fixupReg(insn, &insn->spec->operands[index]))
1248 return -1;
1249 break;
1250 case ENCODING_CB:
1251 case ENCODING_CW:
1252 case ENCODING_CD:
1253 case ENCODING_CP:
1254 case ENCODING_CO:
1255 case ENCODING_CT:
Nuno Lopes392bbd92009-12-19 12:07:00 +00001256 dbgprintf(insn, "We currently don't hande code-offset encodings");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001257 return -1;
1258 case ENCODING_IB:
1259 if (readImmediate(insn, 1))
1260 return -1;
1261 break;
1262 case ENCODING_IW:
1263 if (readImmediate(insn, 2))
1264 return -1;
1265 break;
1266 case ENCODING_ID:
1267 if (readImmediate(insn, 4))
1268 return -1;
1269 break;
1270 case ENCODING_IO:
1271 if (readImmediate(insn, 8))
1272 return -1;
1273 break;
1274 case ENCODING_Iv:
1275 readImmediate(insn, insn->immediateSize);
1276 break;
1277 case ENCODING_Ia:
1278 readImmediate(insn, insn->addressSize);
1279 break;
1280 case ENCODING_RB:
1281 readOpcodeRegister(insn, 1);
1282 break;
1283 case ENCODING_RW:
1284 readOpcodeRegister(insn, 2);
1285 break;
1286 case ENCODING_RD:
1287 readOpcodeRegister(insn, 4);
1288 break;
1289 case ENCODING_RO:
1290 readOpcodeRegister(insn, 8);
1291 break;
1292 case ENCODING_Rv:
1293 readOpcodeRegister(insn, 0);
1294 break;
1295 case ENCODING_I:
1296 readOpcodeModifier(insn);
1297 break;
1298 case ENCODING_DUP:
1299 break;
1300 default:
Nuno Lopes392bbd92009-12-19 12:07:00 +00001301 dbgprintf(insn, "Encountered an operand with an unknown encoding.");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001302 return -1;
1303 }
1304 }
1305
1306 return 0;
1307}
1308
1309/*
1310 * decodeInstruction - Reads and interprets a full instruction provided by the
1311 * user.
1312 *
1313 * @param insn - A pointer to the instruction to be populated. Must be
1314 * pre-allocated.
1315 * @param reader - The function to be used to read the instruction's bytes.
1316 * @param readerArg - A generic argument to be passed to the reader to store
1317 * any internal state.
1318 * @param logger - If non-NULL, the function to be used to write log messages
1319 * and warnings.
1320 * @param loggerArg - A generic argument to be passed to the logger to store
1321 * any internal state.
1322 * @param startLoc - The address (in the reader's address space) of the first
1323 * byte in the instruction.
1324 * @param mode - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1325 * decode the instruction in.
1326 * @return - 0 if the instruction's memory could be read; nonzero if
1327 * not.
1328 */
1329int decodeInstruction(struct InternalInstruction* insn,
1330 byteReader_t reader,
1331 void* readerArg,
1332 dlog_t logger,
1333 void* loggerArg,
1334 uint64_t startLoc,
1335 DisassemblerMode mode) {
Daniel Dunbar71f842d2009-12-19 03:31:50 +00001336 memset(insn, 0, sizeof(struct InternalInstruction));
Sean Callanan8ed9f512009-12-19 02:59:52 +00001337
1338 insn->reader = reader;
1339 insn->readerArg = readerArg;
1340 insn->dlog = logger;
1341 insn->dlogArg = loggerArg;
1342 insn->startLocation = startLoc;
1343 insn->readerCursor = startLoc;
1344 insn->mode = mode;
1345 insn->numImmediatesConsumed = 0;
1346
1347 if (readPrefixes(insn) ||
1348 readOpcode(insn) ||
1349 getID(insn) ||
1350 insn->instructionID == 0 ||
1351 readOperands(insn))
1352 return -1;
1353
1354 insn->length = insn->readerCursor - insn->startLocation;
1355
Nuno Lopes392bbd92009-12-19 12:07:00 +00001356 dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %llu",
Sean Callanan8ed9f512009-12-19 02:59:52 +00001357 startLoc, insn->readerCursor, insn->length);
1358
1359 if (insn->length > 15)
Nuno Lopes392bbd92009-12-19 12:07:00 +00001360 dbgprintf(insn, "Instruction exceeds 15-byte limit");
Sean Callanan8ed9f512009-12-19 02:59:52 +00001361
1362 return 0;
1363}
Daniel Dunbarc8143462009-12-19 17:11:53 +00001364
1365#endif
1366
1367int X86DissemblerDecoder_dummy = 0;