Eliminated the classification of control registers into %ecr_
[oota-llvm.git] / lib / Target / X86 / Disassembler / X86DisassemblerDecoder.c
1 /*===- 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
16 #include <stdarg.h>   /* for va_*()       */
17 #include <stdio.h>    /* for vsnprintf()  */
18 #include <stdlib.h>   /* for exit()       */
19 #include <string.h>   /* for memset()     */
20
21 #include "X86DisassemblerDecoder.h"
22
23 #include "X86GenDisassemblerTables.inc"
24
25 #define TRUE  1
26 #define FALSE 0
27
28 typedef int8_t bool;
29
30 #ifdef __GNUC__
31 #define NORETURN __attribute__((noreturn))
32 #else
33 #define NORETURN
34 #endif
35
36 #ifndef NDEBUG
37 #define debug(s) do { x86DisassemblerDebug(__FILE__, __LINE__, s); } while (0)
38 #else
39 #define debug(s) do { } while (0)
40 #endif
41
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  */
51 static 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  */
66 static int modRMRequired(OpcodeType type,
67                                 InstructionContext insnContext,
68                                 uint8_t opcode) {
69   const struct ContextDecision* decision = 0;
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   return 0;
90 }
91
92 /*
93  * decode - Reads the appropriate instruction table to obtain the unique ID of
94  *   an instruction.
95  *
96  * @param type        - See modRMRequired().
97  * @param insnContext - See modRMRequired().
98  * @param opcode      - See modRMRequired().
99  * @param modRM       - The ModR/M byte if required, or any value if not.
100  * @return            - The UID of the instruction, or 0 on failure.
101  */
102 static 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     debug("Unknown opcode type");
111     return 0;
112   case ONEBYTE:
113     dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
114     break;
115   case TWOBYTE:
116     dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
117     break;
118   case THREEBYTE_38:
119     dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
120     break;
121   case THREEBYTE_3A:
122     dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
123     break;
124   }
125   
126   switch (dec->modrm_type) {
127   default:
128     debug("Corrupt table!  Unknown modrm_type");
129     return 0;
130   case MODRM_ONEENTRY:
131     return dec->instructionIDs[0];
132   case MODRM_SPLITRM:
133     if (modFromModRM(modRM) == 0x3)
134       return dec->instructionIDs[1];
135     else
136       return dec->instructionIDs[0];
137   case MODRM_FULL:
138     return dec->instructionIDs[modRM];
139   }
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  */
150 static 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  */
164 static 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  */
180 static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
181   return insn->reader(insn->readerArg, byte, insn->readerCursor);
182 }
183
184 static void unconsumeByte(struct InternalInstruction* insn) {
185   insn->readerCursor--;
186 }
187
188 #define CONSUME_FUNC(name, type)                                  \
189   static 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  */
216 CONSUME_FUNC(consumeInt8, int8_t)
217 CONSUME_FUNC(consumeInt16, int16_t)
218 CONSUME_FUNC(consumeInt32, int32_t)
219 CONSUME_FUNC(consumeUInt16, uint16_t)
220 CONSUME_FUNC(consumeUInt32, uint32_t)
221 CONSUME_FUNC(consumeUInt64, uint64_t)
222
223 /*
224  * dbgprintf - Uses the logging function provided by the user to log a single
225  *   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  */
231 static void dbgprintf(struct InternalInstruction* insn,
232                       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  */
258 static 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  */
275 static 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  */
295 static 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   
304   dbgprintf(insn, "readPrefixes()");
305     
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])
317         dbgprintf(insn, "Redundant Group 1 prefix");
318       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         debug("Unhandled override");
348         return -1;
349       }
350       if (prefixGroups[1])
351         dbgprintf(insn, "Redundant Group 2 prefix");
352       prefixGroups[1] = TRUE;
353       setPrefixPresent(insn, byte, prefixLocation);
354       break;
355     case 0x66:  /* Operand-size override */
356       if (prefixGroups[2])
357         dbgprintf(insn, "Redundant Group 3 prefix");
358       prefixGroups[2] = TRUE;
359       hasOpSize = TRUE;
360       setPrefixPresent(insn, byte, prefixLocation);
361       break;
362     case 0x67:  /* Address-size override */
363       if (prefixGroups[3])
364         dbgprintf(insn, "Redundant Group 4 prefix");
365       prefixGroups[3] = TRUE;
366       hasAdSize = TRUE;
367       setPrefixPresent(insn, byte, prefixLocation);
368       break;
369     default:    /* Not a prefix byte */
370       isPrefix = FALSE;
371       break;
372     }
373     
374     if (isPrefix)
375       dbgprintf(insn, "Found prefix 0x%hhx", byte);
376   }
377   
378   if (insn->mode == MODE_64BIT) {
379     if ((byte & 0xf0) == 0x40) {
380       uint8_t opcodeByte;
381       
382       if (lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) {
383         dbgprintf(insn, "Redundant REX prefix");
384         return -1;
385       }
386       
387       insn->rexPrefix = byte;
388       insn->necessaryPrefixLocation = insn->readerCursor - 2;
389       
390       dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
391     } else {                
392       unconsumeByte(insn);
393       insn->necessaryPrefixLocation = insn->readerCursor - 1;
394     }
395   } else {
396     unconsumeByte(insn);
397   }
398   
399   if (insn->mode == MODE_16BIT) {
400     insn->registerSize       = (hasOpSize ? 4 : 2);
401     insn->addressSize        = (hasAdSize ? 4 : 2);
402     insn->displacementSize   = (hasAdSize ? 4 : 2);
403     insn->immediateSize      = (hasOpSize ? 4 : 2);
404   } else if (insn->mode == MODE_32BIT) {
405     insn->registerSize       = (hasOpSize ? 2 : 4);
406     insn->addressSize        = (hasAdSize ? 2 : 4);
407     insn->displacementSize   = (hasAdSize ? 2 : 4);
408     insn->immediateSize      = (hasAdSize ? 2 : 4);
409   } else if (insn->mode == MODE_64BIT) {
410     if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
411       insn->registerSize       = 8;
412       insn->addressSize        = (hasAdSize ? 4 : 8);
413       insn->displacementSize   = 4;
414       insn->immediateSize      = 4;
415     } else if (insn->rexPrefix) {
416       insn->registerSize       = (hasOpSize ? 2 : 4);
417       insn->addressSize        = (hasAdSize ? 4 : 8);
418       insn->displacementSize   = (hasOpSize ? 2 : 4);
419       insn->immediateSize      = (hasOpSize ? 2 : 4);
420     } else {
421       insn->registerSize       = (hasOpSize ? 2 : 4);
422       insn->addressSize        = (hasAdSize ? 4 : 8);
423       insn->displacementSize   = (hasOpSize ? 2 : 4);
424       insn->immediateSize      = (hasOpSize ? 2 : 4);
425     }
426   }
427   
428   return 0;
429 }
430
431 /*
432  * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
433  *   extended or escape opcodes).
434  *
435  * @param insn  - The instruction whose opcode is to be read.
436  * @return      - 0 if the opcode could be read successfully; nonzero otherwise.
437  */
438 static int readOpcode(struct InternalInstruction* insn) {  
439   /* Determine the length of the primary opcode */
440   
441   uint8_t current;
442   
443   dbgprintf(insn, "readOpcode()");
444   
445   insn->opcodeType = ONEBYTE;
446   if (consumeByte(insn, &current))
447     return -1;
448   
449   if (current == 0x0f) {
450     dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
451     
452     insn->twoByteEscape = current;
453     
454     if (consumeByte(insn, &current))
455       return -1;
456     
457     if (current == 0x38) {
458       dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
459       
460       insn->threeByteEscape = current;
461       
462       if (consumeByte(insn, &current))
463         return -1;
464       
465       insn->opcodeType = THREEBYTE_38;
466     } else if (current == 0x3a) {
467       dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
468       
469       insn->threeByteEscape = current;
470       
471       if (consumeByte(insn, &current))
472         return -1;
473       
474       insn->opcodeType = THREEBYTE_3A;
475     } else {
476       dbgprintf(insn, "Didn't find a three-byte escape prefix");
477       
478       insn->opcodeType = TWOBYTE;
479     }
480   }
481   
482   /*
483    * At this point we have consumed the full opcode.
484    * Anything we consume from here on must be unconsumed.
485    */
486   
487   insn->opcode = current;
488   
489   return 0;
490 }
491
492 static int readModRM(struct InternalInstruction* insn);
493
494 /*
495  * getIDWithAttrMask - Determines the ID of an instruction, consuming
496  *   the ModR/M byte as appropriate for extended and escape opcodes,
497  *   and using a supplied attribute mask.
498  *
499  * @param instructionID - A pointer whose target is filled in with the ID of the
500  *                        instruction.
501  * @param insn          - The instruction whose ID is to be determined.
502  * @param attrMask      - The attribute mask to search.
503  * @return              - 0 if the ModR/M could be read when needed or was not
504  *                        needed; nonzero otherwise.
505  */
506 static int getIDWithAttrMask(uint16_t* instructionID,
507                              struct InternalInstruction* insn,
508                              uint8_t attrMask) {
509   BOOL hasModRMExtension;
510   
511   uint8_t instructionClass;
512
513   instructionClass = contextForAttrs(attrMask);
514   
515   hasModRMExtension = modRMRequired(insn->opcodeType,
516                                     instructionClass,
517                                     insn->opcode);
518   
519   if (hasModRMExtension) {
520     readModRM(insn);
521     
522     *instructionID = decode(insn->opcodeType,
523                             instructionClass,
524                             insn->opcode,
525                             insn->modRM);
526   } else {
527     *instructionID = decode(insn->opcodeType,
528                             instructionClass,
529                             insn->opcode,
530                             0);
531   }
532       
533   return 0;
534 }
535
536 /*
537  * is16BitEquivalent - Determines whether two instruction names refer to
538  * equivalent instructions but one is 16-bit whereas the other is not.
539  *
540  * @param orig  - The instruction that is not 16-bit
541  * @param equiv - The instruction that is 16-bit
542  */
543 static BOOL is16BitEquvalent(const char* orig, const char* equiv) {
544   off_t i;
545   
546   for (i = 0;; i++) {
547     if (orig[i] == '\0' && equiv[i] == '\0')
548       return TRUE;
549     if (orig[i] == '\0' || equiv[i] == '\0')
550       return FALSE;
551     if (orig[i] != equiv[i]) {
552       if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
553         continue;
554       if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
555         continue;
556       if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
557         continue;
558       return FALSE;
559     }
560   }
561 }
562
563 /*
564  * is64BitEquivalent - Determines whether two instruction names refer to
565  * equivalent instructions but one is 64-bit whereas the other is not.
566  *
567  * @param orig  - The instruction that is not 64-bit
568  * @param equiv - The instruction that is 64-bit
569  */
570 static BOOL is64BitEquivalent(const char* orig, const char* equiv) {
571   off_t i;
572   
573   for (i = 0;; i++) {
574     if (orig[i] == '\0' && equiv[i] == '\0')
575       return TRUE;
576     if (orig[i] == '\0' || equiv[i] == '\0')
577       return FALSE;
578     if (orig[i] != equiv[i]) {
579       if ((orig[i] == 'W' || orig[i] == 'L') && equiv[i] == 'Q')
580         continue;
581       if ((orig[i] == '1' || orig[i] == '3') && equiv[i] == '6')
582         continue;
583       if ((orig[i] == '6' || orig[i] == '2') && equiv[i] == '4')
584         continue;
585       return FALSE;
586     }
587   }
588 }
589
590
591 /*
592  * getID - Determines the ID of an instruction, consuming the ModR/M byte as 
593  *   appropriate for extended and escape opcodes.  Determines the attributes and 
594  *   context for the instruction before doing so.
595  *
596  * @param insn  - The instruction whose ID is to be determined.
597  * @return      - 0 if the ModR/M could be read when needed or was not needed;
598  *                nonzero otherwise.
599  */
600 static int getID(struct InternalInstruction* insn) {  
601   uint8_t attrMask;
602   uint16_t instructionID;
603   
604   dbgprintf(insn, "getID()");
605     
606   attrMask = ATTR_NONE;
607   
608   if (insn->mode == MODE_64BIT)
609     attrMask |= ATTR_64BIT;
610   
611   if (insn->rexPrefix & 0x08)
612     attrMask |= ATTR_REXW;
613   
614   if (isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
615     attrMask |= ATTR_OPSIZE;
616   else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
617     attrMask |= ATTR_XS;
618   else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
619     attrMask |= ATTR_XD;
620   
621   if (getIDWithAttrMask(&instructionID, insn, attrMask))
622     return -1;
623   
624   /* The following clauses compensate for limitations of the tables. */
625   
626   if ((attrMask & ATTR_XD) && (attrMask & ATTR_REXW)) {
627     /*
628      * Although for SSE instructions it is usually necessary to treat REX.W+F2
629      * as F2 for decode (in the absence of a 64BIT_REXW_XD category) there is
630      * an occasional instruction where F2 is incidental and REX.W is the more
631      * significant.  If the decoded instruction is 32-bit and adding REX.W
632      * instead of F2 changes a 32 to a 64, we adopt the new encoding.
633      */
634     
635     struct InstructionSpecifier* spec;
636     uint16_t instructionIDWithREXw;
637     struct InstructionSpecifier* specWithREXw;
638     
639     spec = specifierForUID(instructionID);
640     
641     if (getIDWithAttrMask(&instructionIDWithREXw,
642                           insn,
643                           attrMask & (~ATTR_XD))) {
644       /*
645        * Decoding with REX.w would yield nothing; give up and return original
646        * decode.
647        */
648       
649       insn->instructionID = instructionID;
650       insn->spec = spec;
651       return 0;
652     }
653     
654     specWithREXw = specifierForUID(instructionIDWithREXw);
655     
656     if (is64BitEquivalent(spec->name, specWithREXw->name)) {
657       insn->instructionID = instructionIDWithREXw;
658       insn->spec = specWithREXw;
659     } else {
660       insn->instructionID = instructionID;
661       insn->spec = spec;
662     }
663     return 0;
664   }
665   
666   if (insn->prefixPresent[0x66] && !(attrMask & ATTR_OPSIZE)) {
667     /*
668      * The instruction tables make no distinction between instructions that
669      * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
670      * particular spot (i.e., many MMX operations).  In general we're
671      * conservative, but in the specific case where OpSize is present but not
672      * in the right place we check if there's a 16-bit operation.
673      */
674     
675     struct InstructionSpecifier* spec;
676     uint16_t instructionIDWithOpsize;
677     struct InstructionSpecifier* specWithOpsize;
678     
679     spec = specifierForUID(instructionID);
680     
681     if (getIDWithAttrMask(&instructionIDWithOpsize,
682                           insn,
683                           attrMask | ATTR_OPSIZE)) {
684       /* 
685        * ModRM required with OpSize but not present; give up and return version
686        * without OpSize set
687        */
688       
689       insn->instructionID = instructionID;
690       insn->spec = spec;
691       return 0;
692     }
693     
694     specWithOpsize = specifierForUID(instructionIDWithOpsize);
695     
696     if (is16BitEquvalent(spec->name, specWithOpsize->name)) {
697       insn->instructionID = instructionIDWithOpsize;
698       insn->spec = specWithOpsize;
699     } else {
700       insn->instructionID = instructionID;
701       insn->spec = spec;
702     }
703     return 0;
704   }
705   
706   insn->instructionID = instructionID;
707   insn->spec = specifierForUID(insn->instructionID);
708   
709   return 0;
710 }
711
712 /*
713  * readSIB - Consumes the SIB byte to determine addressing information for an
714  *   instruction.
715  *
716  * @param insn  - The instruction whose SIB byte is to be read.
717  * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
718  */
719 static int readSIB(struct InternalInstruction* insn) {
720   SIBIndex sibIndexBase = 0;
721   SIBBase sibBaseBase = 0;
722   uint8_t index, base;
723   
724   dbgprintf(insn, "readSIB()");
725   
726   if (insn->consumedSIB)
727     return 0;
728   
729   insn->consumedSIB = TRUE;
730   
731   switch (insn->addressSize) {
732   case 2:
733     dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
734     return -1;
735     break;
736   case 4:
737     sibIndexBase = SIB_INDEX_EAX;
738     sibBaseBase = SIB_BASE_EAX;
739     break;
740   case 8:
741     sibIndexBase = SIB_INDEX_RAX;
742     sibBaseBase = SIB_BASE_RAX;
743     break;
744   }
745
746   if (consumeByte(insn, &insn->sib))
747     return -1;
748   
749   index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
750   
751   switch (index) {
752   case 0x4:
753     insn->sibIndex = SIB_INDEX_NONE;
754     break;
755   default:
756     insn->sibIndex = (EABase)(sibIndexBase + index);
757     if (insn->sibIndex == SIB_INDEX_sib ||
758         insn->sibIndex == SIB_INDEX_sib64)
759       insn->sibIndex = SIB_INDEX_NONE;
760     break;
761   }
762   
763   switch (scaleFromSIB(insn->sib)) {
764   case 0:
765     insn->sibScale = 1;
766     break;
767   case 1:
768     insn->sibScale = 2;
769     break;
770   case 2:
771     insn->sibScale = 4;
772     break;
773   case 3:
774     insn->sibScale = 8;
775     break;
776   }
777   
778   base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
779   
780   switch (base) {
781   case 0x5:
782     switch (modFromModRM(insn->modRM)) {
783     case 0x0:
784       insn->eaDisplacement = EA_DISP_32;
785       insn->sibBase = SIB_BASE_NONE;
786       break;
787     case 0x1:
788       insn->eaDisplacement = EA_DISP_8;
789       insn->sibBase = (insn->addressSize == 4 ? 
790                        SIB_BASE_EBP : SIB_BASE_RBP);
791       break;
792     case 0x2:
793       insn->eaDisplacement = EA_DISP_32;
794       insn->sibBase = (insn->addressSize == 4 ? 
795                        SIB_BASE_EBP : SIB_BASE_RBP);
796       break;
797     case 0x3:
798       debug("Cannot have Mod = 0b11 and a SIB byte");
799       return -1;
800     }
801     break;
802   default:
803     insn->sibBase = (EABase)(sibBaseBase + base);
804     break;
805   }
806   
807   return 0;
808 }
809
810 /*
811  * readDisplacement - Consumes the displacement of an instruction.
812  *
813  * @param insn  - The instruction whose displacement is to be read.
814  * @return      - 0 if the displacement byte was successfully read; nonzero 
815  *                otherwise.
816  */
817 static int readDisplacement(struct InternalInstruction* insn) {  
818   int8_t d8;
819   int16_t d16;
820   int32_t d32;
821   
822   dbgprintf(insn, "readDisplacement()");
823   
824   if (insn->consumedDisplacement)
825     return 0;
826   
827   insn->consumedDisplacement = TRUE;
828   
829   switch (insn->eaDisplacement) {
830   case EA_DISP_NONE:
831     insn->consumedDisplacement = FALSE;
832     break;
833   case EA_DISP_8:
834     if (consumeInt8(insn, &d8))
835       return -1;
836     insn->displacement = d8;
837     break;
838   case EA_DISP_16:
839     if (consumeInt16(insn, &d16))
840       return -1;
841     insn->displacement = d16;
842     break;
843   case EA_DISP_32:
844     if (consumeInt32(insn, &d32))
845       return -1;
846     insn->displacement = d32;
847     break;
848   }
849   
850   insn->consumedDisplacement = TRUE;
851   return 0;
852 }
853
854 /*
855  * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
856  *   displacement) for an instruction and interprets it.
857  *
858  * @param insn  - The instruction whose addressing information is to be read.
859  * @return      - 0 if the information was successfully read; nonzero otherwise.
860  */
861 static int readModRM(struct InternalInstruction* insn) {  
862   uint8_t mod, rm, reg;
863   
864   dbgprintf(insn, "readModRM()");
865   
866   if (insn->consumedModRM)
867     return 0;
868   
869   consumeByte(insn, &insn->modRM);
870   insn->consumedModRM = TRUE;
871   
872   mod     = modFromModRM(insn->modRM);
873   rm      = rmFromModRM(insn->modRM);
874   reg     = regFromModRM(insn->modRM);
875   
876   /*
877    * This goes by insn->registerSize to pick the correct register, which messes
878    * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
879    * fixupReg().
880    */
881   switch (insn->registerSize) {
882   case 2:
883     insn->regBase = MODRM_REG_AX;
884     insn->eaRegBase = EA_REG_AX;
885     break;
886   case 4:
887     insn->regBase = MODRM_REG_EAX;
888     insn->eaRegBase = EA_REG_EAX;
889     break;
890   case 8:
891     insn->regBase = MODRM_REG_RAX;
892     insn->eaRegBase = EA_REG_RAX;
893     break;
894   }
895   
896   reg |= rFromREX(insn->rexPrefix) << 3;
897   rm  |= bFromREX(insn->rexPrefix) << 3;
898   
899   insn->reg = (Reg)(insn->regBase + reg);
900   
901   switch (insn->addressSize) {
902   case 2:
903     insn->eaBaseBase = EA_BASE_BX_SI;
904      
905     switch (mod) {
906     case 0x0:
907       if (rm == 0x6) {
908         insn->eaBase = EA_BASE_NONE;
909         insn->eaDisplacement = EA_DISP_16;
910         if (readDisplacement(insn))
911           return -1;
912       } else {
913         insn->eaBase = (EABase)(insn->eaBaseBase + rm);
914         insn->eaDisplacement = EA_DISP_NONE;
915       }
916       break;
917     case 0x1:
918       insn->eaBase = (EABase)(insn->eaBaseBase + rm);
919       insn->eaDisplacement = EA_DISP_8;
920       if (readDisplacement(insn))
921         return -1;
922       break;
923     case 0x2:
924       insn->eaBase = (EABase)(insn->eaBaseBase + rm);
925       insn->eaDisplacement = EA_DISP_16;
926       if (readDisplacement(insn))
927         return -1;
928       break;
929     case 0x3:
930       insn->eaBase = (EABase)(insn->eaRegBase + rm);
931       if (readDisplacement(insn))
932         return -1;
933       break;
934     }
935     break;
936   case 4:
937   case 8:
938     insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
939     
940     switch (mod) {
941     case 0x0:
942       insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
943       switch (rm) {
944       case 0x4:
945       case 0xc:   /* in case REXW.b is set */
946         insn->eaBase = (insn->addressSize == 4 ? 
947                         EA_BASE_sib : EA_BASE_sib64);
948         readSIB(insn);
949         if (readDisplacement(insn))
950           return -1;
951         break;
952       case 0x5:
953         insn->eaBase = EA_BASE_NONE;
954         insn->eaDisplacement = EA_DISP_32;
955         if (readDisplacement(insn))
956           return -1;
957         break;
958       default:
959         insn->eaBase = (EABase)(insn->eaBaseBase + rm);
960         break;
961       }
962       break;
963     case 0x1:
964     case 0x2:
965       insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
966       switch (rm) {
967       case 0x4:
968       case 0xc:   /* in case REXW.b is set */
969         insn->eaBase = EA_BASE_sib;
970         readSIB(insn);
971         if (readDisplacement(insn))
972           return -1;
973         break;
974       default:
975         insn->eaBase = (EABase)(insn->eaBaseBase + rm);
976         if (readDisplacement(insn))
977           return -1;
978         break;
979       }
980       break;
981     case 0x3:
982       insn->eaDisplacement = EA_DISP_NONE;
983       insn->eaBase = (EABase)(insn->eaRegBase + rm);
984       break;
985     }
986     break;
987   } /* switch (insn->addressSize) */
988   
989   return 0;
990 }
991
992 #define GENERIC_FIXUP_FUNC(name, base, prefix)            \
993   static uint8_t name(struct InternalInstruction *insn,   \
994                       OperandType type,                   \
995                       uint8_t index,                      \
996                       uint8_t *valid) {                   \
997     *valid = 1;                                           \
998     switch (type) {                                       \
999     default:                                              \
1000       debug("Unhandled register type");                   \
1001       *valid = 0;                                         \
1002       return 0;                                           \
1003     case TYPE_Rv:                                         \
1004       return base + index;                                \
1005     case TYPE_R8:                                         \
1006       if (insn->rexPrefix &&                              \
1007          index >= 4 && index <= 7) {                      \
1008         return prefix##_SPL + (index - 4);                \
1009       } else {                                            \
1010         return prefix##_AL + index;                       \
1011       }                                                   \
1012     case TYPE_R16:                                        \
1013       return prefix##_AX + index;                         \
1014     case TYPE_R32:                                        \
1015       return prefix##_EAX + index;                        \
1016     case TYPE_R64:                                        \
1017       return prefix##_RAX + index;                        \
1018     case TYPE_XMM128:                                     \
1019     case TYPE_XMM64:                                      \
1020     case TYPE_XMM32:                                      \
1021     case TYPE_XMM:                                        \
1022       return prefix##_XMM0 + index;                       \
1023     case TYPE_MM64:                                       \
1024     case TYPE_MM32:                                       \
1025     case TYPE_MM:                                         \
1026       if (index > 7)                                      \
1027         *valid = 0;                                       \
1028       return prefix##_MM0 + index;                        \
1029     case TYPE_SEGMENTREG:                                 \
1030       if (index > 5)                                      \
1031         *valid = 0;                                       \
1032       return prefix##_ES + index;                         \
1033     case TYPE_DEBUGREG:                                   \
1034       if (index > 7)                                      \
1035         *valid = 0;                                       \
1036       return prefix##_DR0 + index;                        \
1037     case TYPE_CONTROLREG:                                 \
1038       if (index > 8)                                      \
1039         *valid = 0;                                       \
1040       return prefix##_CR0 + 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  * @return      - The proper value.
1056  */
1057 GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase,    MODRM_REG)
1058 GENERIC_FIXUP_FUNC(fixupRMValue,  insn->eaRegBase,  EA_REG)
1059
1060 /*
1061  * fixupReg - Consults an operand specifier to determine which of the
1062  *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1063  *
1064  * @param insn  - See fixup*Value().
1065  * @param op    - The operand specifier.
1066  * @return      - 0 if fixup was successful; -1 if the register returned was
1067  *                invalid for its class.
1068  */
1069 static int fixupReg(struct InternalInstruction *insn, 
1070                     struct OperandSpecifier *op) {
1071   uint8_t valid;
1072   
1073   dbgprintf(insn, "fixupReg()");
1074   
1075   switch ((OperandEncoding)op->encoding) {
1076   default:
1077     debug("Expected a REG or R/M encoding in fixupReg");
1078     return -1;
1079   case ENCODING_REG:
1080     insn->reg = (Reg)fixupRegValue(insn,
1081                                    (OperandType)op->type,
1082                                    insn->reg - insn->regBase,
1083                                    &valid);
1084     if (!valid)
1085       return -1;
1086     break;
1087   case ENCODING_RM:
1088     if (insn->eaBase >= insn->eaRegBase) {
1089       insn->eaBase = (EABase)fixupRMValue(insn,
1090                                           (OperandType)op->type,
1091                                           insn->eaBase - insn->eaRegBase,
1092                                           &valid);
1093       if (!valid)
1094         return -1;
1095     }
1096     break;
1097   }
1098   
1099   return 0;
1100 }
1101
1102 /*
1103  * readOpcodeModifier - Reads an operand from the opcode field of an 
1104  *   instruction.  Handles AddRegFrm instructions.
1105  *
1106  * @param insn    - The instruction whose opcode field is to be read.
1107  * @param inModRM - Indicates that the opcode field is to be read from the
1108  *                  ModR/M extension; useful for escape opcodes
1109  * @return        - 0 on success; nonzero otherwise.
1110  */
1111 static int readOpcodeModifier(struct InternalInstruction* insn) {
1112   dbgprintf(insn, "readOpcodeModifier()");
1113   
1114   if (insn->consumedOpcodeModifier)
1115     return 0;
1116   
1117   insn->consumedOpcodeModifier = TRUE;
1118   
1119   switch (insn->spec->modifierType) {
1120   default:
1121     debug("Unknown modifier type.");
1122     return -1;
1123   case MODIFIER_NONE:
1124     debug("No modifier but an operand expects one.");
1125     return -1;
1126   case MODIFIER_OPCODE:
1127     insn->opcodeModifier = insn->opcode - insn->spec->modifierBase;
1128     return 0;
1129   case MODIFIER_MODRM:
1130     insn->opcodeModifier = insn->modRM - insn->spec->modifierBase;
1131     return 0;
1132   }  
1133 }
1134
1135 /*
1136  * readOpcodeRegister - Reads an operand from the opcode field of an 
1137  *   instruction and interprets it appropriately given the operand width.
1138  *   Handles AddRegFrm instructions.
1139  *
1140  * @param insn  - See readOpcodeModifier().
1141  * @param size  - The width (in bytes) of the register being specified.
1142  *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1143  *                RAX.
1144  * @return      - 0 on success; nonzero otherwise.
1145  */
1146 static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
1147   dbgprintf(insn, "readOpcodeRegister()");
1148
1149   if (readOpcodeModifier(insn))
1150     return -1;
1151   
1152   if (size == 0)
1153     size = insn->registerSize;
1154   
1155   switch (size) {
1156   case 1:
1157     insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) 
1158                                                   | insn->opcodeModifier));
1159     if (insn->rexPrefix && 
1160         insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1161         insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1162       insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1163                                    + (insn->opcodeRegister - MODRM_REG_AL - 4));
1164     }
1165       
1166     break;
1167   case 2:
1168     insn->opcodeRegister = (Reg)(MODRM_REG_AX
1169                                  + ((bFromREX(insn->rexPrefix) << 3) 
1170                                     | insn->opcodeModifier));
1171     break;
1172   case 4:
1173     insn->opcodeRegister = (Reg)(MODRM_REG_EAX
1174                                  + ((bFromREX(insn->rexPrefix) << 3) 
1175                                     | insn->opcodeModifier));
1176     break;
1177   case 8:
1178     insn->opcodeRegister = (Reg)(MODRM_REG_RAX 
1179                                  + ((bFromREX(insn->rexPrefix) << 3) 
1180                                     | insn->opcodeModifier));
1181     break;
1182   }
1183   
1184   return 0;
1185 }
1186
1187 /*
1188  * readImmediate - Consumes an immediate operand from an instruction, given the
1189  *   desired operand size.
1190  *
1191  * @param insn  - The instruction whose operand is to be read.
1192  * @param size  - The width (in bytes) of the operand.
1193  * @return      - 0 if the immediate was successfully consumed; nonzero
1194  *                otherwise.
1195  */
1196 static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1197   uint8_t imm8;
1198   uint16_t imm16;
1199   uint32_t imm32;
1200   uint64_t imm64;
1201   
1202   dbgprintf(insn, "readImmediate()");
1203   
1204   if (insn->numImmediatesConsumed == 2) {
1205     debug("Already consumed two immediates");
1206     return -1;
1207   }
1208   
1209   if (size == 0)
1210     size = insn->immediateSize;
1211   else
1212     insn->immediateSize = size;
1213   
1214   switch (size) {
1215   case 1:
1216     if (consumeByte(insn, &imm8))
1217       return -1;
1218     insn->immediates[insn->numImmediatesConsumed] = imm8;
1219     break;
1220   case 2:
1221     if (consumeUInt16(insn, &imm16))
1222       return -1;
1223     insn->immediates[insn->numImmediatesConsumed] = imm16;
1224     break;
1225   case 4:
1226     if (consumeUInt32(insn, &imm32))
1227       return -1;
1228     insn->immediates[insn->numImmediatesConsumed] = imm32;
1229     break;
1230   case 8:
1231     if (consumeUInt64(insn, &imm64))
1232       return -1;
1233     insn->immediates[insn->numImmediatesConsumed] = imm64;
1234     break;
1235   }
1236   
1237   insn->numImmediatesConsumed++;
1238   
1239   return 0;
1240 }
1241
1242 /*
1243  * readOperands - Consults the specifier for an instruction and consumes all
1244  *   operands for that instruction, interpreting them as it goes.
1245  *
1246  * @param insn  - The instruction whose operands are to be read and interpreted.
1247  * @return      - 0 if all operands could be read; nonzero otherwise.
1248  */
1249 static int readOperands(struct InternalInstruction* insn) {
1250   int index;
1251   
1252   dbgprintf(insn, "readOperands()");
1253   
1254   for (index = 0; index < X86_MAX_OPERANDS; ++index) {
1255     switch (insn->spec->operands[index].encoding) {
1256     case ENCODING_NONE:
1257       break;
1258     case ENCODING_REG:
1259     case ENCODING_RM:
1260       if (readModRM(insn))
1261         return -1;
1262       if (fixupReg(insn, &insn->spec->operands[index]))
1263         return -1;
1264       break;
1265     case ENCODING_CB:
1266     case ENCODING_CW:
1267     case ENCODING_CD:
1268     case ENCODING_CP:
1269     case ENCODING_CO:
1270     case ENCODING_CT:
1271       dbgprintf(insn, "We currently don't hande code-offset encodings");
1272       return -1;
1273     case ENCODING_IB:
1274       if (readImmediate(insn, 1))
1275         return -1;
1276       if (insn->spec->operands[index].type == TYPE_IMM3 &&
1277           insn->immediates[insn->numImmediatesConsumed - 1] > 7)
1278         return -1;
1279       break;
1280     case ENCODING_IW:
1281       if (readImmediate(insn, 2))
1282         return -1;
1283       break;
1284     case ENCODING_ID:
1285       if (readImmediate(insn, 4))
1286         return -1;
1287       break;
1288     case ENCODING_IO:
1289       if (readImmediate(insn, 8))
1290         return -1;
1291       break;
1292     case ENCODING_Iv:
1293       if (readImmediate(insn, insn->immediateSize))
1294         return -1;
1295       break;
1296     case ENCODING_Ia:
1297       if (readImmediate(insn, insn->addressSize))
1298         return -1;
1299       break;
1300     case ENCODING_RB:
1301       if (readOpcodeRegister(insn, 1))
1302         return -1;
1303       break;
1304     case ENCODING_RW:
1305       if (readOpcodeRegister(insn, 2))
1306         return -1;
1307       break;
1308     case ENCODING_RD:
1309       if (readOpcodeRegister(insn, 4))
1310         return -1;
1311       break;
1312     case ENCODING_RO:
1313       if (readOpcodeRegister(insn, 8))
1314         return -1;
1315       break;
1316     case ENCODING_Rv:
1317       if (readOpcodeRegister(insn, 0))
1318         return -1;
1319       break;
1320     case ENCODING_I:
1321       if (readOpcodeModifier(insn))
1322         return -1;
1323     case ENCODING_DUP:
1324       break;
1325     default:
1326       dbgprintf(insn, "Encountered an operand with an unknown encoding.");
1327       return -1;
1328     }
1329   }
1330   
1331   return 0;
1332 }
1333
1334 /*
1335  * decodeInstruction - Reads and interprets a full instruction provided by the
1336  *   user.
1337  *
1338  * @param insn      - A pointer to the instruction to be populated.  Must be 
1339  *                    pre-allocated.
1340  * @param reader    - The function to be used to read the instruction's bytes.
1341  * @param readerArg - A generic argument to be passed to the reader to store
1342  *                    any internal state.
1343  * @param logger    - If non-NULL, the function to be used to write log messages
1344  *                    and warnings.
1345  * @param loggerArg - A generic argument to be passed to the logger to store
1346  *                    any internal state.
1347  * @param startLoc  - The address (in the reader's address space) of the first
1348  *                    byte in the instruction.
1349  * @param mode      - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1350  *                    decode the instruction in.
1351  * @return          - 0 if the instruction's memory could be read; nonzero if
1352  *                    not.
1353  */
1354 int decodeInstruction(struct InternalInstruction* insn,
1355                       byteReader_t reader,
1356                       void* readerArg,
1357                       dlog_t logger,
1358                       void* loggerArg,
1359                       uint64_t startLoc,
1360                       DisassemblerMode mode) {
1361   memset(insn, 0, sizeof(struct InternalInstruction));
1362     
1363   insn->reader = reader;
1364   insn->readerArg = readerArg;
1365   insn->dlog = logger;
1366   insn->dlogArg = loggerArg;
1367   insn->startLocation = startLoc;
1368   insn->readerCursor = startLoc;
1369   insn->mode = mode;
1370   insn->numImmediatesConsumed = 0;
1371   
1372   if (readPrefixes(insn)       ||
1373       readOpcode(insn)         ||
1374       getID(insn)              ||
1375       insn->instructionID == 0 ||
1376       readOperands(insn))
1377     return -1;
1378   
1379   insn->length = insn->readerCursor - insn->startLocation;
1380   
1381   dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1382             startLoc, insn->readerCursor, insn->length);
1383     
1384   if (insn->length > 15)
1385     dbgprintf(insn, "Instruction exceeds 15-byte limit");
1386   
1387   return 0;
1388 }