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