Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "disassembler_x86.h" |
| 18 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 19 | #include <iostream> |
| 20 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 21 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 22 | #include "base/stringprintf.h" |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 23 | #include "thread.h" |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 24 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 25 | namespace art { |
| 26 | namespace x86 { |
| 27 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 28 | size_t DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin) { |
| 29 | return DumpInstruction(os, begin); |
| 30 | } |
| 31 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 32 | void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) { |
| 33 | size_t length = 0; |
| 34 | for (const uint8_t* cur = begin; cur < end; cur += length) { |
| 35 | length = DumpInstruction(os, cur); |
| 36 | } |
| 37 | } |
| 38 | |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 39 | static const char* gReg8Names[] = { |
| 40 | "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" |
| 41 | }; |
| 42 | static const char* gExtReg8Names[] = { |
| 43 | "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", |
| 44 | "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l" |
| 45 | }; |
| 46 | static const char* gReg16Names[] = { |
| 47 | "ax", "cx", "dx", "bx", "sp", "bp", "si", "di", |
| 48 | "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" |
| 49 | }; |
| 50 | static const char* gReg32Names[] = { |
| 51 | "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", |
| 52 | "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" |
| 53 | }; |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 54 | static const char* gReg64Names[] = { |
| 55 | "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", |
| 56 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 57 | }; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 58 | |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 59 | static void DumpReg0(std::ostream& os, uint8_t rex, size_t reg, |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 60 | bool byte_operand, uint8_t size_override) { |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 61 | DCHECK_LT(reg, (rex == 0) ? 8u : 16u); |
| 62 | bool rex_w = (rex & 0b1000) != 0; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 63 | if (byte_operand) { |
| 64 | os << ((rex == 0) ? gReg8Names[reg] : gExtReg8Names[reg]); |
| 65 | } else if (rex_w) { |
| 66 | os << gReg64Names[reg]; |
| 67 | } else if (size_override == 0x66) { |
| 68 | os << gReg16Names[reg]; |
| 69 | } else { |
| 70 | os << gReg32Names[reg]; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 74 | enum RegFile { GPR, MMX, SSE }; |
| 75 | |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 76 | static void DumpAnyReg(std::ostream& os, uint8_t rex, uint8_t reg, |
| 77 | bool byte_operand, uint8_t size_override, RegFile reg_file) { |
| 78 | if (reg_file == GPR) { |
| 79 | DumpReg0(os, rex, reg, byte_operand, size_override); |
| 80 | } else if (reg_file == SSE) { |
| 81 | os << "xmm" << reg; |
| 82 | } else { |
| 83 | os << "mm" << reg; |
| 84 | } |
| 85 | } |
| 86 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 87 | static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg, |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 88 | bool byte_operand, uint8_t size_override, RegFile reg_file) { |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 89 | bool rex_r = (rex & 0b0100) != 0; |
| 90 | size_t reg_num = rex_r ? (reg + 8) : reg; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 91 | DumpAnyReg(os, rex, reg_num, byte_operand, size_override, reg_file); |
| 92 | } |
| 93 | |
| 94 | static void DumpRmReg(std::ostream& os, uint8_t rex, uint8_t reg, |
| 95 | bool byte_operand, uint8_t size_override, RegFile reg_file) { |
| 96 | bool rex_b = (rex & 0b0001) != 0; |
| 97 | size_t reg_num = rex_b ? (reg + 8) : reg; |
| 98 | DumpAnyReg(os, rex, reg_num, byte_operand, size_override, reg_file); |
| 99 | } |
| 100 | |
| 101 | static void DumpAddrReg(std::ostream& os, uint8_t rex, uint8_t reg) { |
| 102 | if (rex != 0) { |
| 103 | os << gReg64Names[reg]; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 104 | } else { |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 105 | os << gReg32Names[reg]; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 106 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 109 | static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) { |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 110 | bool rex_b = (rex & 0b0001) != 0; |
| 111 | size_t reg_num = rex_b ? (reg + 8) : reg; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 112 | DumpAddrReg(os, rex, reg_num); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 115 | static void DumpIndexReg(std::ostream& os, uint8_t rex, uint8_t reg) { |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 116 | bool rex_x = (rex & 0b0010) != 0; |
| 117 | uint8_t reg_num = rex_x ? (reg + 8) : reg; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 118 | DumpAddrReg(os, rex, reg_num); |
| 119 | } |
| 120 | |
| 121 | static void DumpOpcodeReg(std::ostream& os, uint8_t rex, uint8_t reg) { |
| 122 | bool rex_b = (rex & 0b0001) != 0; |
| 123 | size_t reg_num = rex_b ? (reg + 8) : reg; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 124 | DumpReg0(os, rex, reg_num, false, 0); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 127 | enum SegmentPrefix { |
| 128 | kCs = 0x2e, |
| 129 | kSs = 0x36, |
| 130 | kDs = 0x3e, |
| 131 | kEs = 0x26, |
| 132 | kFs = 0x64, |
| 133 | kGs = 0x65, |
| 134 | }; |
| 135 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 136 | static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) { |
| 137 | switch (segment_prefix) { |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 138 | case kCs: os << "cs:"; break; |
| 139 | case kSs: os << "ss:"; break; |
| 140 | case kDs: os << "ds:"; break; |
| 141 | case kEs: os << "es:"; break; |
| 142 | case kFs: os << "fs:"; break; |
| 143 | case kGs: os << "gs:"; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 144 | default: break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) { |
| 149 | const uint8_t* begin_instr = instr; |
| 150 | bool have_prefixes = true; |
| 151 | uint8_t prefix[4] = {0, 0, 0, 0}; |
| 152 | const char** modrm_opcodes = NULL; |
| 153 | do { |
| 154 | switch (*instr) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 155 | // Group 1 - lock and repeat prefixes: |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 156 | case 0xF0: |
| 157 | case 0xF2: |
| 158 | case 0xF3: |
| 159 | prefix[0] = *instr; |
| 160 | break; |
| 161 | // Group 2 - segment override prefixes: |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 162 | case kCs: |
| 163 | case kSs: |
| 164 | case kDs: |
| 165 | case kEs: |
| 166 | case kFs: |
| 167 | case kGs: |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 168 | prefix[1] = *instr; |
| 169 | break; |
| 170 | // Group 3 - operand size override: |
| 171 | case 0x66: |
| 172 | prefix[2] = *instr; |
| 173 | break; |
| 174 | // Group 4 - address size override: |
| 175 | case 0x67: |
| 176 | prefix[3] = *instr; |
| 177 | break; |
| 178 | default: |
| 179 | have_prefixes = false; |
| 180 | break; |
| 181 | } |
| 182 | if (have_prefixes) { |
| 183 | instr++; |
| 184 | } |
| 185 | } while (have_prefixes); |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 186 | uint8_t rex = (supports_rex_ && (*instr >= 0x40) && (*instr <= 0x4F)) ? *instr : 0; |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 187 | if (rex != 0) { |
| 188 | instr++; |
| 189 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 190 | bool has_modrm = false; |
| 191 | bool reg_is_opcode = false; |
| 192 | size_t immediate_bytes = 0; |
| 193 | size_t branch_bytes = 0; |
| 194 | std::ostringstream opcode; |
| 195 | bool store = false; // stores to memory (ie rm is on the left) |
| 196 | bool load = false; // loads from memory (ie rm is on the right) |
| 197 | bool byte_operand = false; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 198 | bool target_specific = false; // register name depends on target (64 vs 32 bits). |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 199 | bool ax = false; // implicit use of ax |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 200 | bool cx = false; // implicit use of cx |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 201 | bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 202 | bool no_ops = false; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 203 | RegFile src_reg_file = GPR; |
| 204 | RegFile dst_reg_file = GPR; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 205 | switch (*instr) { |
| 206 | #define DISASSEMBLER_ENTRY(opname, \ |
| 207 | rm8_r8, rm32_r32, \ |
| 208 | r8_rm8, r32_rm32, \ |
| 209 | ax8_i8, ax32_i32) \ |
| 210 | case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \ |
| 211 | case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \ |
| 212 | case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \ |
| 213 | case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \ |
| 214 | case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \ |
| 215 | case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break; |
| 216 | |
| 217 | DISASSEMBLER_ENTRY(add, |
| 218 | 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */, |
| 219 | 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */, |
| 220 | 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */) |
| 221 | DISASSEMBLER_ENTRY(or, |
| 222 | 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */, |
| 223 | 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */, |
| 224 | 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */) |
| 225 | DISASSEMBLER_ENTRY(adc, |
| 226 | 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */, |
| 227 | 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */, |
| 228 | 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */) |
| 229 | DISASSEMBLER_ENTRY(sbb, |
| 230 | 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */, |
| 231 | 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */, |
| 232 | 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */) |
| 233 | DISASSEMBLER_ENTRY(and, |
| 234 | 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */, |
| 235 | 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */, |
| 236 | 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */) |
| 237 | DISASSEMBLER_ENTRY(sub, |
| 238 | 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */, |
| 239 | 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */, |
| 240 | 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */) |
| 241 | DISASSEMBLER_ENTRY(xor, |
| 242 | 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */, |
| 243 | 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */, |
| 244 | 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */) |
| 245 | DISASSEMBLER_ENTRY(cmp, |
| 246 | 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */, |
| 247 | 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */, |
| 248 | 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */) |
| 249 | |
| 250 | #undef DISASSEMBLER_ENTRY |
| 251 | case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: |
| 252 | opcode << "push"; |
| 253 | reg_in_opcode = true; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 254 | target_specific = true; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 255 | break; |
| 256 | case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F: |
| 257 | opcode << "pop"; |
| 258 | reg_in_opcode = true; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 259 | target_specific = true; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 260 | break; |
| 261 | case 0x68: opcode << "push"; immediate_bytes = 4; break; |
Mark Mendell | d19b55a | 2013-12-12 09:55:34 -0800 | [diff] [blame] | 262 | case 0x69: opcode << "imul"; load = true; has_modrm = true; immediate_bytes = 4; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 263 | case 0x6A: opcode << "push"; immediate_bytes = 1; break; |
Mark Mendell | d19b55a | 2013-12-12 09:55:34 -0800 | [diff] [blame] | 264 | case 0x6B: opcode << "imul"; load = true; has_modrm = true; immediate_bytes = 1; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 265 | case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: |
| 266 | case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F: |
| 267 | static const char* condition_codes[] = |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 268 | {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a", |
| 269 | "s", "ns", "p/pe", "np/po", "l/nge", "nl/ge", "le/ng", "nle/g" |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 270 | }; |
| 271 | opcode << "j" << condition_codes[*instr & 0xF]; |
| 272 | branch_bytes = 1; |
| 273 | break; |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 274 | case 0x86: case 0x87: |
| 275 | opcode << "xchg"; |
| 276 | store = true; |
| 277 | has_modrm = true; |
| 278 | byte_operand = (*instr == 0x86); |
| 279 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 280 | case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break; |
| 281 | case 0x89: opcode << "mov"; store = true; has_modrm = true; break; |
| 282 | case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break; |
| 283 | case 0x8B: opcode << "mov"; load = true; has_modrm = true; break; |
| 284 | |
| 285 | case 0x0F: // 2 byte extended opcode |
| 286 | instr++; |
| 287 | switch (*instr) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 288 | case 0x10: case 0x11: |
| 289 | if (prefix[0] == 0xF2) { |
| 290 | opcode << "movsd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 291 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 292 | } else if (prefix[0] == 0xF3) { |
| 293 | opcode << "movss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 294 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 295 | } else if (prefix[2] == 0x66) { |
| 296 | opcode << "movupd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 297 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 298 | } else { |
| 299 | opcode << "movups"; |
| 300 | } |
| 301 | has_modrm = true; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 302 | src_reg_file = dst_reg_file = SSE; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 303 | load = *instr == 0x10; |
| 304 | store = !load; |
| 305 | break; |
Razvan A Lupusoru | 2c498d1 | 2014-01-29 16:02:57 -0800 | [diff] [blame] | 306 | case 0x12: case 0x13: |
| 307 | if (prefix[2] == 0x66) { |
| 308 | opcode << "movlpd"; |
| 309 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 310 | } else if (prefix[0] == 0) { |
| 311 | opcode << "movlps"; |
| 312 | } |
| 313 | has_modrm = true; |
| 314 | src_reg_file = dst_reg_file = SSE; |
| 315 | load = *instr == 0x12; |
| 316 | store = !load; |
| 317 | break; |
| 318 | case 0x16: case 0x17: |
| 319 | if (prefix[2] == 0x66) { |
| 320 | opcode << "movhpd"; |
| 321 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 322 | } else if (prefix[0] == 0) { |
| 323 | opcode << "movhps"; |
| 324 | } |
| 325 | has_modrm = true; |
| 326 | src_reg_file = dst_reg_file = SSE; |
| 327 | load = *instr == 0x16; |
| 328 | store = !load; |
| 329 | break; |
| 330 | case 0x28: case 0x29: |
| 331 | if (prefix[2] == 0x66) { |
| 332 | opcode << "movapd"; |
| 333 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 334 | } else if (prefix[0] == 0) { |
| 335 | opcode << "movaps"; |
| 336 | } |
| 337 | has_modrm = true; |
| 338 | src_reg_file = dst_reg_file = SSE; |
| 339 | load = *instr == 0x28; |
| 340 | store = !load; |
| 341 | break; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 342 | case 0x2A: |
| 343 | if (prefix[2] == 0x66) { |
| 344 | opcode << "cvtpi2pd"; |
| 345 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 346 | } else if (prefix[0] == 0xF2) { |
| 347 | opcode << "cvtsi2sd"; |
| 348 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 349 | } else if (prefix[0] == 0xF3) { |
| 350 | opcode << "cvtsi2ss"; |
| 351 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 352 | } else { |
| 353 | opcode << "cvtpi2ps"; |
| 354 | } |
| 355 | load = true; |
| 356 | has_modrm = true; |
| 357 | dst_reg_file = SSE; |
| 358 | break; |
| 359 | case 0x2C: |
| 360 | if (prefix[2] == 0x66) { |
| 361 | opcode << "cvttpd2pi"; |
| 362 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 363 | } else if (prefix[0] == 0xF2) { |
| 364 | opcode << "cvttsd2si"; |
| 365 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 366 | } else if (prefix[0] == 0xF3) { |
| 367 | opcode << "cvttss2si"; |
| 368 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 369 | } else { |
| 370 | opcode << "cvttps2pi"; |
| 371 | } |
| 372 | load = true; |
| 373 | has_modrm = true; |
| 374 | src_reg_file = SSE; |
| 375 | break; |
| 376 | case 0x2D: |
| 377 | if (prefix[2] == 0x66) { |
| 378 | opcode << "cvtpd2pi"; |
| 379 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 380 | } else if (prefix[0] == 0xF2) { |
| 381 | opcode << "cvtsd2si"; |
| 382 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 383 | } else if (prefix[0] == 0xF3) { |
| 384 | opcode << "cvtss2si"; |
| 385 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 386 | } else { |
| 387 | opcode << "cvtps2pi"; |
| 388 | } |
| 389 | load = true; |
| 390 | has_modrm = true; |
| 391 | src_reg_file = SSE; |
| 392 | break; |
| 393 | case 0x2E: |
| 394 | opcode << "u"; |
| 395 | // FALLTHROUGH |
| 396 | case 0x2F: |
| 397 | if (prefix[2] == 0x66) { |
| 398 | opcode << "comisd"; |
| 399 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 400 | } else { |
| 401 | opcode << "comiss"; |
| 402 | } |
| 403 | has_modrm = true; |
| 404 | load = true; |
| 405 | src_reg_file = dst_reg_file = SSE; |
| 406 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 407 | case 0x38: // 3 byte extended opcode |
Mark Mendell | fe94578 | 2014-05-22 09:52:36 -0400 | [diff] [blame] | 408 | instr++; |
| 409 | if (prefix[2] == 0x66) { |
| 410 | switch (*instr) { |
| 411 | case 0x40: |
| 412 | opcode << "pmulld"; |
| 413 | prefix[2] = 0; |
| 414 | has_modrm = true; |
| 415 | load = true; |
| 416 | src_reg_file = dst_reg_file = SSE; |
| 417 | break; |
| 418 | default: |
| 419 | opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr); |
| 420 | } |
| 421 | } else { |
| 422 | opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr); |
| 423 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 424 | break; |
| 425 | case 0x3A: // 3 byte extended opcode |
Mark Mendell | fe94578 | 2014-05-22 09:52:36 -0400 | [diff] [blame] | 426 | instr++; |
| 427 | if (prefix[2] == 0x66) { |
| 428 | switch (*instr) { |
| 429 | case 0x14: |
| 430 | opcode << "pextrb"; |
| 431 | prefix[2] = 0; |
| 432 | has_modrm = true; |
| 433 | store = true; |
| 434 | dst_reg_file = SSE; |
| 435 | immediate_bytes = 1; |
| 436 | break; |
| 437 | case 0x16: |
| 438 | opcode << "pextrd"; |
| 439 | prefix[2] = 0; |
| 440 | has_modrm = true; |
| 441 | store = true; |
| 442 | dst_reg_file = SSE; |
| 443 | immediate_bytes = 1; |
| 444 | break; |
| 445 | default: |
| 446 | opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr); |
| 447 | } |
| 448 | } else { |
| 449 | opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr); |
| 450 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 451 | break; |
Razvan A Lupusoru | bd288c2 | 2013-12-20 17:27:23 -0800 | [diff] [blame] | 452 | case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47: |
| 453 | case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F: |
| 454 | opcode << "cmov" << condition_codes[*instr & 0xF]; |
| 455 | has_modrm = true; |
| 456 | load = true; |
| 457 | break; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 458 | case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: |
| 459 | case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: { |
| 460 | switch (*instr) { |
| 461 | case 0x50: opcode << "movmsk"; break; |
| 462 | case 0x51: opcode << "sqrt"; break; |
| 463 | case 0x52: opcode << "rsqrt"; break; |
| 464 | case 0x53: opcode << "rcp"; break; |
| 465 | case 0x54: opcode << "and"; break; |
| 466 | case 0x55: opcode << "andn"; break; |
| 467 | case 0x56: opcode << "or"; break; |
| 468 | case 0x57: opcode << "xor"; break; |
| 469 | case 0x58: opcode << "add"; break; |
| 470 | case 0x59: opcode << "mul"; break; |
| 471 | case 0x5C: opcode << "sub"; break; |
| 472 | case 0x5D: opcode << "min"; break; |
| 473 | case 0x5E: opcode << "div"; break; |
| 474 | case 0x5F: opcode << "max"; break; |
| 475 | default: LOG(FATAL) << "Unreachable"; |
| 476 | } |
| 477 | if (prefix[2] == 0x66) { |
| 478 | opcode << "pd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 479 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 480 | } else if (prefix[0] == 0xF2) { |
| 481 | opcode << "sd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 482 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 483 | } else if (prefix[0] == 0xF3) { |
| 484 | opcode << "ss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 485 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 486 | } else { |
| 487 | opcode << "ps"; |
| 488 | } |
| 489 | load = true; |
| 490 | has_modrm = true; |
| 491 | src_reg_file = dst_reg_file = SSE; |
| 492 | break; |
| 493 | } |
| 494 | case 0x5A: |
| 495 | if (prefix[2] == 0x66) { |
| 496 | opcode << "cvtpd2ps"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 497 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 498 | } else if (prefix[0] == 0xF2) { |
| 499 | opcode << "cvtsd2ss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 500 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 501 | } else if (prefix[0] == 0xF3) { |
| 502 | opcode << "cvtss2sd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 503 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 504 | } else { |
| 505 | opcode << "cvtps2pd"; |
| 506 | } |
| 507 | load = true; |
| 508 | has_modrm = true; |
| 509 | src_reg_file = dst_reg_file = SSE; |
| 510 | break; |
| 511 | case 0x5B: |
| 512 | if (prefix[2] == 0x66) { |
| 513 | opcode << "cvtps2dq"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 514 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 515 | } else if (prefix[0] == 0xF2) { |
| 516 | opcode << "bad opcode F2 0F 5B"; |
| 517 | } else if (prefix[0] == 0xF3) { |
| 518 | opcode << "cvttps2dq"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 519 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 520 | } else { |
| 521 | opcode << "cvtdq2ps"; |
| 522 | } |
| 523 | load = true; |
| 524 | has_modrm = true; |
| 525 | src_reg_file = dst_reg_file = SSE; |
| 526 | break; |
Razvan A Lupusoru | d3266bc | 2014-01-24 12:55:31 -0800 | [diff] [blame] | 527 | case 0x62: |
| 528 | if (prefix[2] == 0x66) { |
| 529 | src_reg_file = dst_reg_file = SSE; |
| 530 | prefix[2] = 0; // Clear prefix now. It has served its purpose as part of the opcode. |
| 531 | } else { |
| 532 | src_reg_file = dst_reg_file = MMX; |
| 533 | } |
| 534 | opcode << "punpckldq"; |
| 535 | load = true; |
| 536 | has_modrm = true; |
| 537 | break; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 538 | case 0x6E: |
| 539 | if (prefix[2] == 0x66) { |
| 540 | dst_reg_file = SSE; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 541 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 542 | } else { |
| 543 | dst_reg_file = MMX; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 544 | } |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 545 | opcode << "movd"; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 546 | load = true; |
| 547 | has_modrm = true; |
| 548 | break; |
| 549 | case 0x6F: |
| 550 | if (prefix[2] == 0x66) { |
Mark Mendell | fe94578 | 2014-05-22 09:52:36 -0400 | [diff] [blame] | 551 | src_reg_file = dst_reg_file = SSE; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 552 | opcode << "movdqa"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 553 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 554 | } else if (prefix[0] == 0xF3) { |
Mark Mendell | fe94578 | 2014-05-22 09:52:36 -0400 | [diff] [blame] | 555 | src_reg_file = dst_reg_file = SSE; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 556 | opcode << "movdqu"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 557 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 558 | } else { |
| 559 | dst_reg_file = MMX; |
| 560 | opcode << "movq"; |
| 561 | } |
| 562 | load = true; |
| 563 | has_modrm = true; |
| 564 | break; |
Mark Mendell | fe94578 | 2014-05-22 09:52:36 -0400 | [diff] [blame] | 565 | case 0x70: |
| 566 | if (prefix[2] == 0x66) { |
| 567 | opcode << "pshufd"; |
| 568 | prefix[2] = 0; |
| 569 | has_modrm = true; |
| 570 | store = true; |
| 571 | src_reg_file = dst_reg_file = SSE; |
| 572 | immediate_bytes = 1; |
| 573 | } else if (prefix[0] == 0xF2) { |
| 574 | opcode << "pshuflw"; |
| 575 | prefix[0] = 0; |
| 576 | has_modrm = true; |
| 577 | store = true; |
| 578 | src_reg_file = dst_reg_file = SSE; |
| 579 | immediate_bytes = 1; |
| 580 | } else { |
| 581 | opcode << StringPrintf("unknown opcode '0F %02X'", *instr); |
| 582 | } |
| 583 | break; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 584 | case 0x71: |
| 585 | if (prefix[2] == 0x66) { |
| 586 | dst_reg_file = SSE; |
| 587 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 588 | } else { |
| 589 | dst_reg_file = MMX; |
| 590 | } |
| 591 | static const char* x71_opcodes[] = {"unknown-71", "unknown-71", "psrlw", "unknown-71", "psraw", "unknown-71", "psllw", "unknown-71"}; |
| 592 | modrm_opcodes = x71_opcodes; |
| 593 | reg_is_opcode = true; |
| 594 | has_modrm = true; |
| 595 | store = true; |
| 596 | immediate_bytes = 1; |
| 597 | break; |
| 598 | case 0x72: |
| 599 | if (prefix[2] == 0x66) { |
| 600 | dst_reg_file = SSE; |
| 601 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 602 | } else { |
| 603 | dst_reg_file = MMX; |
| 604 | } |
| 605 | static const char* x72_opcodes[] = {"unknown-72", "unknown-72", "psrld", "unknown-72", "psrad", "unknown-72", "pslld", "unknown-72"}; |
| 606 | modrm_opcodes = x72_opcodes; |
| 607 | reg_is_opcode = true; |
| 608 | has_modrm = true; |
| 609 | store = true; |
| 610 | immediate_bytes = 1; |
| 611 | break; |
| 612 | case 0x73: |
| 613 | if (prefix[2] == 0x66) { |
| 614 | dst_reg_file = SSE; |
| 615 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 616 | } else { |
| 617 | dst_reg_file = MMX; |
| 618 | } |
| 619 | static const char* x73_opcodes[] = {"unknown-73", "unknown-73", "psrlq", "unknown-73", "unknown-73", "unknown-73", "psllq", "unknown-73"}; |
| 620 | modrm_opcodes = x73_opcodes; |
| 621 | reg_is_opcode = true; |
| 622 | has_modrm = true; |
| 623 | store = true; |
| 624 | immediate_bytes = 1; |
| 625 | break; |
| 626 | case 0x7E: |
| 627 | if (prefix[2] == 0x66) { |
| 628 | src_reg_file = SSE; |
| 629 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 630 | } else { |
| 631 | src_reg_file = MMX; |
| 632 | } |
| 633 | opcode << "movd"; |
| 634 | has_modrm = true; |
| 635 | store = true; |
| 636 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 637 | case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87: |
| 638 | case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F: |
| 639 | opcode << "j" << condition_codes[*instr & 0xF]; |
| 640 | branch_bytes = 4; |
| 641 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 642 | case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97: |
| 643 | case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F: |
| 644 | opcode << "set" << condition_codes[*instr & 0xF]; |
| 645 | modrm_opcodes = NULL; |
| 646 | reg_is_opcode = true; |
| 647 | has_modrm = true; |
| 648 | store = true; |
| 649 | break; |
Mark Mendell | 4708dcd | 2014-01-22 09:05:18 -0800 | [diff] [blame] | 650 | case 0xA4: |
| 651 | opcode << "shld"; |
| 652 | has_modrm = true; |
| 653 | load = true; |
| 654 | immediate_bytes = 1; |
| 655 | break; |
| 656 | case 0xAC: |
| 657 | opcode << "shrd"; |
| 658 | has_modrm = true; |
| 659 | load = true; |
| 660 | immediate_bytes = 1; |
| 661 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 662 | case 0xAE: |
| 663 | if (prefix[0] == 0xF3) { |
Ian Rogers | 5e588b3 | 2013-02-21 15:05:09 -0800 | [diff] [blame] | 664 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 665 | static const char* xAE_opcodes[] = {"rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"}; |
| 666 | modrm_opcodes = xAE_opcodes; |
| 667 | reg_is_opcode = true; |
| 668 | has_modrm = true; |
| 669 | uint8_t reg_or_opcode = (instr[1] >> 3) & 7; |
| 670 | switch (reg_or_opcode) { |
| 671 | case 0: |
| 672 | prefix[1] = kFs; |
| 673 | load = true; |
| 674 | break; |
| 675 | case 1: |
| 676 | prefix[1] = kGs; |
| 677 | load = true; |
| 678 | break; |
| 679 | case 2: |
| 680 | prefix[1] = kFs; |
| 681 | store = true; |
| 682 | break; |
| 683 | case 3: |
| 684 | prefix[1] = kGs; |
| 685 | store = true; |
| 686 | break; |
| 687 | default: |
| 688 | load = true; |
| 689 | break; |
| 690 | } |
| 691 | } else { |
| 692 | static const char* xAE_opcodes[] = {"unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "lfence", "mfence", "sfence"}; |
| 693 | modrm_opcodes = xAE_opcodes; |
| 694 | reg_is_opcode = true; |
| 695 | has_modrm = true; |
| 696 | load = true; |
| 697 | no_ops = true; |
| 698 | } |
| 699 | break; |
Mark Mendell | f723f0c | 2013-12-11 17:50:58 -0800 | [diff] [blame] | 700 | case 0xAF: opcode << "imul"; has_modrm = true; load = true; break; |
jeffhao | 8302576 | 2012-08-02 11:08:56 -0700 | [diff] [blame] | 701 | case 0xB1: opcode << "cmpxchg"; has_modrm = true; store = true; break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 702 | case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break; |
| 703 | case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break; |
jeffhao | 854029c | 2012-07-23 17:31:30 -0700 | [diff] [blame] | 704 | case 0xBE: opcode << "movsxb"; has_modrm = true; load = true; break; |
| 705 | case 0xBF: opcode << "movsxw"; has_modrm = true; load = true; break; |
Mark Mendell | fe94578 | 2014-05-22 09:52:36 -0400 | [diff] [blame] | 706 | case 0xC5: |
| 707 | if (prefix[2] == 0x66) { |
| 708 | opcode << "pextrw"; |
| 709 | prefix[2] = 0; |
| 710 | has_modrm = true; |
| 711 | store = true; |
| 712 | src_reg_file = dst_reg_file = SSE; |
| 713 | immediate_bytes = 1; |
| 714 | } else { |
| 715 | opcode << StringPrintf("unknown opcode '0F %02X'", *instr); |
| 716 | } |
| 717 | break; |
Vladimir Marko | 70b797d | 2013-12-03 15:25:24 +0000 | [diff] [blame] | 718 | case 0xC7: |
| 719 | static const char* x0FxC7_opcodes[] = { "unknown-0f-c7", "cmpxchg8b", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7", "unknown-0f-c7" }; |
| 720 | modrm_opcodes = x0FxC7_opcodes; |
| 721 | has_modrm = true; |
| 722 | reg_is_opcode = true; |
| 723 | store = true; |
| 724 | break; |
Vladimir Marko | a8b4caf | 2013-10-24 15:08:57 +0100 | [diff] [blame] | 725 | case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF: |
| 726 | opcode << "bswap"; |
| 727 | reg_in_opcode = true; |
| 728 | break; |
Mark Mendell | fe94578 | 2014-05-22 09:52:36 -0400 | [diff] [blame] | 729 | case 0xDB: |
| 730 | if (prefix[2] == 0x66) { |
| 731 | src_reg_file = dst_reg_file = SSE; |
| 732 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 733 | } else { |
| 734 | src_reg_file = dst_reg_file = MMX; |
| 735 | } |
| 736 | opcode << "pand"; |
| 737 | prefix[2] = 0; |
| 738 | has_modrm = true; |
| 739 | load = true; |
| 740 | break; |
| 741 | case 0xD5: |
| 742 | if (prefix[2] == 0x66) { |
| 743 | opcode << "pmullw"; |
| 744 | prefix[2] = 0; |
| 745 | has_modrm = true; |
| 746 | load = true; |
| 747 | src_reg_file = dst_reg_file = SSE; |
| 748 | } else { |
| 749 | opcode << StringPrintf("unknown opcode '0F %02X'", *instr); |
| 750 | } |
| 751 | break; |
| 752 | case 0xEB: |
| 753 | if (prefix[2] == 0x66) { |
| 754 | src_reg_file = dst_reg_file = SSE; |
| 755 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 756 | } else { |
| 757 | src_reg_file = dst_reg_file = MMX; |
| 758 | } |
| 759 | opcode << "por"; |
| 760 | prefix[2] = 0; |
| 761 | has_modrm = true; |
| 762 | load = true; |
| 763 | break; |
| 764 | case 0xEF: |
| 765 | if (prefix[2] == 0x66) { |
| 766 | src_reg_file = dst_reg_file = SSE; |
| 767 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 768 | } else { |
| 769 | src_reg_file = dst_reg_file = MMX; |
| 770 | } |
| 771 | opcode << "pxor"; |
| 772 | prefix[2] = 0; |
| 773 | has_modrm = true; |
| 774 | load = true; |
| 775 | break; |
| 776 | case 0xF8: |
| 777 | if (prefix[2] == 0x66) { |
| 778 | src_reg_file = dst_reg_file = SSE; |
| 779 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 780 | } else { |
| 781 | src_reg_file = dst_reg_file = MMX; |
| 782 | } |
| 783 | opcode << "psubb"; |
| 784 | prefix[2] = 0; |
| 785 | has_modrm = true; |
| 786 | load = true; |
| 787 | break; |
| 788 | case 0xF9: |
| 789 | if (prefix[2] == 0x66) { |
| 790 | src_reg_file = dst_reg_file = SSE; |
| 791 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 792 | } else { |
| 793 | src_reg_file = dst_reg_file = MMX; |
| 794 | } |
| 795 | opcode << "psubw"; |
| 796 | prefix[2] = 0; |
| 797 | has_modrm = true; |
| 798 | load = true; |
| 799 | break; |
| 800 | case 0xFA: |
| 801 | if (prefix[2] == 0x66) { |
| 802 | src_reg_file = dst_reg_file = SSE; |
| 803 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 804 | } else { |
| 805 | src_reg_file = dst_reg_file = MMX; |
| 806 | } |
| 807 | opcode << "psubd"; |
| 808 | prefix[2] = 0; |
| 809 | has_modrm = true; |
| 810 | load = true; |
| 811 | break; |
| 812 | case 0xFC: |
| 813 | if (prefix[2] == 0x66) { |
| 814 | src_reg_file = dst_reg_file = SSE; |
| 815 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 816 | } else { |
| 817 | src_reg_file = dst_reg_file = MMX; |
| 818 | } |
| 819 | opcode << "paddb"; |
| 820 | prefix[2] = 0; |
| 821 | has_modrm = true; |
| 822 | load = true; |
| 823 | break; |
| 824 | case 0xFD: |
| 825 | if (prefix[2] == 0x66) { |
| 826 | src_reg_file = dst_reg_file = SSE; |
| 827 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 828 | } else { |
| 829 | src_reg_file = dst_reg_file = MMX; |
| 830 | } |
| 831 | opcode << "paddw"; |
| 832 | prefix[2] = 0; |
| 833 | has_modrm = true; |
| 834 | load = true; |
| 835 | break; |
| 836 | case 0xFE: |
| 837 | if (prefix[2] == 0x66) { |
| 838 | src_reg_file = dst_reg_file = SSE; |
| 839 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 840 | } else { |
| 841 | src_reg_file = dst_reg_file = MMX; |
| 842 | } |
| 843 | opcode << "paddd"; |
| 844 | prefix[2] = 0; |
| 845 | has_modrm = true; |
| 846 | load = true; |
| 847 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 848 | default: |
| 849 | opcode << StringPrintf("unknown opcode '0F %02X'", *instr); |
| 850 | break; |
| 851 | } |
| 852 | break; |
| 853 | case 0x80: case 0x81: case 0x82: case 0x83: |
| 854 | static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"}; |
| 855 | modrm_opcodes = x80_opcodes; |
| 856 | has_modrm = true; |
| 857 | reg_is_opcode = true; |
| 858 | store = true; |
| 859 | byte_operand = (*instr & 1) == 0; |
| 860 | immediate_bytes = *instr == 0x81 ? 4 : 1; |
| 861 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 862 | case 0x84: case 0x85: |
| 863 | opcode << "test"; |
| 864 | has_modrm = true; |
| 865 | load = true; |
| 866 | byte_operand = (*instr & 1) == 0; |
| 867 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 868 | case 0x8D: |
| 869 | opcode << "lea"; |
| 870 | has_modrm = true; |
| 871 | load = true; |
| 872 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 873 | case 0x8F: |
| 874 | opcode << "pop"; |
| 875 | has_modrm = true; |
| 876 | reg_is_opcode = true; |
| 877 | store = true; |
| 878 | break; |
Mark Mendell | 2bf31e6 | 2014-01-23 12:13:40 -0800 | [diff] [blame] | 879 | case 0x99: |
| 880 | opcode << "cdq"; |
| 881 | break; |
Mark Mendell | 4028a6c | 2014-02-19 20:06:20 -0800 | [diff] [blame] | 882 | case 0xAF: |
| 883 | opcode << (prefix[2] == 0x66 ? "scasw" : "scasl"); |
| 884 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 885 | case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7: |
| 886 | opcode << "mov"; |
| 887 | immediate_bytes = 1; |
| 888 | reg_in_opcode = true; |
| 889 | break; |
| 890 | case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF: |
| 891 | opcode << "mov"; |
| 892 | immediate_bytes = 4; |
| 893 | reg_in_opcode = true; |
| 894 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 895 | case 0xC0: case 0xC1: |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 896 | case 0xD0: case 0xD1: case 0xD2: case 0xD3: |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 897 | static const char* shift_opcodes[] = |
| 898 | {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"}; |
| 899 | modrm_opcodes = shift_opcodes; |
| 900 | has_modrm = true; |
| 901 | reg_is_opcode = true; |
| 902 | store = true; |
Elliott Hughes | 16b5c29 | 2012-04-16 20:37:16 -0700 | [diff] [blame] | 903 | immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0; |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 904 | cx = (*instr == 0xD2) || (*instr == 0xD3); |
| 905 | byte_operand = (*instr == 0xC0); |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 906 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 907 | case 0xC3: opcode << "ret"; break; |
Elliott Hughes | 0589ca9 | 2012-04-09 18:26:20 -0700 | [diff] [blame] | 908 | case 0xC7: |
| 909 | static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"}; |
| 910 | modrm_opcodes = c7_opcodes; |
| 911 | store = true; |
| 912 | immediate_bytes = 4; |
| 913 | has_modrm = true; |
| 914 | reg_is_opcode = true; |
| 915 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 916 | case 0xCC: opcode << "int 3"; break; |
Mark Mendell | d19b55a | 2013-12-12 09:55:34 -0800 | [diff] [blame] | 917 | case 0xD9: |
| 918 | static const char* d9_opcodes[] = {"flds", "unknown-d9", "fsts", "fstps", "fldenv", "fldcw", "fnstenv", "fnstcw"}; |
| 919 | modrm_opcodes = d9_opcodes; |
| 920 | store = true; |
| 921 | has_modrm = true; |
| 922 | reg_is_opcode = true; |
| 923 | break; |
Razvan A Lupusoru | 614c2b4 | 2014-01-28 17:05:21 -0800 | [diff] [blame] | 924 | case 0xDB: |
| 925 | static const char* db_opcodes[] = {"fildl", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db"}; |
| 926 | modrm_opcodes = db_opcodes; |
| 927 | load = true; |
| 928 | has_modrm = true; |
| 929 | reg_is_opcode = true; |
| 930 | break; |
Mark Mendell | d19b55a | 2013-12-12 09:55:34 -0800 | [diff] [blame] | 931 | case 0xDD: |
| 932 | static const char* dd_opcodes[] = {"fldl", "fisttp", "fstl", "fstpl", "frstor", "unknown-dd", "fnsave", "fnstsw"}; |
| 933 | modrm_opcodes = dd_opcodes; |
| 934 | store = true; |
| 935 | has_modrm = true; |
| 936 | reg_is_opcode = true; |
| 937 | break; |
Razvan A Lupusoru | 614c2b4 | 2014-01-28 17:05:21 -0800 | [diff] [blame] | 938 | case 0xDF: |
| 939 | static const char* df_opcodes[] = {"fild", "unknown-df", "unknown-df", "unknown-df", "unknown-df", "fildll", "unknown-df", "unknown-df"}; |
| 940 | modrm_opcodes = df_opcodes; |
| 941 | load = true; |
| 942 | has_modrm = true; |
| 943 | reg_is_opcode = true; |
| 944 | break; |
Mark Mendell | 4028a6c | 2014-02-19 20:06:20 -0800 | [diff] [blame] | 945 | case 0xE3: opcode << "jecxz"; branch_bytes = 1; break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 946 | case 0xE8: opcode << "call"; branch_bytes = 4; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 947 | case 0xE9: opcode << "jmp"; branch_bytes = 4; break; |
| 948 | case 0xEB: opcode << "jmp"; branch_bytes = 1; break; |
jeffhao | 77ae36b | 2012-08-07 14:18:16 -0700 | [diff] [blame] | 949 | case 0xF5: opcode << "cmc"; break; |
jeffhao | 174651d | 2012-04-19 15:27:22 -0700 | [diff] [blame] | 950 | case 0xF6: case 0xF7: |
| 951 | static const char* f7_opcodes[] = {"test", "unknown-f7", "not", "neg", "mul edx:eax, eax *", "imul edx:eax, eax *", "div edx:eax, edx:eax /", "idiv edx:eax, edx:eax /"}; |
| 952 | modrm_opcodes = f7_opcodes; |
| 953 | has_modrm = true; |
| 954 | reg_is_opcode = true; |
| 955 | store = true; |
| 956 | immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0; |
| 957 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 958 | case 0xFF: |
| 959 | static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"}; |
| 960 | modrm_opcodes = ff_opcodes; |
| 961 | has_modrm = true; |
| 962 | reg_is_opcode = true; |
| 963 | load = true; |
| 964 | break; |
| 965 | default: |
| 966 | opcode << StringPrintf("unknown opcode '%02X'", *instr); |
| 967 | break; |
| 968 | } |
| 969 | std::ostringstream args; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 970 | // We force the REX prefix to be available for 64-bit target |
| 971 | // in order to dump addr (base/index) registers correctly. |
| 972 | uint8_t rex64 = supports_rex_ ? (rex | 0x40) : rex; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 973 | if (reg_in_opcode) { |
| 974 | DCHECK(!has_modrm); |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 975 | // REX.W should be forced for 64-target and target-specific instructions (i.e., push or pop). |
| 976 | uint8_t rex_w = (supports_rex_ && target_specific) ? (rex | 0x48) : rex; |
| 977 | DumpOpcodeReg(args, rex_w, *instr & 0x7); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 978 | } |
| 979 | instr++; |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 980 | uint32_t address_bits = 0; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 981 | if (has_modrm) { |
| 982 | uint8_t modrm = *instr; |
| 983 | instr++; |
| 984 | uint8_t mod = modrm >> 6; |
| 985 | uint8_t reg_or_opcode = (modrm >> 3) & 7; |
| 986 | uint8_t rm = modrm & 7; |
| 987 | std::ostringstream address; |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 988 | if (mod == 0 && rm == 5) { |
| 989 | if (!supports_rex_) { // Absolute address. |
| 990 | address_bits = *reinterpret_cast<const uint32_t*>(instr); |
| 991 | address << StringPrintf("[0x%x]", address_bits); |
| 992 | } else { // 64-bit RIP relative addressing. |
| 993 | address << StringPrintf("[RIP + 0x%x]", *reinterpret_cast<const uint32_t*>(instr)); |
| 994 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 995 | instr += 4; |
| 996 | } else if (rm == 4 && mod != 3) { // SIB |
| 997 | uint8_t sib = *instr; |
| 998 | instr++; |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 999 | uint8_t scale = (sib >> 6) & 3; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1000 | uint8_t index = (sib >> 3) & 7; |
| 1001 | uint8_t base = sib & 7; |
| 1002 | address << "["; |
| 1003 | if (base != 5 || mod != 0) { |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 1004 | DumpBaseReg(address, rex64, base); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1005 | if (index != 4) { |
| 1006 | address << " + "; |
| 1007 | } |
| 1008 | } |
| 1009 | if (index != 4) { |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 1010 | DumpIndexReg(address, rex64, index); |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 1011 | if (scale != 0) { |
| 1012 | address << StringPrintf(" * %d", 1 << scale); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1013 | } |
| 1014 | } |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 1015 | if (mod == 0) { |
| 1016 | if (base == 5) { |
| 1017 | if (index != 4) { |
| 1018 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 1019 | } else { |
| 1020 | // 64-bit low 32-bit absolute address, redundant absolute address encoding on 32-bit. |
| 1021 | address_bits = *reinterpret_cast<const uint32_t*>(instr); |
| 1022 | address << StringPrintf("%d", address_bits); |
| 1023 | } |
| 1024 | instr += 4; |
| 1025 | } |
| 1026 | } else if (mod == 1) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1027 | address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr)); |
| 1028 | instr++; |
| 1029 | } else if (mod == 2) { |
| 1030 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 1031 | instr += 4; |
| 1032 | } |
| 1033 | address << "]"; |
| 1034 | } else { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 1035 | if (mod == 3) { |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 1036 | if (!no_ops) { |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 1037 | DumpRmReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file); |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 1038 | } |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 1039 | } else { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1040 | address << "["; |
Vladimir Kostyukov | 122113a | 2014-05-30 17:56:23 +0700 | [diff] [blame^] | 1041 | DumpBaseReg(address, rex64, rm); |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 1042 | if (mod == 1) { |
| 1043 | address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr)); |
| 1044 | instr++; |
| 1045 | } else if (mod == 2) { |
| 1046 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 1047 | instr += 4; |
| 1048 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1049 | address << "]"; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 1053 | if (reg_is_opcode && modrm_opcodes != NULL) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1054 | opcode << modrm_opcodes[reg_or_opcode]; |
| 1055 | } |
| 1056 | if (load) { |
| 1057 | if (!reg_is_opcode) { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 1058 | DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], dst_reg_file); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1059 | args << ", "; |
| 1060 | } |
| 1061 | DumpSegmentOverride(args, prefix[1]); |
| 1062 | args << address.str(); |
| 1063 | } else { |
| 1064 | DCHECK(store); |
| 1065 | DumpSegmentOverride(args, prefix[1]); |
| 1066 | args << address.str(); |
| 1067 | if (!reg_is_opcode) { |
| 1068 | args << ", "; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 1069 | DumpReg(args, rex, reg_or_opcode, byte_operand, prefix[2], src_reg_file); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | if (ax) { |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 1074 | // If this opcode implicitly uses ax, ax is always the first arg. |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 1075 | DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1076 | } |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 1077 | if (cx) { |
| 1078 | args << ", "; |
| 1079 | DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR); |
| 1080 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1081 | if (immediate_bytes > 0) { |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 1082 | if (has_modrm || reg_in_opcode || ax || cx) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1083 | args << ", "; |
| 1084 | } |
| 1085 | if (immediate_bytes == 1) { |
| 1086 | args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr)); |
| 1087 | instr++; |
| 1088 | } else { |
| 1089 | CHECK_EQ(immediate_bytes, 4u); |
Mark Mendell | 67d18be | 2014-05-30 15:05:09 -0400 | [diff] [blame] | 1090 | if (prefix[2] == 0x66) { // Operand size override from 32-bit to 16-bit. |
| 1091 | args << StringPrintf("%d", *reinterpret_cast<const int16_t*>(instr)); |
| 1092 | instr += 2; |
| 1093 | } else { |
| 1094 | args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr)); |
| 1095 | instr += 4; |
| 1096 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } else if (branch_bytes > 0) { |
| 1099 | DCHECK(!has_modrm); |
| 1100 | int32_t displacement; |
| 1101 | if (branch_bytes == 1) { |
| 1102 | displacement = *reinterpret_cast<const int8_t*>(instr); |
| 1103 | instr++; |
| 1104 | } else { |
| 1105 | CHECK_EQ(branch_bytes, 4u); |
| 1106 | displacement = *reinterpret_cast<const int32_t*>(instr); |
| 1107 | instr += 4; |
| 1108 | } |
Elliott Hughes | 14178a9 | 2012-04-16 17:24:51 -0700 | [diff] [blame] | 1109 | args << StringPrintf("%+d (%p)", displacement, instr + displacement); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1110 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1111 | if (prefix[1] == kFs && !supports_rex_) { |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 1112 | args << " ; "; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 1113 | Thread::DumpThreadOffset<4>(args, address_bits); |
| 1114 | } |
| 1115 | if (prefix[1] == kGs && supports_rex_) { |
| 1116 | args << " ; "; |
| 1117 | Thread::DumpThreadOffset<8>(args, address_bits); |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 1118 | } |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1119 | std::stringstream hex; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1120 | for (size_t i = 0; begin_instr + i < instr; ++i) { |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1121 | hex << StringPrintf("%02X", begin_instr[i]); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1122 | } |
Ian Rogers | 5e588b3 | 2013-02-21 15:05:09 -0800 | [diff] [blame] | 1123 | std::stringstream prefixed_opcode; |
| 1124 | switch (prefix[0]) { |
| 1125 | case 0xF0: prefixed_opcode << "lock "; break; |
| 1126 | case 0xF2: prefixed_opcode << "repne "; break; |
| 1127 | case 0xF3: prefixed_opcode << "repe "; break; |
| 1128 | case 0: break; |
| 1129 | default: LOG(FATAL) << "Unreachable"; |
| 1130 | } |
| 1131 | prefixed_opcode << opcode.str(); |
| 1132 | os << StringPrintf("%p: %22s \t%-7s ", begin_instr, hex.str().c_str(), |
| 1133 | prefixed_opcode.str().c_str()) |
| 1134 | << args.str() << '\n'; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1135 | return instr - begin_instr; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1136 | } // NOLINT(readability/fn_size) |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 1137 | |
| 1138 | } // namespace x86 |
| 1139 | } // namespace art |