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