X86 table-generator and disassembler support for the AVX
[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 #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
36
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  */
45 static InstructionContext contextForAttrs(uint8_t attrMask) {
46   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  */
60 static int modRMRequired(OpcodeType type,
61                                 InstructionContext insnContext,
62                                 uint8_t opcode) {
63   const struct ContextDecision* decision = 0;
64   
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;
78   }
79   
80   return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].
81     modrm_type != MODRM_ONEENTRY;
82   
83   return 0;
84 }
85
86 /*
87  * decode - Reads the appropriate instruction table to obtain the unique ID of
88  *   an instruction.
89  *
90  * @param type        - See modRMRequired().
91  * @param insnContext - See modRMRequired().
92  * @param opcode      - See modRMRequired().
93  * @param modRM       - The ModR/M byte if required, or any value if not.
94  * @return            - The UID of the instruction, or 0 on failure.
95  */
96 static InstrUID decode(OpcodeType type,
97                        InstructionContext insnContext,
98                        uint8_t opcode,
99                        uint8_t modRM) {
100   const struct ModRMDecision* dec;
101   
102   switch (type) {
103   default:
104     debug("Unknown opcode type");
105     return 0;
106   case ONEBYTE:
107     dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
108     break;
109   case TWOBYTE:
110     dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
111     break;
112   case THREEBYTE_38:
113     dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
114     break;
115   case THREEBYTE_3A:
116     dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
117     break;
118   }
119   
120   switch (dec->modrm_type) {
121   default:
122     debug("Corrupt table!  Unknown modrm_type");
123     return 0;
124   case MODRM_ONEENTRY:
125     return dec->instructionIDs[0];
126   case MODRM_SPLITRM:
127     if (modFromModRM(modRM) == 0x3)
128       return dec->instructionIDs[1];
129     else
130       return dec->instructionIDs[0];
131   case MODRM_FULL:
132     return dec->instructionIDs[modRM];
133   }
134 }
135
136 /*
137  * specifierForUID - Given a UID, returns the name and operand specification for
138  *   that instruction.
139  *
140  * @param uid - The unique ID for the instruction.  This should be returned by
141  *              decode(); specifierForUID will not check bounds.
142  * @return    - A pointer to the specification for that instruction.
143  */
144 static const struct InstructionSpecifier *specifierForUID(InstrUID uid) {
145   return &INSTRUCTIONS_SYM[uid];
146 }
147
148 /*
149  * consumeByte - Uses the reader function provided by the user to consume one
150  *   byte from the instruction's memory and advance the cursor.
151  *
152  * @param insn  - The instruction with the reader function to use.  The cursor
153  *                for this instruction is advanced.
154  * @param byte  - A pointer to a pre-allocated memory buffer to be populated
155  *                with the data read.
156  * @return      - 0 if the read was successful; nonzero otherwise.
157  */
158 static int consumeByte(struct InternalInstruction* insn, uint8_t* byte) {
159   int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
160   
161   if (!ret)
162     ++(insn->readerCursor);
163   
164   return ret;
165 }
166
167 /*
168  * lookAtByte - Like consumeByte, but does not advance the cursor.
169  *
170  * @param insn  - See consumeByte().
171  * @param byte  - See consumeByte().
172  * @return      - See consumeByte().
173  */
174 static int lookAtByte(struct InternalInstruction* insn, uint8_t* byte) {
175   return insn->reader(insn->readerArg, byte, insn->readerCursor);
176 }
177
178 static void unconsumeByte(struct InternalInstruction* insn) {
179   insn->readerCursor--;
180 }
181
182 #define CONSUME_FUNC(name, type)                                  \
183   static int name(struct InternalInstruction* insn, type* ptr) {  \
184     type combined = 0;                                            \
185     unsigned offset;                                              \
186     for (offset = 0; offset < sizeof(type); ++offset) {           \
187       uint8_t byte;                                               \
188       int ret = insn->reader(insn->readerArg,                     \
189                              &byte,                               \
190                              insn->readerCursor + offset);        \
191       if (ret)                                                    \
192         return ret;                                               \
193       combined = combined | ((type)byte << ((type)offset * 8));   \
194     }                                                             \
195     *ptr = combined;                                              \
196     insn->readerCursor += sizeof(type);                           \
197     return 0;                                                     \
198   }
199
200 /*
201  * consume* - Use the reader function provided by the user to consume data
202  *   values of various sizes from the instruction's memory and advance the
203  *   cursor appropriately.  These readers perform endian conversion.
204  *
205  * @param insn    - See consumeByte().
206  * @param ptr     - A pointer to a pre-allocated memory of appropriate size to
207  *                  be populated with the data read.
208  * @return        - See consumeByte().
209  */
210 CONSUME_FUNC(consumeInt8, int8_t)
211 CONSUME_FUNC(consumeInt16, int16_t)
212 CONSUME_FUNC(consumeInt32, int32_t)
213 CONSUME_FUNC(consumeUInt16, uint16_t)
214 CONSUME_FUNC(consumeUInt32, uint32_t)
215 CONSUME_FUNC(consumeUInt64, uint64_t)
216
217 /*
218  * dbgprintf - Uses the logging function provided by the user to log a single
219  *   message, typically without a carriage-return.
220  *
221  * @param insn    - The instruction containing the logging function.
222  * @param format  - See printf().
223  * @param ...     - See printf().
224  */
225 static void dbgprintf(struct InternalInstruction* insn,
226                       const char* format,
227                       ...) {  
228   char buffer[256];
229   va_list ap;
230   
231   if (!insn->dlog)
232     return;
233     
234   va_start(ap, format);
235   (void)vsnprintf(buffer, sizeof(buffer), format, ap);
236   va_end(ap);
237   
238   insn->dlog(insn->dlogArg, buffer);
239   
240   return;
241 }
242
243 /*
244  * setPrefixPresent - Marks that a particular prefix is present at a particular
245  *   location.
246  *
247  * @param insn      - The instruction to be marked as having the prefix.
248  * @param prefix    - The prefix that is present.
249  * @param location  - The location where the prefix is located (in the address
250  *                    space of the instruction's reader).
251  */
252 static void setPrefixPresent(struct InternalInstruction* insn,
253                                     uint8_t prefix,
254                                     uint64_t location)
255 {
256   insn->prefixPresent[prefix] = 1;
257   insn->prefixLocations[prefix] = location;
258 }
259
260 /*
261  * isPrefixAtLocation - Queries an instruction to determine whether a prefix is
262  *   present at a given location.
263  *
264  * @param insn      - The instruction to be queried.
265  * @param prefix    - The prefix.
266  * @param location  - The location to query.
267  * @return          - Whether the prefix is at that location.
268  */
269 static BOOL isPrefixAtLocation(struct InternalInstruction* insn,
270                                uint8_t prefix,
271                                uint64_t location)
272 {
273   if (insn->prefixPresent[prefix] == 1 &&
274      insn->prefixLocations[prefix] == location)
275     return TRUE;
276   else
277     return FALSE;
278 }
279
280 /*
281  * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
282  *   instruction as having them.  Also sets the instruction's default operand,
283  *   address, and other relevant data sizes to report operands correctly.
284  *
285  * @param insn  - The instruction whose prefixes are to be read.
286  * @return      - 0 if the instruction could be read until the end of the prefix
287  *                bytes, and no prefixes conflicted; nonzero otherwise.
288  */
289 static int readPrefixes(struct InternalInstruction* insn) {
290   BOOL isPrefix = TRUE;
291   BOOL prefixGroups[4] = { FALSE };
292   uint64_t prefixLocation;
293   uint8_t byte = 0;
294   
295   BOOL hasAdSize = FALSE;
296   BOOL hasOpSize = FALSE;
297   
298   dbgprintf(insn, "readPrefixes()");
299     
300   while (isPrefix) {
301     prefixLocation = insn->readerCursor;
302     
303     if (consumeByte(insn, &byte))
304       return -1;
305     
306     switch (byte) {
307     case 0xf0:  /* LOCK */
308     case 0xf2:  /* REPNE/REPNZ */
309     case 0xf3:  /* REP or REPE/REPZ */
310       if (prefixGroups[0])
311         dbgprintf(insn, "Redundant Group 1 prefix");
312       prefixGroups[0] = TRUE;
313       setPrefixPresent(insn, byte, prefixLocation);
314       break;
315     case 0x2e:  /* CS segment override -OR- Branch not taken */
316     case 0x36:  /* SS segment override -OR- Branch taken */
317     case 0x3e:  /* DS segment override */
318     case 0x26:  /* ES segment override */
319     case 0x64:  /* FS segment override */
320     case 0x65:  /* GS segment override */
321       switch (byte) {
322       case 0x2e:
323         insn->segmentOverride = SEG_OVERRIDE_CS;
324         break;
325       case 0x36:
326         insn->segmentOverride = SEG_OVERRIDE_SS;
327         break;
328       case 0x3e:
329         insn->segmentOverride = SEG_OVERRIDE_DS;
330         break;
331       case 0x26:
332         insn->segmentOverride = SEG_OVERRIDE_ES;
333         break;
334       case 0x64:
335         insn->segmentOverride = SEG_OVERRIDE_FS;
336         break;
337       case 0x65:
338         insn->segmentOverride = SEG_OVERRIDE_GS;
339         break;
340       default:
341         debug("Unhandled override");
342         return -1;
343       }
344       if (prefixGroups[1])
345         dbgprintf(insn, "Redundant Group 2 prefix");
346       prefixGroups[1] = TRUE;
347       setPrefixPresent(insn, byte, prefixLocation);
348       break;
349     case 0x66:  /* Operand-size override */
350       if (prefixGroups[2])
351         dbgprintf(insn, "Redundant Group 3 prefix");
352       prefixGroups[2] = TRUE;
353       hasOpSize = TRUE;
354       setPrefixPresent(insn, byte, prefixLocation);
355       break;
356     case 0x67:  /* Address-size override */
357       if (prefixGroups[3])
358         dbgprintf(insn, "Redundant Group 4 prefix");
359       prefixGroups[3] = TRUE;
360       hasAdSize = TRUE;
361       setPrefixPresent(insn, byte, prefixLocation);
362       break;
363     default:    /* Not a prefix byte */
364       isPrefix = FALSE;
365       break;
366     }
367     
368     if (isPrefix)
369       dbgprintf(insn, "Found prefix 0x%hhx", byte);
370   }
371     
372   insn->vexSize = 0;
373   
374   if (byte == 0xc4) {
375     uint8_t byte1;
376       
377     if (lookAtByte(insn, &byte1)) {
378       dbgprintf(insn, "Couldn't read second byte of VEX");
379       return -1;
380     }
381     
382     if (insn->mode == MODE_64BIT || byte1 & 0x8) {
383       insn->vexSize = 3;
384       insn->necessaryPrefixLocation = insn->readerCursor - 1;
385     }
386     else {
387       unconsumeByte(insn);
388       insn->necessaryPrefixLocation = insn->readerCursor - 1;
389     }
390     
391     if (insn->vexSize == 3) {
392       insn->vexPrefix[0] = byte;
393       consumeByte(insn, &insn->vexPrefix[1]);
394       consumeByte(insn, &insn->vexPrefix[2]);
395
396       /* We simulate the REX prefix for simplicity's sake */
397     
398       insn->rexPrefix = 0x40 
399                       | (wFromVEX3of3(insn->vexPrefix[2]) << 3)
400                       | (rFromVEX2of3(insn->vexPrefix[1]) << 2)
401                       | (xFromVEX2of3(insn->vexPrefix[1]) << 1)
402                       | (bFromVEX2of3(insn->vexPrefix[1]) << 0);
403     
404       switch (ppFromVEX3of3(insn->vexPrefix[2]))
405       {
406       default:
407         break;
408       case VEX_PREFIX_66:
409         hasOpSize = TRUE;      
410         break;
411       }
412     
413       dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx", insn->vexPrefix[0], insn->vexPrefix[1], insn->vexPrefix[2]);
414     }
415   }
416   else if (byte == 0xc5) {
417     uint8_t byte1;
418     
419     if (lookAtByte(insn, &byte1)) {
420       dbgprintf(insn, "Couldn't read second byte of VEX");
421       return -1;
422     }
423       
424     if (insn->mode == MODE_64BIT || byte1 & 0x8) {
425       insn->vexSize = 2;
426     }
427     else {
428       unconsumeByte(insn);
429     }
430     
431     if (insn->vexSize == 2) {
432       insn->vexPrefix[0] = byte;
433       consumeByte(insn, &insn->vexPrefix[1]);
434         
435       insn->rexPrefix = 0x40 
436                       | (rFromVEX2of2(insn->vexPrefix[1]) << 2);
437         
438       switch (ppFromVEX2of2(insn->vexPrefix[1]))
439       {
440       default:
441         break;
442       case VEX_PREFIX_66:
443         hasOpSize = TRUE;      
444         break;
445       }
446          
447       dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx", insn->vexPrefix[0], insn->vexPrefix[1]);
448     }
449   }
450   else {
451     if (insn->mode == MODE_64BIT) {
452       if ((byte & 0xf0) == 0x40) {
453         uint8_t opcodeByte;
454           
455         if (lookAtByte(insn, &opcodeByte) || ((opcodeByte & 0xf0) == 0x40)) {
456           dbgprintf(insn, "Redundant REX prefix");
457           return -1;
458         }
459           
460         insn->rexPrefix = byte;
461         insn->necessaryPrefixLocation = insn->readerCursor - 2;
462           
463         dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
464       } else {                
465         unconsumeByte(insn);
466         insn->necessaryPrefixLocation = insn->readerCursor - 1;
467       }
468     } else {
469       unconsumeByte(insn);
470       insn->necessaryPrefixLocation = insn->readerCursor - 1;
471     }
472   }
473
474   if (insn->mode == MODE_16BIT) {
475     insn->registerSize       = (hasOpSize ? 4 : 2);
476     insn->addressSize        = (hasAdSize ? 4 : 2);
477     insn->displacementSize   = (hasAdSize ? 4 : 2);
478     insn->immediateSize      = (hasOpSize ? 4 : 2);
479   } else if (insn->mode == MODE_32BIT) {
480     insn->registerSize       = (hasOpSize ? 2 : 4);
481     insn->addressSize        = (hasAdSize ? 2 : 4);
482     insn->displacementSize   = (hasAdSize ? 2 : 4);
483     insn->immediateSize      = (hasOpSize ? 2 : 4);
484   } else if (insn->mode == MODE_64BIT) {
485     if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
486       insn->registerSize       = 8;
487       insn->addressSize        = (hasAdSize ? 4 : 8);
488       insn->displacementSize   = 4;
489       insn->immediateSize      = 4;
490     } else if (insn->rexPrefix) {
491       insn->registerSize       = (hasOpSize ? 2 : 4);
492       insn->addressSize        = (hasAdSize ? 4 : 8);
493       insn->displacementSize   = (hasOpSize ? 2 : 4);
494       insn->immediateSize      = (hasOpSize ? 2 : 4);
495     } else {
496       insn->registerSize       = (hasOpSize ? 2 : 4);
497       insn->addressSize        = (hasAdSize ? 4 : 8);
498       insn->displacementSize   = (hasOpSize ? 2 : 4);
499       insn->immediateSize      = (hasOpSize ? 2 : 4);
500     }
501   }
502   
503   return 0;
504 }
505
506 /*
507  * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
508  *   extended or escape opcodes).
509  *
510  * @param insn  - The instruction whose opcode is to be read.
511  * @return      - 0 if the opcode could be read successfully; nonzero otherwise.
512  */
513 static int readOpcode(struct InternalInstruction* insn) {  
514   /* Determine the length of the primary opcode */
515   
516   uint8_t current;
517   
518   dbgprintf(insn, "readOpcode()");
519   
520   insn->opcodeType = ONEBYTE;
521     
522   if (insn->vexSize == 3)
523   {
524     switch (mmmmmFromVEX2of3(insn->vexPrefix[1]))
525     {
526     default:
527       dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)", mmmmmFromVEX2of3(insn->vexPrefix[1]));
528       return -1;      
529     case 0:
530       break;
531     case VEX_LOB_0F:
532       insn->twoByteEscape = 0x0f;
533       insn->opcodeType = TWOBYTE;
534       return consumeByte(insn, &insn->opcode);
535     case VEX_LOB_0F38:
536       insn->twoByteEscape = 0x0f;
537       insn->threeByteEscape = 0x38;
538       insn->opcodeType = THREEBYTE_38;
539       return consumeByte(insn, &insn->opcode);
540     case VEX_LOB_0F3A:    
541       insn->twoByteEscape = 0x0f;
542       insn->threeByteEscape = 0x3a;
543       insn->opcodeType = THREEBYTE_3A;
544       return consumeByte(insn, &insn->opcode);
545     }
546   }
547   else if (insn->vexSize == 2)
548   {
549     insn->twoByteEscape = 0x0f;
550     insn->opcodeType = TWOBYTE;
551     return consumeByte(insn, &insn->opcode);
552   }
553     
554   if (consumeByte(insn, &current))
555     return -1;
556   
557   if (current == 0x0f) {
558     dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
559     
560     insn->twoByteEscape = current;
561     
562     if (consumeByte(insn, &current))
563       return -1;
564     
565     if (current == 0x38) {
566       dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
567       
568       insn->threeByteEscape = current;
569       
570       if (consumeByte(insn, &current))
571         return -1;
572       
573       insn->opcodeType = THREEBYTE_38;
574     } else if (current == 0x3a) {
575       dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
576       
577       insn->threeByteEscape = current;
578       
579       if (consumeByte(insn, &current))
580         return -1;
581       
582       insn->opcodeType = THREEBYTE_3A;
583     } else {
584       dbgprintf(insn, "Didn't find a three-byte escape prefix");
585       
586       insn->opcodeType = TWOBYTE;
587     }
588   }
589   
590   /*
591    * At this point we have consumed the full opcode.
592    * Anything we consume from here on must be unconsumed.
593    */
594   
595   insn->opcode = current;
596   
597   return 0;
598 }
599
600 static int readModRM(struct InternalInstruction* insn);
601
602 /*
603  * getIDWithAttrMask - Determines the ID of an instruction, consuming
604  *   the ModR/M byte as appropriate for extended and escape opcodes,
605  *   and using a supplied attribute mask.
606  *
607  * @param instructionID - A pointer whose target is filled in with the ID of the
608  *                        instruction.
609  * @param insn          - The instruction whose ID is to be determined.
610  * @param attrMask      - The attribute mask to search.
611  * @return              - 0 if the ModR/M could be read when needed or was not
612  *                        needed; nonzero otherwise.
613  */
614 static int getIDWithAttrMask(uint16_t* instructionID,
615                              struct InternalInstruction* insn,
616                              uint8_t attrMask) {
617   BOOL hasModRMExtension;
618   
619   uint8_t instructionClass;
620
621   instructionClass = contextForAttrs(attrMask);
622   
623   hasModRMExtension = modRMRequired(insn->opcodeType,
624                                     instructionClass,
625                                     insn->opcode);
626   
627   if (hasModRMExtension) {
628     if (readModRM(insn))
629       return -1;
630     
631     *instructionID = decode(insn->opcodeType,
632                             instructionClass,
633                             insn->opcode,
634                             insn->modRM);
635   } else {
636     *instructionID = decode(insn->opcodeType,
637                             instructionClass,
638                             insn->opcode,
639                             0);
640   }
641       
642   return 0;
643 }
644
645 /*
646  * is16BitEquivalent - Determines whether two instruction names refer to
647  * equivalent instructions but one is 16-bit whereas the other is not.
648  *
649  * @param orig  - The instruction that is not 16-bit
650  * @param equiv - The instruction that is 16-bit
651  */
652 static BOOL is16BitEquvalent(const char* orig, const char* equiv) {
653   off_t i;
654   
655   for (i = 0;; i++) {
656     if (orig[i] == '\0' && equiv[i] == '\0')
657       return TRUE;
658     if (orig[i] == '\0' || equiv[i] == '\0')
659       return FALSE;
660     if (orig[i] != equiv[i]) {
661       if ((orig[i] == 'Q' || orig[i] == 'L') && equiv[i] == 'W')
662         continue;
663       if ((orig[i] == '6' || orig[i] == '3') && equiv[i] == '1')
664         continue;
665       if ((orig[i] == '4' || orig[i] == '2') && equiv[i] == '6')
666         continue;
667       return FALSE;
668     }
669   }
670 }
671
672 /*
673  * is64BitEquivalent - Determines whether two instruction names refer to
674  * equivalent instructions but one is 64-bit whereas the other is not.
675  *
676  * @param orig  - The instruction that is not 64-bit
677  * @param equiv - The instruction that is 64-bit
678  */
679 static BOOL is64BitEquivalent(const char* orig, const char* equiv) {
680   off_t i;
681   
682   for (i = 0;; i++) {
683     if (orig[i] == '\0' && equiv[i] == '\0')
684       return TRUE;
685     if (orig[i] == '\0' || equiv[i] == '\0')
686       return FALSE;
687     if (orig[i] != equiv[i]) {
688       if ((orig[i] == 'W' || orig[i] == 'L') && equiv[i] == 'Q')
689         continue;
690       if ((orig[i] == '1' || orig[i] == '3') && equiv[i] == '6')
691         continue;
692       if ((orig[i] == '6' || orig[i] == '2') && equiv[i] == '4')
693         continue;
694       return FALSE;
695     }
696   }
697 }
698
699
700 /*
701  * getID - Determines the ID of an instruction, consuming the ModR/M byte as 
702  *   appropriate for extended and escape opcodes.  Determines the attributes and 
703  *   context for the instruction before doing so.
704  *
705  * @param insn  - The instruction whose ID is to be determined.
706  * @return      - 0 if the ModR/M could be read when needed or was not needed;
707  *                nonzero otherwise.
708  */
709 static int getID(struct InternalInstruction* insn) {  
710   uint8_t attrMask;
711   uint16_t instructionID;
712   
713   dbgprintf(insn, "getID()");
714     
715   attrMask = ATTR_NONE;
716
717   if (insn->mode == MODE_64BIT)
718     attrMask |= ATTR_64BIT;
719     
720   if (insn->vexSize) {
721     attrMask |= ATTR_VEX;
722
723     if (insn->vexSize == 3) {
724       switch (ppFromVEX3of3(insn->vexPrefix[2])) {
725       case VEX_PREFIX_66:
726         attrMask |= ATTR_OPSIZE;    
727         break;
728       case VEX_PREFIX_F3:
729         attrMask |= ATTR_XS;
730         break;
731       case VEX_PREFIX_F2:
732         attrMask |= ATTR_XD;
733         break;
734       }
735     
736       if (wFromVEX3of3(insn->vexPrefix[2]))
737         attrMask |= ATTR_REXW;
738       if (lFromVEX3of3(insn->vexPrefix[2]))
739         attrMask |= ATTR_VEXL;
740     }
741     else if (insn->vexSize == 2) {
742       switch (ppFromVEX2of2(insn->vexPrefix[1])) {
743       case VEX_PREFIX_66:
744         attrMask |= ATTR_OPSIZE;    
745         break;
746       case VEX_PREFIX_F3:
747         attrMask |= ATTR_XS;
748         break;
749       case VEX_PREFIX_F2:
750         attrMask |= ATTR_XD;
751         break;
752       }
753     
754       if (lFromVEX2of2(insn->vexPrefix[1]))
755         attrMask |= ATTR_VEXL;
756     }
757     else {
758       return -1;
759     }
760   }
761   else {
762     if (insn->rexPrefix & 0x08)
763       attrMask |= ATTR_REXW;
764   
765     if (isPrefixAtLocation(insn, 0x66, insn->necessaryPrefixLocation))
766       attrMask |= ATTR_OPSIZE;
767     else if (isPrefixAtLocation(insn, 0xf3, insn->necessaryPrefixLocation))
768       attrMask |= ATTR_XS;
769     else if (isPrefixAtLocation(insn, 0xf2, insn->necessaryPrefixLocation))
770       attrMask |= ATTR_XD;
771     
772   }
773
774   if (getIDWithAttrMask(&instructionID, insn, attrMask))
775     return -1;
776   
777   /* The following clauses compensate for limitations of the tables. */
778   
779   if ((attrMask & ATTR_XD) && (attrMask & ATTR_REXW)) {
780     /*
781      * Although for SSE instructions it is usually necessary to treat REX.W+F2
782      * as F2 for decode (in the absence of a 64BIT_REXW_XD category) there is
783      * an occasional instruction where F2 is incidental and REX.W is the more
784      * significant.  If the decoded instruction is 32-bit and adding REX.W
785      * instead of F2 changes a 32 to a 64, we adopt the new encoding.
786      */
787     
788     const struct InstructionSpecifier *spec;
789     uint16_t instructionIDWithREXw;
790     const struct InstructionSpecifier *specWithREXw;
791     
792     spec = specifierForUID(instructionID);
793     
794     if (getIDWithAttrMask(&instructionIDWithREXw,
795                           insn,
796                           attrMask & (~ATTR_XD))) {
797       /*
798        * Decoding with REX.w would yield nothing; give up and return original
799        * decode.
800        */
801       
802       insn->instructionID = instructionID;
803       insn->spec = spec;
804       return 0;
805     }
806     
807     specWithREXw = specifierForUID(instructionIDWithREXw);
808     
809     if (is64BitEquivalent(spec->name, specWithREXw->name)) {
810       insn->instructionID = instructionIDWithREXw;
811       insn->spec = specWithREXw;
812     } else {
813       insn->instructionID = instructionID;
814       insn->spec = spec;
815     }
816     return 0;
817   }
818   
819   if (insn->prefixPresent[0x66] && !(attrMask & ATTR_OPSIZE)) {
820     /*
821      * The instruction tables make no distinction between instructions that
822      * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
823      * particular spot (i.e., many MMX operations).  In general we're
824      * conservative, but in the specific case where OpSize is present but not
825      * in the right place we check if there's a 16-bit operation.
826      */
827     
828     const struct InstructionSpecifier *spec;
829     uint16_t instructionIDWithOpsize;
830     const struct InstructionSpecifier *specWithOpsize;
831     
832     spec = specifierForUID(instructionID);
833     
834     if (getIDWithAttrMask(&instructionIDWithOpsize,
835                           insn,
836                           attrMask | ATTR_OPSIZE)) {
837       /* 
838        * ModRM required with OpSize but not present; give up and return version
839        * without OpSize set
840        */
841       
842       insn->instructionID = instructionID;
843       insn->spec = spec;
844       return 0;
845     }
846     
847     specWithOpsize = specifierForUID(instructionIDWithOpsize);
848     
849     if (is16BitEquvalent(spec->name, specWithOpsize->name)) {
850       insn->instructionID = instructionIDWithOpsize;
851       insn->spec = specWithOpsize;
852     } else {
853       insn->instructionID = instructionID;
854       insn->spec = spec;
855     }
856     return 0;
857   }
858   
859   insn->instructionID = instructionID;
860   insn->spec = specifierForUID(insn->instructionID);
861   
862   return 0;
863 }
864
865 /*
866  * readSIB - Consumes the SIB byte to determine addressing information for an
867  *   instruction.
868  *
869  * @param insn  - The instruction whose SIB byte is to be read.
870  * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
871  */
872 static int readSIB(struct InternalInstruction* insn) {
873   SIBIndex sibIndexBase = 0;
874   SIBBase sibBaseBase = 0;
875   uint8_t index, base;
876   
877   dbgprintf(insn, "readSIB()");
878   
879   if (insn->consumedSIB)
880     return 0;
881   
882   insn->consumedSIB = TRUE;
883   
884   switch (insn->addressSize) {
885   case 2:
886     dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
887     return -1;
888     break;
889   case 4:
890     sibIndexBase = SIB_INDEX_EAX;
891     sibBaseBase = SIB_BASE_EAX;
892     break;
893   case 8:
894     sibIndexBase = SIB_INDEX_RAX;
895     sibBaseBase = SIB_BASE_RAX;
896     break;
897   }
898
899   if (consumeByte(insn, &insn->sib))
900     return -1;
901   
902   index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
903   
904   switch (index) {
905   case 0x4:
906     insn->sibIndex = SIB_INDEX_NONE;
907     break;
908   default:
909     insn->sibIndex = (SIBIndex)(sibIndexBase + index);
910     if (insn->sibIndex == SIB_INDEX_sib ||
911         insn->sibIndex == SIB_INDEX_sib64)
912       insn->sibIndex = SIB_INDEX_NONE;
913     break;
914   }
915   
916   switch (scaleFromSIB(insn->sib)) {
917   case 0:
918     insn->sibScale = 1;
919     break;
920   case 1:
921     insn->sibScale = 2;
922     break;
923   case 2:
924     insn->sibScale = 4;
925     break;
926   case 3:
927     insn->sibScale = 8;
928     break;
929   }
930   
931   base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
932   
933   switch (base) {
934   case 0x5:
935     switch (modFromModRM(insn->modRM)) {
936     case 0x0:
937       insn->eaDisplacement = EA_DISP_32;
938       insn->sibBase = SIB_BASE_NONE;
939       break;
940     case 0x1:
941       insn->eaDisplacement = EA_DISP_8;
942       insn->sibBase = (insn->addressSize == 4 ? 
943                        SIB_BASE_EBP : SIB_BASE_RBP);
944       break;
945     case 0x2:
946       insn->eaDisplacement = EA_DISP_32;
947       insn->sibBase = (insn->addressSize == 4 ? 
948                        SIB_BASE_EBP : SIB_BASE_RBP);
949       break;
950     case 0x3:
951       debug("Cannot have Mod = 0b11 and a SIB byte");
952       return -1;
953     }
954     break;
955   default:
956     insn->sibBase = (SIBBase)(sibBaseBase + base);
957     break;
958   }
959   
960   return 0;
961 }
962
963 /*
964  * readDisplacement - Consumes the displacement of an instruction.
965  *
966  * @param insn  - The instruction whose displacement is to be read.
967  * @return      - 0 if the displacement byte was successfully read; nonzero 
968  *                otherwise.
969  */
970 static int readDisplacement(struct InternalInstruction* insn) {  
971   int8_t d8;
972   int16_t d16;
973   int32_t d32;
974   
975   dbgprintf(insn, "readDisplacement()");
976   
977   if (insn->consumedDisplacement)
978     return 0;
979   
980   insn->consumedDisplacement = TRUE;
981   
982   switch (insn->eaDisplacement) {
983   case EA_DISP_NONE:
984     insn->consumedDisplacement = FALSE;
985     break;
986   case EA_DISP_8:
987     if (consumeInt8(insn, &d8))
988       return -1;
989     insn->displacement = d8;
990     break;
991   case EA_DISP_16:
992     if (consumeInt16(insn, &d16))
993       return -1;
994     insn->displacement = d16;
995     break;
996   case EA_DISP_32:
997     if (consumeInt32(insn, &d32))
998       return -1;
999     insn->displacement = d32;
1000     break;
1001   }
1002   
1003   insn->consumedDisplacement = TRUE;
1004   return 0;
1005 }
1006
1007 /*
1008  * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1009  *   displacement) for an instruction and interprets it.
1010  *
1011  * @param insn  - The instruction whose addressing information is to be read.
1012  * @return      - 0 if the information was successfully read; nonzero otherwise.
1013  */
1014 static int readModRM(struct InternalInstruction* insn) {  
1015   uint8_t mod, rm, reg;
1016   
1017   dbgprintf(insn, "readModRM()");
1018   
1019   if (insn->consumedModRM)
1020     return 0;
1021   
1022   if (consumeByte(insn, &insn->modRM))
1023     return -1;
1024   insn->consumedModRM = TRUE;
1025   
1026   mod     = modFromModRM(insn->modRM);
1027   rm      = rmFromModRM(insn->modRM);
1028   reg     = regFromModRM(insn->modRM);
1029   
1030   /*
1031    * This goes by insn->registerSize to pick the correct register, which messes
1032    * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
1033    * fixupReg().
1034    */
1035   switch (insn->registerSize) {
1036   case 2:
1037     insn->regBase = MODRM_REG_AX;
1038     insn->eaRegBase = EA_REG_AX;
1039     break;
1040   case 4:
1041     insn->regBase = MODRM_REG_EAX;
1042     insn->eaRegBase = EA_REG_EAX;
1043     break;
1044   case 8:
1045     insn->regBase = MODRM_REG_RAX;
1046     insn->eaRegBase = EA_REG_RAX;
1047     break;
1048   }
1049   
1050   reg |= rFromREX(insn->rexPrefix) << 3;
1051   rm  |= bFromREX(insn->rexPrefix) << 3;
1052   
1053   insn->reg = (Reg)(insn->regBase + reg);
1054   
1055   switch (insn->addressSize) {
1056   case 2:
1057     insn->eaBaseBase = EA_BASE_BX_SI;
1058      
1059     switch (mod) {
1060     case 0x0:
1061       if (rm == 0x6) {
1062         insn->eaBase = EA_BASE_NONE;
1063         insn->eaDisplacement = EA_DISP_16;
1064         if (readDisplacement(insn))
1065           return -1;
1066       } else {
1067         insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1068         insn->eaDisplacement = EA_DISP_NONE;
1069       }
1070       break;
1071     case 0x1:
1072       insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1073       insn->eaDisplacement = EA_DISP_8;
1074       if (readDisplacement(insn))
1075         return -1;
1076       break;
1077     case 0x2:
1078       insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1079       insn->eaDisplacement = EA_DISP_16;
1080       if (readDisplacement(insn))
1081         return -1;
1082       break;
1083     case 0x3:
1084       insn->eaBase = (EABase)(insn->eaRegBase + rm);
1085       if (readDisplacement(insn))
1086         return -1;
1087       break;
1088     }
1089     break;
1090   case 4:
1091   case 8:
1092     insn->eaBaseBase = (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1093     
1094     switch (mod) {
1095     case 0x0:
1096       insn->eaDisplacement = EA_DISP_NONE; /* readSIB may override this */
1097       switch (rm) {
1098       case 0x4:
1099       case 0xc:   /* in case REXW.b is set */
1100         insn->eaBase = (insn->addressSize == 4 ? 
1101                         EA_BASE_sib : EA_BASE_sib64);
1102         readSIB(insn);
1103         if (readDisplacement(insn))
1104           return -1;
1105         break;
1106       case 0x5:
1107         insn->eaBase = EA_BASE_NONE;
1108         insn->eaDisplacement = EA_DISP_32;
1109         if (readDisplacement(insn))
1110           return -1;
1111         break;
1112       default:
1113         insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1114         break;
1115       }
1116       break;
1117     case 0x1:
1118     case 0x2:
1119       insn->eaDisplacement = (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1120       switch (rm) {
1121       case 0x4:
1122       case 0xc:   /* in case REXW.b is set */
1123         insn->eaBase = EA_BASE_sib;
1124         readSIB(insn);
1125         if (readDisplacement(insn))
1126           return -1;
1127         break;
1128       default:
1129         insn->eaBase = (EABase)(insn->eaBaseBase + rm);
1130         if (readDisplacement(insn))
1131           return -1;
1132         break;
1133       }
1134       break;
1135     case 0x3:
1136       insn->eaDisplacement = EA_DISP_NONE;
1137       insn->eaBase = (EABase)(insn->eaRegBase + rm);
1138       break;
1139     }
1140     break;
1141   } /* switch (insn->addressSize) */
1142   
1143   return 0;
1144 }
1145
1146 #define GENERIC_FIXUP_FUNC(name, base, prefix)            \
1147   static uint8_t name(struct InternalInstruction *insn,   \
1148                       OperandType type,                   \
1149                       uint8_t index,                      \
1150                       uint8_t *valid) {                   \
1151     *valid = 1;                                           \
1152     switch (type) {                                       \
1153     default:                                              \
1154       debug("Unhandled register type");                   \
1155       *valid = 0;                                         \
1156       return 0;                                           \
1157     case TYPE_Rv:                                         \
1158       return base + index;                                \
1159     case TYPE_R8:                                         \
1160       if (insn->rexPrefix &&                              \
1161          index >= 4 && index <= 7) {                      \
1162         return prefix##_SPL + (index - 4);                \
1163       } else {                                            \
1164         return prefix##_AL + index;                       \
1165       }                                                   \
1166     case TYPE_R16:                                        \
1167       return prefix##_AX + index;                         \
1168     case TYPE_R32:                                        \
1169       return prefix##_EAX + index;                        \
1170     case TYPE_R64:                                        \
1171       return prefix##_RAX + index;                        \
1172     case TYPE_XMM256:                                     \
1173       return prefix##_YMM0 + index;                       \
1174     case TYPE_XMM128:                                     \
1175     case TYPE_XMM64:                                      \
1176     case TYPE_XMM32:                                      \
1177     case TYPE_XMM:                                        \
1178       return prefix##_XMM0 + index;                       \
1179     case TYPE_MM64:                                       \
1180     case TYPE_MM32:                                       \
1181     case TYPE_MM:                                         \
1182       if (index > 7)                                      \
1183         *valid = 0;                                       \
1184       return prefix##_MM0 + index;                        \
1185     case TYPE_SEGMENTREG:                                 \
1186       if (index > 5)                                      \
1187         *valid = 0;                                       \
1188       return prefix##_ES + index;                         \
1189     case TYPE_DEBUGREG:                                   \
1190       if (index > 7)                                      \
1191         *valid = 0;                                       \
1192       return prefix##_DR0 + index;                        \
1193     case TYPE_CONTROLREG:                                 \
1194       if (index > 8)                                      \
1195         *valid = 0;                                       \
1196       return prefix##_CR0 + index;                        \
1197     }                                                     \
1198   }
1199
1200 /*
1201  * fixup*Value - Consults an operand type to determine the meaning of the
1202  *   reg or R/M field.  If the operand is an XMM operand, for example, an
1203  *   operand would be XMM0 instead of AX, which readModRM() would otherwise
1204  *   misinterpret it as.
1205  *
1206  * @param insn  - The instruction containing the operand.
1207  * @param type  - The operand type.
1208  * @param index - The existing value of the field as reported by readModRM().
1209  * @param valid - The address of a uint8_t.  The target is set to 1 if the
1210  *                field is valid for the register class; 0 if not.
1211  * @return      - The proper value.
1212  */
1213 GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase,    MODRM_REG)
1214 GENERIC_FIXUP_FUNC(fixupRMValue,  insn->eaRegBase,  EA_REG)
1215
1216 /*
1217  * fixupReg - Consults an operand specifier to determine which of the
1218  *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1219  *
1220  * @param insn  - See fixup*Value().
1221  * @param op    - The operand specifier.
1222  * @return      - 0 if fixup was successful; -1 if the register returned was
1223  *                invalid for its class.
1224  */
1225 static int fixupReg(struct InternalInstruction *insn, 
1226                     const struct OperandSpecifier *op) {
1227   uint8_t valid;
1228   
1229   dbgprintf(insn, "fixupReg()");
1230   
1231   switch ((OperandEncoding)op->encoding) {
1232   default:
1233     debug("Expected a REG or R/M encoding in fixupReg");
1234     return -1;
1235   case ENCODING_VVVV:
1236     insn->vvvv = (Reg)fixupRegValue(insn,
1237                                     (OperandType)op->type,
1238                                     insn->vvvv,
1239                                     &valid);
1240     if (!valid)
1241       return -1;
1242     break;
1243   case ENCODING_REG:
1244     insn->reg = (Reg)fixupRegValue(insn,
1245                                    (OperandType)op->type,
1246                                    insn->reg - insn->regBase,
1247                                    &valid);
1248     if (!valid)
1249       return -1;
1250     break;
1251   case ENCODING_RM:
1252     if (insn->eaBase >= insn->eaRegBase) {
1253       insn->eaBase = (EABase)fixupRMValue(insn,
1254                                           (OperandType)op->type,
1255                                           insn->eaBase - insn->eaRegBase,
1256                                           &valid);
1257       if (!valid)
1258         return -1;
1259     }
1260     break;
1261   }
1262   
1263   return 0;
1264 }
1265
1266 /*
1267  * readOpcodeModifier - Reads an operand from the opcode field of an 
1268  *   instruction.  Handles AddRegFrm instructions.
1269  *
1270  * @param insn    - The instruction whose opcode field is to be read.
1271  * @param inModRM - Indicates that the opcode field is to be read from the
1272  *                  ModR/M extension; useful for escape opcodes
1273  * @return        - 0 on success; nonzero otherwise.
1274  */
1275 static int readOpcodeModifier(struct InternalInstruction* insn) {
1276   dbgprintf(insn, "readOpcodeModifier()");
1277   
1278   if (insn->consumedOpcodeModifier)
1279     return 0;
1280   
1281   insn->consumedOpcodeModifier = TRUE;
1282   
1283   switch (insn->spec->modifierType) {
1284   default:
1285     debug("Unknown modifier type.");
1286     return -1;
1287   case MODIFIER_NONE:
1288     debug("No modifier but an operand expects one.");
1289     return -1;
1290   case MODIFIER_OPCODE:
1291     insn->opcodeModifier = insn->opcode - insn->spec->modifierBase;
1292     return 0;
1293   case MODIFIER_MODRM:
1294     insn->opcodeModifier = insn->modRM - insn->spec->modifierBase;
1295     return 0;
1296   }  
1297 }
1298
1299 /*
1300  * readOpcodeRegister - Reads an operand from the opcode field of an 
1301  *   instruction and interprets it appropriately given the operand width.
1302  *   Handles AddRegFrm instructions.
1303  *
1304  * @param insn  - See readOpcodeModifier().
1305  * @param size  - The width (in bytes) of the register being specified.
1306  *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1307  *                RAX.
1308  * @return      - 0 on success; nonzero otherwise.
1309  */
1310 static int readOpcodeRegister(struct InternalInstruction* insn, uint8_t size) {
1311   dbgprintf(insn, "readOpcodeRegister()");
1312
1313   if (readOpcodeModifier(insn))
1314     return -1;
1315   
1316   if (size == 0)
1317     size = insn->registerSize;
1318   
1319   switch (size) {
1320   case 1:
1321     insn->opcodeRegister = (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) 
1322                                                   | insn->opcodeModifier));
1323     if (insn->rexPrefix && 
1324         insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1325         insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1326       insn->opcodeRegister = (Reg)(MODRM_REG_SPL
1327                                    + (insn->opcodeRegister - MODRM_REG_AL - 4));
1328     }
1329       
1330     break;
1331   case 2:
1332     insn->opcodeRegister = (Reg)(MODRM_REG_AX
1333                                  + ((bFromREX(insn->rexPrefix) << 3) 
1334                                     | insn->opcodeModifier));
1335     break;
1336   case 4:
1337     insn->opcodeRegister = (Reg)(MODRM_REG_EAX
1338                                  + ((bFromREX(insn->rexPrefix) << 3) 
1339                                     | insn->opcodeModifier));
1340     break;
1341   case 8:
1342     insn->opcodeRegister = (Reg)(MODRM_REG_RAX 
1343                                  + ((bFromREX(insn->rexPrefix) << 3) 
1344                                     | insn->opcodeModifier));
1345     break;
1346   }
1347   
1348   return 0;
1349 }
1350
1351 /*
1352  * readImmediate - Consumes an immediate operand from an instruction, given the
1353  *   desired operand size.
1354  *
1355  * @param insn  - The instruction whose operand is to be read.
1356  * @param size  - The width (in bytes) of the operand.
1357  * @return      - 0 if the immediate was successfully consumed; nonzero
1358  *                otherwise.
1359  */
1360 static int readImmediate(struct InternalInstruction* insn, uint8_t size) {
1361   uint8_t imm8;
1362   uint16_t imm16;
1363   uint32_t imm32;
1364   uint64_t imm64;
1365   
1366   dbgprintf(insn, "readImmediate()");
1367   
1368   if (insn->numImmediatesConsumed == 2) {
1369     debug("Already consumed two immediates");
1370     return -1;
1371   }
1372   
1373   if (size == 0)
1374     size = insn->immediateSize;
1375   else
1376     insn->immediateSize = size;
1377   
1378   switch (size) {
1379   case 1:
1380     if (consumeByte(insn, &imm8))
1381       return -1;
1382     insn->immediates[insn->numImmediatesConsumed] = imm8;
1383     break;
1384   case 2:
1385     if (consumeUInt16(insn, &imm16))
1386       return -1;
1387     insn->immediates[insn->numImmediatesConsumed] = imm16;
1388     break;
1389   case 4:
1390     if (consumeUInt32(insn, &imm32))
1391       return -1;
1392     insn->immediates[insn->numImmediatesConsumed] = imm32;
1393     break;
1394   case 8:
1395     if (consumeUInt64(insn, &imm64))
1396       return -1;
1397     insn->immediates[insn->numImmediatesConsumed] = imm64;
1398     break;
1399   }
1400   
1401   insn->numImmediatesConsumed++;
1402   
1403   return 0;
1404 }
1405
1406 /*
1407  * readVVVV - Consumes an immediate operand from an instruction, given the
1408  *   desired operand size.
1409  *
1410  * @param insn  - The instruction whose operand is to be read.
1411  * @return      - 0 if the immediate was successfully consumed; nonzero
1412  *                otherwise.
1413  */
1414 static int readVVVV(struct InternalInstruction* insn) {
1415   dbgprintf(insn, "readVVVV()");
1416         
1417   if (insn->vexSize == 3)
1418     insn->vvvv = vvvvFromVEX3of3(insn->vexPrefix[2]);
1419   else if (insn->vexSize == 2)
1420     insn->vvvv = vvvvFromVEX2of2(insn->vexPrefix[1]);
1421   else
1422     return -1;
1423
1424   return 0;
1425 }
1426
1427 /*
1428  * readOperands - Consults the specifier for an instruction and consumes all
1429  *   operands for that instruction, interpreting them as it goes.
1430  *
1431  * @param insn  - The instruction whose operands are to be read and interpreted.
1432  * @return      - 0 if all operands could be read; nonzero otherwise.
1433  */
1434 static int readOperands(struct InternalInstruction* insn) {
1435   int index;
1436   
1437   dbgprintf(insn, "readOperands()");
1438   
1439   for (index = 0; index < X86_MAX_OPERANDS; ++index) {
1440     switch (insn->spec->operands[index].encoding) {
1441     case ENCODING_NONE:
1442       break;
1443     case ENCODING_REG:
1444     case ENCODING_RM:
1445       if (readModRM(insn))
1446         return -1;
1447       if (fixupReg(insn, &insn->spec->operands[index]))
1448         return -1;
1449       break;
1450     case ENCODING_CB:
1451     case ENCODING_CW:
1452     case ENCODING_CD:
1453     case ENCODING_CP:
1454     case ENCODING_CO:
1455     case ENCODING_CT:
1456       dbgprintf(insn, "We currently don't hande code-offset encodings");
1457       return -1;
1458     case ENCODING_IB:
1459       if (readImmediate(insn, 1))
1460         return -1;
1461       if (insn->spec->operands[index].type == TYPE_IMM3 &&
1462           insn->immediates[insn->numImmediatesConsumed - 1] > 7)
1463         return -1;
1464       break;
1465     case ENCODING_IW:
1466       if (readImmediate(insn, 2))
1467         return -1;
1468       break;
1469     case ENCODING_ID:
1470       if (readImmediate(insn, 4))
1471         return -1;
1472       break;
1473     case ENCODING_IO:
1474       if (readImmediate(insn, 8))
1475         return -1;
1476       break;
1477     case ENCODING_Iv:
1478       if (readImmediate(insn, insn->immediateSize))
1479         return -1;
1480       break;
1481     case ENCODING_Ia:
1482       if (readImmediate(insn, insn->addressSize))
1483         return -1;
1484       break;
1485     case ENCODING_RB:
1486       if (readOpcodeRegister(insn, 1))
1487         return -1;
1488       break;
1489     case ENCODING_RW:
1490       if (readOpcodeRegister(insn, 2))
1491         return -1;
1492       break;
1493     case ENCODING_RD:
1494       if (readOpcodeRegister(insn, 4))
1495         return -1;
1496       break;
1497     case ENCODING_RO:
1498       if (readOpcodeRegister(insn, 8))
1499         return -1;
1500       break;
1501     case ENCODING_Rv:
1502       if (readOpcodeRegister(insn, 0))
1503         return -1;
1504       break;
1505     case ENCODING_I:
1506       if (readOpcodeModifier(insn))
1507         return -1;
1508       break;
1509     case ENCODING_VVVV:
1510       if (readVVVV(insn))
1511         return -1;
1512       if (fixupReg(insn, &insn->spec->operands[index]))
1513         return -1;
1514       break;
1515     case ENCODING_DUP:
1516       break;
1517     default:
1518       dbgprintf(insn, "Encountered an operand with an unknown encoding.");
1519       return -1;
1520     }
1521   }
1522   
1523   return 0;
1524 }
1525
1526 /*
1527  * decodeInstruction - Reads and interprets a full instruction provided by the
1528  *   user.
1529  *
1530  * @param insn      - A pointer to the instruction to be populated.  Must be 
1531  *                    pre-allocated.
1532  * @param reader    - The function to be used to read the instruction's bytes.
1533  * @param readerArg - A generic argument to be passed to the reader to store
1534  *                    any internal state.
1535  * @param logger    - If non-NULL, the function to be used to write log messages
1536  *                    and warnings.
1537  * @param loggerArg - A generic argument to be passed to the logger to store
1538  *                    any internal state.
1539  * @param startLoc  - The address (in the reader's address space) of the first
1540  *                    byte in the instruction.
1541  * @param mode      - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
1542  *                    decode the instruction in.
1543  * @return          - 0 if the instruction's memory could be read; nonzero if
1544  *                    not.
1545  */
1546 int decodeInstruction(struct InternalInstruction* insn,
1547                       byteReader_t reader,
1548                       void* readerArg,
1549                       dlog_t logger,
1550                       void* loggerArg,
1551                       uint64_t startLoc,
1552                       DisassemblerMode mode) {
1553   memset(insn, 0, sizeof(struct InternalInstruction));
1554     
1555   insn->reader = reader;
1556   insn->readerArg = readerArg;
1557   insn->dlog = logger;
1558   insn->dlogArg = loggerArg;
1559   insn->startLocation = startLoc;
1560   insn->readerCursor = startLoc;
1561   insn->mode = mode;
1562   insn->numImmediatesConsumed = 0;
1563   
1564   if (readPrefixes(insn)       ||
1565       readOpcode(insn)         ||
1566       getID(insn)              ||
1567       insn->instructionID == 0 ||
1568       readOperands(insn))
1569     return -1;
1570   
1571   insn->length = insn->readerCursor - insn->startLocation;
1572   
1573   dbgprintf(insn, "Read from 0x%llx to 0x%llx: length %zu",
1574             startLoc, insn->readerCursor, insn->length);
1575     
1576   if (insn->length > 15)
1577     dbgprintf(insn, "Instruction exceeds 15-byte limit");
1578   
1579   return 0;
1580 }