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 | |
| 39 | static const char* gReg8Names[] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" }; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 40 | static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" }; |
| 41 | static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" }; |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 42 | static const char* gReg64Names[] = { |
| 43 | "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", |
| 44 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 45 | }; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 46 | |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 47 | static void DumpReg0(std::ostream& os, uint8_t rex, size_t reg, |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 48 | bool byte_operand, uint8_t size_override) { |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 49 | DCHECK_LT(reg, (rex == 0) ? 8u : 16u); |
| 50 | bool rex_w = (rex & 0b1000) != 0; |
| 51 | size_t size = byte_operand ? 1 : (size_override == 0x66 ? 2 : (rex_w ? 8 :4)); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 52 | switch (size) { |
| 53 | case 1: os << gReg8Names[reg]; break; |
| 54 | case 2: os << gReg16Names[reg]; break; |
| 55 | case 4: os << gReg32Names[reg]; break; |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 56 | case 8: os << gReg64Names[reg]; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 57 | default: LOG(FATAL) << "unexpected size " << size; |
| 58 | } |
| 59 | } |
| 60 | |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 61 | enum RegFile { GPR, MMX, SSE }; |
| 62 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 63 | static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg, |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 64 | bool byte_operand, uint8_t size_override, RegFile reg_file) { |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 65 | bool rex_r = (rex & 0b0100) != 0; |
| 66 | size_t reg_num = rex_r ? (reg + 8) : reg; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 67 | if (reg_file == GPR) { |
| 68 | DumpReg0(os, rex, reg_num, byte_operand, size_override); |
| 69 | } else if (reg_file == SSE) { |
| 70 | os << "xmm" << reg_num; |
| 71 | } else { |
| 72 | os << "mm" << reg_num; |
| 73 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 76 | static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) { |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 77 | bool rex_b = (rex & 0b0001) != 0; |
| 78 | size_t reg_num = rex_b ? (reg + 8) : reg; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 79 | DumpReg0(os, rex, reg_num, false, 0); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 82 | static void DumpIndexReg(std::ostream& os, uint8_t rex, uint8_t reg) { |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 83 | bool rex_x = (rex & 0b0010) != 0; |
| 84 | uint8_t reg_num = rex_x ? (reg + 8) : reg; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 85 | DumpReg0(os, rex, reg_num, false, 0); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 88 | enum SegmentPrefix { |
| 89 | kCs = 0x2e, |
| 90 | kSs = 0x36, |
| 91 | kDs = 0x3e, |
| 92 | kEs = 0x26, |
| 93 | kFs = 0x64, |
| 94 | kGs = 0x65, |
| 95 | }; |
| 96 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 97 | static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) { |
| 98 | switch (segment_prefix) { |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 99 | case kCs: os << "cs:"; break; |
| 100 | case kSs: os << "ss:"; break; |
| 101 | case kDs: os << "ds:"; break; |
| 102 | case kEs: os << "es:"; break; |
| 103 | case kFs: os << "fs:"; break; |
| 104 | case kGs: os << "gs:"; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 105 | default: break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) { |
| 110 | const uint8_t* begin_instr = instr; |
| 111 | bool have_prefixes = true; |
| 112 | uint8_t prefix[4] = {0, 0, 0, 0}; |
| 113 | const char** modrm_opcodes = NULL; |
| 114 | do { |
| 115 | switch (*instr) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 116 | // Group 1 - lock and repeat prefixes: |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 117 | case 0xF0: |
| 118 | case 0xF2: |
| 119 | case 0xF3: |
| 120 | prefix[0] = *instr; |
| 121 | break; |
| 122 | // Group 2 - segment override prefixes: |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 123 | case kCs: |
| 124 | case kSs: |
| 125 | case kDs: |
| 126 | case kEs: |
| 127 | case kFs: |
| 128 | case kGs: |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 129 | prefix[1] = *instr; |
| 130 | break; |
| 131 | // Group 3 - operand size override: |
| 132 | case 0x66: |
| 133 | prefix[2] = *instr; |
| 134 | break; |
| 135 | // Group 4 - address size override: |
| 136 | case 0x67: |
| 137 | prefix[3] = *instr; |
| 138 | break; |
| 139 | default: |
| 140 | have_prefixes = false; |
| 141 | break; |
| 142 | } |
| 143 | if (have_prefixes) { |
| 144 | instr++; |
| 145 | } |
| 146 | } while (have_prefixes); |
Ian Rogers | 38e1203 | 2014-03-14 14:06:14 -0700 | [diff] [blame] | 147 | uint8_t rex = (supports_rex_ && (*instr >= 0x40) && (*instr <= 0x4F)) ? *instr : 0; |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 148 | if (rex != 0) { |
| 149 | instr++; |
| 150 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 151 | bool has_modrm = false; |
| 152 | bool reg_is_opcode = false; |
| 153 | size_t immediate_bytes = 0; |
| 154 | size_t branch_bytes = 0; |
| 155 | std::ostringstream opcode; |
| 156 | bool store = false; // stores to memory (ie rm is on the left) |
| 157 | bool load = false; // loads from memory (ie rm is on the right) |
| 158 | bool byte_operand = false; |
| 159 | bool ax = false; // implicit use of ax |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 160 | bool cx = false; // implicit use of cx |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 161 | bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 162 | bool no_ops = false; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 163 | RegFile src_reg_file = GPR; |
| 164 | RegFile dst_reg_file = GPR; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 165 | switch (*instr) { |
| 166 | #define DISASSEMBLER_ENTRY(opname, \ |
| 167 | rm8_r8, rm32_r32, \ |
| 168 | r8_rm8, r32_rm32, \ |
| 169 | ax8_i8, ax32_i32) \ |
| 170 | case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \ |
| 171 | case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \ |
| 172 | case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \ |
| 173 | case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \ |
| 174 | case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \ |
| 175 | case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break; |
| 176 | |
| 177 | DISASSEMBLER_ENTRY(add, |
| 178 | 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */, |
| 179 | 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */, |
| 180 | 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */) |
| 181 | DISASSEMBLER_ENTRY(or, |
| 182 | 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */, |
| 183 | 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */, |
| 184 | 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */) |
| 185 | DISASSEMBLER_ENTRY(adc, |
| 186 | 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */, |
| 187 | 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */, |
| 188 | 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */) |
| 189 | DISASSEMBLER_ENTRY(sbb, |
| 190 | 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */, |
| 191 | 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */, |
| 192 | 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */) |
| 193 | DISASSEMBLER_ENTRY(and, |
| 194 | 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */, |
| 195 | 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */, |
| 196 | 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */) |
| 197 | DISASSEMBLER_ENTRY(sub, |
| 198 | 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */, |
| 199 | 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */, |
| 200 | 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */) |
| 201 | DISASSEMBLER_ENTRY(xor, |
| 202 | 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */, |
| 203 | 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */, |
| 204 | 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */) |
| 205 | DISASSEMBLER_ENTRY(cmp, |
| 206 | 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */, |
| 207 | 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */, |
| 208 | 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */) |
| 209 | |
| 210 | #undef DISASSEMBLER_ENTRY |
| 211 | case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: |
| 212 | opcode << "push"; |
| 213 | reg_in_opcode = true; |
| 214 | break; |
| 215 | case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F: |
| 216 | opcode << "pop"; |
| 217 | reg_in_opcode = true; |
| 218 | break; |
| 219 | case 0x68: opcode << "push"; immediate_bytes = 4; break; |
Mark Mendell | d19b55a | 2013-12-12 09:55:34 -0800 | [diff] [blame] | 220 | 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] | 221 | case 0x6A: opcode << "push"; immediate_bytes = 1; break; |
Mark Mendell | d19b55a | 2013-12-12 09:55:34 -0800 | [diff] [blame] | 222 | 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] | 223 | case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: |
| 224 | case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F: |
| 225 | static const char* condition_codes[] = |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 226 | {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a", |
| 227 | "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] | 228 | }; |
| 229 | opcode << "j" << condition_codes[*instr & 0xF]; |
| 230 | branch_bytes = 1; |
| 231 | break; |
Razvan A Lupusoru | 99ad723 | 2014-02-25 17:41:08 -0800 | [diff] [blame] | 232 | case 0x86: case 0x87: |
| 233 | opcode << "xchg"; |
| 234 | store = true; |
| 235 | has_modrm = true; |
| 236 | byte_operand = (*instr == 0x86); |
| 237 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 238 | case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break; |
| 239 | case 0x89: opcode << "mov"; store = true; has_modrm = true; break; |
| 240 | case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break; |
| 241 | case 0x8B: opcode << "mov"; load = true; has_modrm = true; break; |
| 242 | |
| 243 | case 0x0F: // 2 byte extended opcode |
| 244 | instr++; |
| 245 | switch (*instr) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 246 | case 0x10: case 0x11: |
| 247 | if (prefix[0] == 0xF2) { |
| 248 | opcode << "movsd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 249 | 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] | 250 | } else if (prefix[0] == 0xF3) { |
| 251 | opcode << "movss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 252 | 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] | 253 | } else if (prefix[2] == 0x66) { |
| 254 | opcode << "movupd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 255 | 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] | 256 | } else { |
| 257 | opcode << "movups"; |
| 258 | } |
| 259 | has_modrm = true; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 260 | src_reg_file = dst_reg_file = SSE; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 261 | load = *instr == 0x10; |
| 262 | store = !load; |
| 263 | break; |
Razvan A Lupusoru | 2c498d1 | 2014-01-29 16:02:57 -0800 | [diff] [blame] | 264 | case 0x12: case 0x13: |
| 265 | if (prefix[2] == 0x66) { |
| 266 | opcode << "movlpd"; |
| 267 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 268 | } else if (prefix[0] == 0) { |
| 269 | opcode << "movlps"; |
| 270 | } |
| 271 | has_modrm = true; |
| 272 | src_reg_file = dst_reg_file = SSE; |
| 273 | load = *instr == 0x12; |
| 274 | store = !load; |
| 275 | break; |
| 276 | case 0x16: case 0x17: |
| 277 | if (prefix[2] == 0x66) { |
| 278 | opcode << "movhpd"; |
| 279 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 280 | } else if (prefix[0] == 0) { |
| 281 | opcode << "movhps"; |
| 282 | } |
| 283 | has_modrm = true; |
| 284 | src_reg_file = dst_reg_file = SSE; |
| 285 | load = *instr == 0x16; |
| 286 | store = !load; |
| 287 | break; |
| 288 | case 0x28: case 0x29: |
| 289 | if (prefix[2] == 0x66) { |
| 290 | opcode << "movapd"; |
| 291 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 292 | } else if (prefix[0] == 0) { |
| 293 | opcode << "movaps"; |
| 294 | } |
| 295 | has_modrm = true; |
| 296 | src_reg_file = dst_reg_file = SSE; |
| 297 | load = *instr == 0x28; |
| 298 | store = !load; |
| 299 | break; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 300 | case 0x2A: |
| 301 | if (prefix[2] == 0x66) { |
| 302 | opcode << "cvtpi2pd"; |
| 303 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 304 | } else if (prefix[0] == 0xF2) { |
| 305 | opcode << "cvtsi2sd"; |
| 306 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 307 | } else if (prefix[0] == 0xF3) { |
| 308 | opcode << "cvtsi2ss"; |
| 309 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 310 | } else { |
| 311 | opcode << "cvtpi2ps"; |
| 312 | } |
| 313 | load = true; |
| 314 | has_modrm = true; |
| 315 | dst_reg_file = SSE; |
| 316 | break; |
| 317 | case 0x2C: |
| 318 | if (prefix[2] == 0x66) { |
| 319 | opcode << "cvttpd2pi"; |
| 320 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 321 | } else if (prefix[0] == 0xF2) { |
| 322 | opcode << "cvttsd2si"; |
| 323 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 324 | } else if (prefix[0] == 0xF3) { |
| 325 | opcode << "cvttss2si"; |
| 326 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 327 | } else { |
| 328 | opcode << "cvttps2pi"; |
| 329 | } |
| 330 | load = true; |
| 331 | has_modrm = true; |
| 332 | src_reg_file = SSE; |
| 333 | break; |
| 334 | case 0x2D: |
| 335 | if (prefix[2] == 0x66) { |
| 336 | opcode << "cvtpd2pi"; |
| 337 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 338 | } else if (prefix[0] == 0xF2) { |
| 339 | opcode << "cvtsd2si"; |
| 340 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 341 | } else if (prefix[0] == 0xF3) { |
| 342 | opcode << "cvtss2si"; |
| 343 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 344 | } else { |
| 345 | opcode << "cvtps2pi"; |
| 346 | } |
| 347 | load = true; |
| 348 | has_modrm = true; |
| 349 | src_reg_file = SSE; |
| 350 | break; |
| 351 | case 0x2E: |
| 352 | opcode << "u"; |
| 353 | // FALLTHROUGH |
| 354 | case 0x2F: |
| 355 | if (prefix[2] == 0x66) { |
| 356 | opcode << "comisd"; |
| 357 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 358 | } else { |
| 359 | opcode << "comiss"; |
| 360 | } |
| 361 | has_modrm = true; |
| 362 | load = true; |
| 363 | src_reg_file = dst_reg_file = SSE; |
| 364 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 365 | case 0x38: // 3 byte extended opcode |
| 366 | opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr); |
| 367 | break; |
| 368 | case 0x3A: // 3 byte extended opcode |
| 369 | opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr); |
| 370 | break; |
Razvan A Lupusoru | bd288c2 | 2013-12-20 17:27:23 -0800 | [diff] [blame] | 371 | case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47: |
| 372 | case 0x48: case 0x49: case 0x4A: case 0x4B: case 0x4C: case 0x4D: case 0x4E: case 0x4F: |
| 373 | opcode << "cmov" << condition_codes[*instr & 0xF]; |
| 374 | has_modrm = true; |
| 375 | load = true; |
| 376 | break; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 377 | case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: |
| 378 | case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: { |
| 379 | switch (*instr) { |
| 380 | case 0x50: opcode << "movmsk"; break; |
| 381 | case 0x51: opcode << "sqrt"; break; |
| 382 | case 0x52: opcode << "rsqrt"; break; |
| 383 | case 0x53: opcode << "rcp"; break; |
| 384 | case 0x54: opcode << "and"; break; |
| 385 | case 0x55: opcode << "andn"; break; |
| 386 | case 0x56: opcode << "or"; break; |
| 387 | case 0x57: opcode << "xor"; break; |
| 388 | case 0x58: opcode << "add"; break; |
| 389 | case 0x59: opcode << "mul"; break; |
| 390 | case 0x5C: opcode << "sub"; break; |
| 391 | case 0x5D: opcode << "min"; break; |
| 392 | case 0x5E: opcode << "div"; break; |
| 393 | case 0x5F: opcode << "max"; break; |
| 394 | default: LOG(FATAL) << "Unreachable"; |
| 395 | } |
| 396 | if (prefix[2] == 0x66) { |
| 397 | opcode << "pd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 398 | 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] | 399 | } else if (prefix[0] == 0xF2) { |
| 400 | opcode << "sd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 401 | 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] | 402 | } else if (prefix[0] == 0xF3) { |
| 403 | opcode << "ss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 404 | 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] | 405 | } else { |
| 406 | opcode << "ps"; |
| 407 | } |
| 408 | load = true; |
| 409 | has_modrm = true; |
| 410 | src_reg_file = dst_reg_file = SSE; |
| 411 | break; |
| 412 | } |
| 413 | case 0x5A: |
| 414 | if (prefix[2] == 0x66) { |
| 415 | opcode << "cvtpd2ps"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 416 | 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] | 417 | } else if (prefix[0] == 0xF2) { |
| 418 | opcode << "cvtsd2ss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 419 | 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] | 420 | } else if (prefix[0] == 0xF3) { |
| 421 | opcode << "cvtss2sd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 422 | 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] | 423 | } else { |
| 424 | opcode << "cvtps2pd"; |
| 425 | } |
| 426 | load = true; |
| 427 | has_modrm = true; |
| 428 | src_reg_file = dst_reg_file = SSE; |
| 429 | break; |
| 430 | case 0x5B: |
| 431 | if (prefix[2] == 0x66) { |
| 432 | opcode << "cvtps2dq"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 433 | 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] | 434 | } else if (prefix[0] == 0xF2) { |
| 435 | opcode << "bad opcode F2 0F 5B"; |
| 436 | } else if (prefix[0] == 0xF3) { |
| 437 | opcode << "cvttps2dq"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 438 | 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] | 439 | } else { |
| 440 | opcode << "cvtdq2ps"; |
| 441 | } |
| 442 | load = true; |
| 443 | has_modrm = true; |
| 444 | src_reg_file = dst_reg_file = SSE; |
| 445 | break; |
Razvan A Lupusoru | d3266bc | 2014-01-24 12:55:31 -0800 | [diff] [blame] | 446 | case 0x62: |
| 447 | if (prefix[2] == 0x66) { |
| 448 | src_reg_file = dst_reg_file = SSE; |
| 449 | prefix[2] = 0; // Clear prefix now. It has served its purpose as part of the opcode. |
| 450 | } else { |
| 451 | src_reg_file = dst_reg_file = MMX; |
| 452 | } |
| 453 | opcode << "punpckldq"; |
| 454 | load = true; |
| 455 | has_modrm = true; |
| 456 | break; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 457 | case 0x6E: |
| 458 | if (prefix[2] == 0x66) { |
| 459 | dst_reg_file = SSE; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 460 | 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] | 461 | } else { |
| 462 | dst_reg_file = MMX; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 463 | } |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 464 | opcode << "movd"; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 465 | load = true; |
| 466 | has_modrm = true; |
| 467 | break; |
| 468 | case 0x6F: |
| 469 | if (prefix[2] == 0x66) { |
| 470 | dst_reg_file = SSE; |
| 471 | opcode << "movdqa"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 472 | 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] | 473 | } else if (prefix[0] == 0xF3) { |
| 474 | dst_reg_file = SSE; |
| 475 | opcode << "movdqu"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 476 | 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] | 477 | } else { |
| 478 | dst_reg_file = MMX; |
| 479 | opcode << "movq"; |
| 480 | } |
| 481 | load = true; |
| 482 | has_modrm = true; |
| 483 | break; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 484 | case 0x71: |
| 485 | if (prefix[2] == 0x66) { |
| 486 | dst_reg_file = SSE; |
| 487 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 488 | } else { |
| 489 | dst_reg_file = MMX; |
| 490 | } |
| 491 | static const char* x71_opcodes[] = {"unknown-71", "unknown-71", "psrlw", "unknown-71", "psraw", "unknown-71", "psllw", "unknown-71"}; |
| 492 | modrm_opcodes = x71_opcodes; |
| 493 | reg_is_opcode = true; |
| 494 | has_modrm = true; |
| 495 | store = true; |
| 496 | immediate_bytes = 1; |
| 497 | break; |
| 498 | case 0x72: |
| 499 | if (prefix[2] == 0x66) { |
| 500 | dst_reg_file = SSE; |
| 501 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 502 | } else { |
| 503 | dst_reg_file = MMX; |
| 504 | } |
| 505 | static const char* x72_opcodes[] = {"unknown-72", "unknown-72", "psrld", "unknown-72", "psrad", "unknown-72", "pslld", "unknown-72"}; |
| 506 | modrm_opcodes = x72_opcodes; |
| 507 | reg_is_opcode = true; |
| 508 | has_modrm = true; |
| 509 | store = true; |
| 510 | immediate_bytes = 1; |
| 511 | break; |
| 512 | case 0x73: |
| 513 | if (prefix[2] == 0x66) { |
| 514 | dst_reg_file = SSE; |
| 515 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 516 | } else { |
| 517 | dst_reg_file = MMX; |
| 518 | } |
| 519 | static const char* x73_opcodes[] = {"unknown-73", "unknown-73", "psrlq", "unknown-73", "unknown-73", "unknown-73", "psllq", "unknown-73"}; |
| 520 | modrm_opcodes = x73_opcodes; |
| 521 | reg_is_opcode = true; |
| 522 | has_modrm = true; |
| 523 | store = true; |
| 524 | immediate_bytes = 1; |
| 525 | break; |
| 526 | case 0x7E: |
| 527 | if (prefix[2] == 0x66) { |
| 528 | src_reg_file = SSE; |
| 529 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 530 | } else { |
| 531 | src_reg_file = MMX; |
| 532 | } |
| 533 | opcode << "movd"; |
| 534 | has_modrm = true; |
| 535 | store = true; |
| 536 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 537 | case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87: |
| 538 | case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F: |
| 539 | opcode << "j" << condition_codes[*instr & 0xF]; |
| 540 | branch_bytes = 4; |
| 541 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 542 | case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97: |
| 543 | case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F: |
| 544 | opcode << "set" << condition_codes[*instr & 0xF]; |
| 545 | modrm_opcodes = NULL; |
| 546 | reg_is_opcode = true; |
| 547 | has_modrm = true; |
| 548 | store = true; |
| 549 | break; |
Mark Mendell | 4708dcd | 2014-01-22 09:05:18 -0800 | [diff] [blame] | 550 | case 0xA4: |
| 551 | opcode << "shld"; |
| 552 | has_modrm = true; |
| 553 | load = true; |
| 554 | immediate_bytes = 1; |
| 555 | break; |
| 556 | case 0xAC: |
| 557 | opcode << "shrd"; |
| 558 | has_modrm = true; |
| 559 | load = true; |
| 560 | immediate_bytes = 1; |
| 561 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 562 | case 0xAE: |
| 563 | if (prefix[0] == 0xF3) { |
Ian Rogers | 5e588b3 | 2013-02-21 15:05:09 -0800 | [diff] [blame] | 564 | 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] | 565 | static const char* xAE_opcodes[] = {"rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"}; |
| 566 | modrm_opcodes = xAE_opcodes; |
| 567 | reg_is_opcode = true; |
| 568 | has_modrm = true; |
| 569 | uint8_t reg_or_opcode = (instr[1] >> 3) & 7; |
| 570 | switch (reg_or_opcode) { |
| 571 | case 0: |
| 572 | prefix[1] = kFs; |
| 573 | load = true; |
| 574 | break; |
| 575 | case 1: |
| 576 | prefix[1] = kGs; |
| 577 | load = true; |
| 578 | break; |
| 579 | case 2: |
| 580 | prefix[1] = kFs; |
| 581 | store = true; |
| 582 | break; |
| 583 | case 3: |
| 584 | prefix[1] = kGs; |
| 585 | store = true; |
| 586 | break; |
| 587 | default: |
| 588 | load = true; |
| 589 | break; |
| 590 | } |
| 591 | } else { |
| 592 | static const char* xAE_opcodes[] = {"unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "lfence", "mfence", "sfence"}; |
| 593 | modrm_opcodes = xAE_opcodes; |
| 594 | reg_is_opcode = true; |
| 595 | has_modrm = true; |
| 596 | load = true; |
| 597 | no_ops = true; |
| 598 | } |
| 599 | break; |
Mark Mendell | f723f0c | 2013-12-11 17:50:58 -0800 | [diff] [blame] | 600 | case 0xAF: opcode << "imul"; has_modrm = true; load = true; break; |
jeffhao | 8302576 | 2012-08-02 11:08:56 -0700 | [diff] [blame] | 601 | case 0xB1: opcode << "cmpxchg"; has_modrm = true; store = true; break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 602 | case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break; |
| 603 | case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break; |
jeffhao | 854029c | 2012-07-23 17:31:30 -0700 | [diff] [blame] | 604 | case 0xBE: opcode << "movsxb"; has_modrm = true; load = true; break; |
| 605 | case 0xBF: opcode << "movsxw"; has_modrm = true; load = true; break; |
Vladimir Marko | 70b797d | 2013-12-03 15:25:24 +0000 | [diff] [blame] | 606 | case 0xC7: |
| 607 | 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" }; |
| 608 | modrm_opcodes = x0FxC7_opcodes; |
| 609 | has_modrm = true; |
| 610 | reg_is_opcode = true; |
| 611 | store = true; |
| 612 | break; |
Vladimir Marko | a8b4caf | 2013-10-24 15:08:57 +0100 | [diff] [blame] | 613 | case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF: |
| 614 | opcode << "bswap"; |
| 615 | reg_in_opcode = true; |
| 616 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 617 | default: |
| 618 | opcode << StringPrintf("unknown opcode '0F %02X'", *instr); |
| 619 | break; |
| 620 | } |
| 621 | break; |
| 622 | case 0x80: case 0x81: case 0x82: case 0x83: |
| 623 | static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"}; |
| 624 | modrm_opcodes = x80_opcodes; |
| 625 | has_modrm = true; |
| 626 | reg_is_opcode = true; |
| 627 | store = true; |
| 628 | byte_operand = (*instr & 1) == 0; |
| 629 | immediate_bytes = *instr == 0x81 ? 4 : 1; |
| 630 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 631 | case 0x84: case 0x85: |
| 632 | opcode << "test"; |
| 633 | has_modrm = true; |
| 634 | load = true; |
| 635 | byte_operand = (*instr & 1) == 0; |
| 636 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 637 | case 0x8D: |
| 638 | opcode << "lea"; |
| 639 | has_modrm = true; |
| 640 | load = true; |
| 641 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 642 | case 0x8F: |
| 643 | opcode << "pop"; |
| 644 | has_modrm = true; |
| 645 | reg_is_opcode = true; |
| 646 | store = true; |
| 647 | break; |
Mark Mendell | 2bf31e6 | 2014-01-23 12:13:40 -0800 | [diff] [blame] | 648 | case 0x99: |
| 649 | opcode << "cdq"; |
| 650 | break; |
Mark Mendell | 4028a6c | 2014-02-19 20:06:20 -0800 | [diff] [blame] | 651 | case 0xAF: |
| 652 | opcode << (prefix[2] == 0x66 ? "scasw" : "scasl"); |
| 653 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 654 | case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7: |
| 655 | opcode << "mov"; |
| 656 | immediate_bytes = 1; |
| 657 | reg_in_opcode = true; |
| 658 | break; |
| 659 | case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF: |
| 660 | opcode << "mov"; |
| 661 | immediate_bytes = 4; |
| 662 | reg_in_opcode = true; |
| 663 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 664 | case 0xC0: case 0xC1: |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 665 | case 0xD0: case 0xD1: case 0xD2: case 0xD3: |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 666 | static const char* shift_opcodes[] = |
| 667 | {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"}; |
| 668 | modrm_opcodes = shift_opcodes; |
| 669 | has_modrm = true; |
| 670 | reg_is_opcode = true; |
| 671 | store = true; |
Elliott Hughes | 16b5c29 | 2012-04-16 20:37:16 -0700 | [diff] [blame] | 672 | immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0; |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 673 | cx = (*instr == 0xD2) || (*instr == 0xD3); |
| 674 | byte_operand = (*instr == 0xC0); |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 675 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 676 | case 0xC3: opcode << "ret"; break; |
Elliott Hughes | 0589ca9 | 2012-04-09 18:26:20 -0700 | [diff] [blame] | 677 | case 0xC7: |
| 678 | static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"}; |
| 679 | modrm_opcodes = c7_opcodes; |
| 680 | store = true; |
| 681 | immediate_bytes = 4; |
| 682 | has_modrm = true; |
| 683 | reg_is_opcode = true; |
| 684 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 685 | case 0xCC: opcode << "int 3"; break; |
Mark Mendell | d19b55a | 2013-12-12 09:55:34 -0800 | [diff] [blame] | 686 | case 0xD9: |
| 687 | static const char* d9_opcodes[] = {"flds", "unknown-d9", "fsts", "fstps", "fldenv", "fldcw", "fnstenv", "fnstcw"}; |
| 688 | modrm_opcodes = d9_opcodes; |
| 689 | store = true; |
| 690 | has_modrm = true; |
| 691 | reg_is_opcode = true; |
| 692 | break; |
Razvan A Lupusoru | 614c2b4 | 2014-01-28 17:05:21 -0800 | [diff] [blame] | 693 | case 0xDB: |
| 694 | static const char* db_opcodes[] = {"fildl", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db", "unknown-db"}; |
| 695 | modrm_opcodes = db_opcodes; |
| 696 | load = true; |
| 697 | has_modrm = true; |
| 698 | reg_is_opcode = true; |
| 699 | break; |
Mark Mendell | d19b55a | 2013-12-12 09:55:34 -0800 | [diff] [blame] | 700 | case 0xDD: |
| 701 | static const char* dd_opcodes[] = {"fldl", "fisttp", "fstl", "fstpl", "frstor", "unknown-dd", "fnsave", "fnstsw"}; |
| 702 | modrm_opcodes = dd_opcodes; |
| 703 | store = true; |
| 704 | has_modrm = true; |
| 705 | reg_is_opcode = true; |
| 706 | break; |
Razvan A Lupusoru | 614c2b4 | 2014-01-28 17:05:21 -0800 | [diff] [blame] | 707 | case 0xDF: |
| 708 | static const char* df_opcodes[] = {"fild", "unknown-df", "unknown-df", "unknown-df", "unknown-df", "fildll", "unknown-df", "unknown-df"}; |
| 709 | modrm_opcodes = df_opcodes; |
| 710 | load = true; |
| 711 | has_modrm = true; |
| 712 | reg_is_opcode = true; |
| 713 | break; |
Mark Mendell | 4028a6c | 2014-02-19 20:06:20 -0800 | [diff] [blame] | 714 | case 0xE3: opcode << "jecxz"; branch_bytes = 1; break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 715 | case 0xE8: opcode << "call"; branch_bytes = 4; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 716 | case 0xE9: opcode << "jmp"; branch_bytes = 4; break; |
| 717 | case 0xEB: opcode << "jmp"; branch_bytes = 1; break; |
jeffhao | 77ae36b | 2012-08-07 14:18:16 -0700 | [diff] [blame] | 718 | case 0xF5: opcode << "cmc"; break; |
jeffhao | 174651d | 2012-04-19 15:27:22 -0700 | [diff] [blame] | 719 | case 0xF6: case 0xF7: |
| 720 | 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 /"}; |
| 721 | modrm_opcodes = f7_opcodes; |
| 722 | has_modrm = true; |
| 723 | reg_is_opcode = true; |
| 724 | store = true; |
| 725 | immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0; |
| 726 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 727 | case 0xFF: |
| 728 | static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"}; |
| 729 | modrm_opcodes = ff_opcodes; |
| 730 | has_modrm = true; |
| 731 | reg_is_opcode = true; |
| 732 | load = true; |
| 733 | break; |
| 734 | default: |
| 735 | opcode << StringPrintf("unknown opcode '%02X'", *instr); |
| 736 | break; |
| 737 | } |
| 738 | std::ostringstream args; |
| 739 | if (reg_in_opcode) { |
| 740 | DCHECK(!has_modrm); |
Vladimir Kostyukov | fba52f1 | 2014-04-15 15:41:47 +0700 | [diff] [blame] | 741 | DumpBaseReg(args, rex, *instr & 0x7); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 742 | } |
| 743 | instr++; |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 744 | uint32_t address_bits = 0; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 745 | if (has_modrm) { |
| 746 | uint8_t modrm = *instr; |
| 747 | instr++; |
| 748 | uint8_t mod = modrm >> 6; |
| 749 | uint8_t reg_or_opcode = (modrm >> 3) & 7; |
| 750 | uint8_t rm = modrm & 7; |
| 751 | std::ostringstream address; |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 752 | if (mod == 0 && rm == 5) { |
| 753 | if (!supports_rex_) { // Absolute address. |
| 754 | address_bits = *reinterpret_cast<const uint32_t*>(instr); |
| 755 | address << StringPrintf("[0x%x]", address_bits); |
| 756 | } else { // 64-bit RIP relative addressing. |
| 757 | address << StringPrintf("[RIP + 0x%x]", *reinterpret_cast<const uint32_t*>(instr)); |
| 758 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 759 | instr += 4; |
| 760 | } else if (rm == 4 && mod != 3) { // SIB |
| 761 | uint8_t sib = *instr; |
| 762 | instr++; |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 763 | uint8_t scale = (sib >> 6) & 3; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 764 | uint8_t index = (sib >> 3) & 7; |
| 765 | uint8_t base = sib & 7; |
| 766 | address << "["; |
| 767 | if (base != 5 || mod != 0) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 768 | DumpBaseReg(address, rex, base); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 769 | if (index != 4) { |
| 770 | address << " + "; |
| 771 | } |
| 772 | } |
| 773 | if (index != 4) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 774 | DumpIndexReg(address, rex, index); |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 775 | if (scale != 0) { |
| 776 | address << StringPrintf(" * %d", 1 << scale); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
Vladimir Kostyukov | e8861b3 | 2014-04-18 17:06:15 +0700 | [diff] [blame] | 779 | if (mod == 0) { |
| 780 | if (base == 5) { |
| 781 | if (index != 4) { |
| 782 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 783 | } else { |
| 784 | // 64-bit low 32-bit absolute address, redundant absolute address encoding on 32-bit. |
| 785 | address_bits = *reinterpret_cast<const uint32_t*>(instr); |
| 786 | address << StringPrintf("%d", address_bits); |
| 787 | } |
| 788 | instr += 4; |
| 789 | } |
| 790 | } else if (mod == 1) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 791 | address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr)); |
| 792 | instr++; |
| 793 | } else if (mod == 2) { |
| 794 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 795 | instr += 4; |
| 796 | } |
| 797 | address << "]"; |
| 798 | } else { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 799 | if (mod == 3) { |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 800 | if (!no_ops) { |
| 801 | DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file); |
| 802 | } |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 803 | } else { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 804 | address << "["; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 805 | DumpBaseReg(address, rex, rm); |
| 806 | if (mod == 1) { |
| 807 | address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr)); |
| 808 | instr++; |
| 809 | } else if (mod == 2) { |
| 810 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 811 | instr += 4; |
| 812 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 813 | address << "]"; |
| 814 | } |
| 815 | } |
| 816 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 817 | if (reg_is_opcode && modrm_opcodes != NULL) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 818 | opcode << modrm_opcodes[reg_or_opcode]; |
| 819 | } |
| 820 | if (load) { |
| 821 | if (!reg_is_opcode) { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 822 | 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] | 823 | args << ", "; |
| 824 | } |
| 825 | DumpSegmentOverride(args, prefix[1]); |
| 826 | args << address.str(); |
| 827 | } else { |
| 828 | DCHECK(store); |
| 829 | DumpSegmentOverride(args, prefix[1]); |
| 830 | args << address.str(); |
| 831 | if (!reg_is_opcode) { |
| 832 | args << ", "; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 833 | 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] | 834 | } |
| 835 | } |
| 836 | } |
| 837 | if (ax) { |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 838 | // If this opcode implicitly uses ax, ax is always the first arg. |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 839 | DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 840 | } |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 841 | if (cx) { |
| 842 | args << ", "; |
| 843 | DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR); |
| 844 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 845 | if (immediate_bytes > 0) { |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 846 | if (has_modrm || reg_in_opcode || ax || cx) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 847 | args << ", "; |
| 848 | } |
| 849 | if (immediate_bytes == 1) { |
| 850 | args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr)); |
| 851 | instr++; |
| 852 | } else { |
| 853 | CHECK_EQ(immediate_bytes, 4u); |
| 854 | args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr)); |
| 855 | instr += 4; |
| 856 | } |
| 857 | } else if (branch_bytes > 0) { |
| 858 | DCHECK(!has_modrm); |
| 859 | int32_t displacement; |
| 860 | if (branch_bytes == 1) { |
| 861 | displacement = *reinterpret_cast<const int8_t*>(instr); |
| 862 | instr++; |
| 863 | } else { |
| 864 | CHECK_EQ(branch_bytes, 4u); |
| 865 | displacement = *reinterpret_cast<const int32_t*>(instr); |
| 866 | instr += 4; |
| 867 | } |
Elliott Hughes | 14178a9 | 2012-04-16 17:24:51 -0700 | [diff] [blame] | 868 | args << StringPrintf("%+d (%p)", displacement, instr + displacement); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 869 | } |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 870 | if (prefix[1] == kFs && !supports_rex_) { |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 871 | args << " ; "; |
Ian Rogers | dd7624d | 2014-03-14 17:43:00 -0700 | [diff] [blame] | 872 | Thread::DumpThreadOffset<4>(args, address_bits); |
| 873 | } |
| 874 | if (prefix[1] == kGs && supports_rex_) { |
| 875 | args << " ; "; |
| 876 | Thread::DumpThreadOffset<8>(args, address_bits); |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 877 | } |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 878 | std::stringstream hex; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 879 | for (size_t i = 0; begin_instr + i < instr; ++i) { |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 880 | hex << StringPrintf("%02X", begin_instr[i]); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 881 | } |
Ian Rogers | 5e588b3 | 2013-02-21 15:05:09 -0800 | [diff] [blame] | 882 | std::stringstream prefixed_opcode; |
| 883 | switch (prefix[0]) { |
| 884 | case 0xF0: prefixed_opcode << "lock "; break; |
| 885 | case 0xF2: prefixed_opcode << "repne "; break; |
| 886 | case 0xF3: prefixed_opcode << "repe "; break; |
| 887 | case 0: break; |
| 888 | default: LOG(FATAL) << "Unreachable"; |
| 889 | } |
| 890 | prefixed_opcode << opcode.str(); |
| 891 | os << StringPrintf("%p: %22s \t%-7s ", begin_instr, hex.str().c_str(), |
| 892 | prefixed_opcode.str().c_str()) |
| 893 | << args.str() << '\n'; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 894 | return instr - begin_instr; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 895 | } // NOLINT(readability/fn_size) |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 896 | |
| 897 | } // namespace x86 |
| 898 | } // namespace art |