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