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 | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 21 | #include "logging.h" |
| 22 | #include "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 | |
| 28 | DisassemblerX86::DisassemblerX86() { |
| 29 | } |
| 30 | |
| 31 | void DisassemblerX86::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) { |
| 32 | size_t length = 0; |
| 33 | for (const uint8_t* cur = begin; cur < end; cur += length) { |
| 34 | length = DumpInstruction(os, cur); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | static const char* gReg8Names[] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" }; |
| 39 | static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "di", "si" }; |
| 40 | static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "edi", "esi" }; |
| 41 | |
| 42 | static void DumpReg0(std::ostream& os, uint8_t /*rex*/, size_t reg, |
| 43 | bool byte_operand, uint8_t size_override) { |
| 44 | DCHECK_LT(reg, 8u); |
| 45 | // TODO: combine rex into size |
| 46 | size_t size = byte_operand ? 1 : (size_override == 0x66 ? 2 : 4); |
| 47 | switch (size) { |
| 48 | case 1: os << gReg8Names[reg]; break; |
| 49 | case 2: os << gReg16Names[reg]; break; |
| 50 | case 4: os << gReg32Names[reg]; break; |
| 51 | default: LOG(FATAL) << "unexpected size " << size; |
| 52 | } |
| 53 | } |
| 54 | |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 55 | enum RegFile { GPR, MMX, SSE }; |
| 56 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 57 | static void DumpReg(std::ostream& os, uint8_t rex, uint8_t reg, |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 58 | bool byte_operand, uint8_t size_override, RegFile reg_file) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 59 | size_t reg_num = reg; // TODO: combine with REX.R on 64bit |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 60 | if (reg_file == GPR) { |
| 61 | DumpReg0(os, rex, reg_num, byte_operand, size_override); |
| 62 | } else if (reg_file == SSE) { |
| 63 | os << "xmm" << reg_num; |
| 64 | } else { |
| 65 | os << "mm" << reg_num; |
| 66 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 69 | static void DumpBaseReg(std::ostream& os, uint8_t rex, uint8_t reg) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 70 | size_t reg_num = reg; // TODO: combine with REX.B on 64bit |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 71 | DumpReg0(os, rex, reg_num, false, 0); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 74 | static void DumpIndexReg(std::ostream& os, uint8_t rex, uint8_t reg) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 75 | int reg_num = reg; // TODO: combine with REX.X on 64bit |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 76 | DumpReg0(os, rex, reg_num, false, 0); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 79 | enum SegmentPrefix { |
| 80 | kCs = 0x2e, |
| 81 | kSs = 0x36, |
| 82 | kDs = 0x3e, |
| 83 | kEs = 0x26, |
| 84 | kFs = 0x64, |
| 85 | kGs = 0x65, |
| 86 | }; |
| 87 | |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 88 | static void DumpSegmentOverride(std::ostream& os, uint8_t segment_prefix) { |
| 89 | switch (segment_prefix) { |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 90 | case kCs: os << "cs:"; break; |
| 91 | case kSs: os << "ss:"; break; |
| 92 | case kDs: os << "ds:"; break; |
| 93 | case kEs: os << "es:"; break; |
| 94 | case kFs: os << "fs:"; break; |
| 95 | case kGs: os << "gs:"; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 96 | default: break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | size_t DisassemblerX86::DumpInstruction(std::ostream& os, const uint8_t* instr) { |
| 101 | const uint8_t* begin_instr = instr; |
| 102 | bool have_prefixes = true; |
| 103 | uint8_t prefix[4] = {0, 0, 0, 0}; |
| 104 | const char** modrm_opcodes = NULL; |
| 105 | do { |
| 106 | switch (*instr) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 107 | // Group 1 - lock and repeat prefixes: |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 108 | case 0xF0: |
| 109 | case 0xF2: |
| 110 | case 0xF3: |
| 111 | prefix[0] = *instr; |
| 112 | break; |
| 113 | // Group 2 - segment override prefixes: |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 114 | case kCs: |
| 115 | case kSs: |
| 116 | case kDs: |
| 117 | case kEs: |
| 118 | case kFs: |
| 119 | case kGs: |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 120 | prefix[1] = *instr; |
| 121 | break; |
| 122 | // Group 3 - operand size override: |
| 123 | case 0x66: |
| 124 | prefix[2] = *instr; |
| 125 | break; |
| 126 | // Group 4 - address size override: |
| 127 | case 0x67: |
| 128 | prefix[3] = *instr; |
| 129 | break; |
| 130 | default: |
| 131 | have_prefixes = false; |
| 132 | break; |
| 133 | } |
| 134 | if (have_prefixes) { |
| 135 | instr++; |
| 136 | } |
| 137 | } while (have_prefixes); |
| 138 | uint8_t rex = (*instr >= 0x40 && *instr <= 0x4F) ? *instr : 0; |
| 139 | bool has_modrm = false; |
| 140 | bool reg_is_opcode = false; |
| 141 | size_t immediate_bytes = 0; |
| 142 | size_t branch_bytes = 0; |
| 143 | std::ostringstream opcode; |
| 144 | bool store = false; // stores to memory (ie rm is on the left) |
| 145 | bool load = false; // loads from memory (ie rm is on the right) |
| 146 | bool byte_operand = false; |
| 147 | bool ax = false; // implicit use of ax |
| 148 | bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 149 | RegFile src_reg_file = GPR; |
| 150 | RegFile dst_reg_file = GPR; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 151 | switch (*instr) { |
| 152 | #define DISASSEMBLER_ENTRY(opname, \ |
| 153 | rm8_r8, rm32_r32, \ |
| 154 | r8_rm8, r32_rm32, \ |
| 155 | ax8_i8, ax32_i32) \ |
| 156 | case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \ |
| 157 | case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \ |
| 158 | case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \ |
| 159 | case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \ |
| 160 | case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \ |
| 161 | case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break; |
| 162 | |
| 163 | DISASSEMBLER_ENTRY(add, |
| 164 | 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */, |
| 165 | 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */, |
| 166 | 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */) |
| 167 | DISASSEMBLER_ENTRY(or, |
| 168 | 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */, |
| 169 | 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */, |
| 170 | 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */) |
| 171 | DISASSEMBLER_ENTRY(adc, |
| 172 | 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */, |
| 173 | 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */, |
| 174 | 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */) |
| 175 | DISASSEMBLER_ENTRY(sbb, |
| 176 | 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */, |
| 177 | 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */, |
| 178 | 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */) |
| 179 | DISASSEMBLER_ENTRY(and, |
| 180 | 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */, |
| 181 | 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */, |
| 182 | 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */) |
| 183 | DISASSEMBLER_ENTRY(sub, |
| 184 | 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */, |
| 185 | 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */, |
| 186 | 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */) |
| 187 | DISASSEMBLER_ENTRY(xor, |
| 188 | 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */, |
| 189 | 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */, |
| 190 | 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */) |
| 191 | DISASSEMBLER_ENTRY(cmp, |
| 192 | 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */, |
| 193 | 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */, |
| 194 | 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */) |
| 195 | |
| 196 | #undef DISASSEMBLER_ENTRY |
| 197 | case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: |
| 198 | opcode << "push"; |
| 199 | reg_in_opcode = true; |
| 200 | break; |
| 201 | case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F: |
| 202 | opcode << "pop"; |
| 203 | reg_in_opcode = true; |
| 204 | break; |
| 205 | case 0x68: opcode << "push"; immediate_bytes = 4; break; |
| 206 | case 0x6A: opcode << "push"; immediate_bytes = 1; break; |
| 207 | case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: |
| 208 | case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F: |
| 209 | static const char* condition_codes[] = |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 210 | {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a", |
| 211 | "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] | 212 | }; |
| 213 | opcode << "j" << condition_codes[*instr & 0xF]; |
| 214 | branch_bytes = 1; |
| 215 | break; |
| 216 | case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break; |
| 217 | case 0x89: opcode << "mov"; store = true; has_modrm = true; break; |
| 218 | case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break; |
| 219 | case 0x8B: opcode << "mov"; load = true; has_modrm = true; break; |
| 220 | |
| 221 | case 0x0F: // 2 byte extended opcode |
| 222 | instr++; |
| 223 | switch (*instr) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 224 | case 0x10: case 0x11: |
| 225 | if (prefix[0] == 0xF2) { |
| 226 | opcode << "movsd"; |
| 227 | } else if (prefix[0] == 0xF3) { |
| 228 | opcode << "movss"; |
| 229 | } else if (prefix[2] == 0x66) { |
| 230 | opcode << "movupd"; |
| 231 | } else { |
| 232 | opcode << "movups"; |
| 233 | } |
| 234 | has_modrm = true; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 235 | src_reg_file = dst_reg_file = SSE; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 236 | load = *instr == 0x10; |
| 237 | store = !load; |
| 238 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 239 | case 0x38: // 3 byte extended opcode |
| 240 | opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr); |
| 241 | break; |
| 242 | case 0x3A: // 3 byte extended opcode |
| 243 | opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr); |
| 244 | break; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 245 | case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: |
| 246 | case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: { |
| 247 | switch (*instr) { |
| 248 | case 0x50: opcode << "movmsk"; break; |
| 249 | case 0x51: opcode << "sqrt"; break; |
| 250 | case 0x52: opcode << "rsqrt"; break; |
| 251 | case 0x53: opcode << "rcp"; break; |
| 252 | case 0x54: opcode << "and"; break; |
| 253 | case 0x55: opcode << "andn"; break; |
| 254 | case 0x56: opcode << "or"; break; |
| 255 | case 0x57: opcode << "xor"; break; |
| 256 | case 0x58: opcode << "add"; break; |
| 257 | case 0x59: opcode << "mul"; break; |
| 258 | case 0x5C: opcode << "sub"; break; |
| 259 | case 0x5D: opcode << "min"; break; |
| 260 | case 0x5E: opcode << "div"; break; |
| 261 | case 0x5F: opcode << "max"; break; |
| 262 | default: LOG(FATAL) << "Unreachable"; |
| 263 | } |
| 264 | if (prefix[2] == 0x66) { |
| 265 | opcode << "pd"; |
| 266 | prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode |
| 267 | } else if (prefix[0] == 0xF2) { |
| 268 | opcode << "sd"; |
| 269 | prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode |
| 270 | } else if (prefix[0] == 0xF3) { |
| 271 | opcode << "ss"; |
| 272 | prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode |
| 273 | } else { |
| 274 | opcode << "ps"; |
| 275 | } |
| 276 | load = true; |
| 277 | has_modrm = true; |
| 278 | src_reg_file = dst_reg_file = SSE; |
| 279 | break; |
| 280 | } |
| 281 | case 0x5A: |
| 282 | if (prefix[2] == 0x66) { |
| 283 | opcode << "cvtpd2ps"; |
| 284 | prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode |
| 285 | } else if (prefix[0] == 0xF2) { |
| 286 | opcode << "cvtsd2ss"; |
| 287 | prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode |
| 288 | } else if (prefix[0] == 0xF3) { |
| 289 | opcode << "cvtss2sd"; |
| 290 | prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode |
| 291 | } else { |
| 292 | opcode << "cvtps2pd"; |
| 293 | } |
| 294 | load = true; |
| 295 | has_modrm = true; |
| 296 | src_reg_file = dst_reg_file = SSE; |
| 297 | break; |
| 298 | case 0x5B: |
| 299 | if (prefix[2] == 0x66) { |
| 300 | opcode << "cvtps2dq"; |
| 301 | prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode |
| 302 | } else if (prefix[0] == 0xF2) { |
| 303 | opcode << "bad opcode F2 0F 5B"; |
| 304 | } else if (prefix[0] == 0xF3) { |
| 305 | opcode << "cvttps2dq"; |
| 306 | prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode |
| 307 | } else { |
| 308 | opcode << "cvtdq2ps"; |
| 309 | } |
| 310 | load = true; |
| 311 | has_modrm = true; |
| 312 | src_reg_file = dst_reg_file = SSE; |
| 313 | break; |
| 314 | case 0x6E: |
| 315 | if (prefix[2] == 0x66) { |
| 316 | dst_reg_file = SSE; |
| 317 | opcode << "movq"; |
| 318 | prefix[2] = 0; // clear prefix now its served its purpose as part of the opcode |
| 319 | } else { |
| 320 | dst_reg_file = MMX; |
| 321 | opcode << "movd"; |
| 322 | } |
| 323 | load = true; |
| 324 | has_modrm = true; |
| 325 | break; |
| 326 | case 0x6F: |
| 327 | if (prefix[2] == 0x66) { |
| 328 | dst_reg_file = SSE; |
| 329 | opcode << "movdqa"; |
| 330 | } else if (prefix[0] == 0xF3) { |
| 331 | dst_reg_file = SSE; |
| 332 | opcode << "movdqu"; |
| 333 | prefix[0] = 0; // clear prefix now its served its purpose as part of the opcode |
| 334 | } else { |
| 335 | dst_reg_file = MMX; |
| 336 | opcode << "movq"; |
| 337 | } |
| 338 | load = true; |
| 339 | has_modrm = true; |
| 340 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 341 | case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87: |
| 342 | case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F: |
| 343 | opcode << "j" << condition_codes[*instr & 0xF]; |
| 344 | branch_bytes = 4; |
| 345 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 346 | case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97: |
| 347 | case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F: |
| 348 | opcode << "set" << condition_codes[*instr & 0xF]; |
| 349 | modrm_opcodes = NULL; |
| 350 | reg_is_opcode = true; |
| 351 | has_modrm = true; |
| 352 | store = true; |
| 353 | break; |
| 354 | case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break; |
| 355 | case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 356 | default: |
| 357 | opcode << StringPrintf("unknown opcode '0F %02X'", *instr); |
| 358 | break; |
| 359 | } |
| 360 | break; |
| 361 | case 0x80: case 0x81: case 0x82: case 0x83: |
| 362 | static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"}; |
| 363 | modrm_opcodes = x80_opcodes; |
| 364 | has_modrm = true; |
| 365 | reg_is_opcode = true; |
| 366 | store = true; |
| 367 | byte_operand = (*instr & 1) == 0; |
| 368 | immediate_bytes = *instr == 0x81 ? 4 : 1; |
| 369 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 370 | case 0x8D: |
| 371 | opcode << "lea"; |
| 372 | has_modrm = true; |
| 373 | load = true; |
| 374 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 375 | case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7: |
| 376 | opcode << "mov"; |
| 377 | immediate_bytes = 1; |
| 378 | reg_in_opcode = true; |
| 379 | break; |
| 380 | case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF: |
| 381 | opcode << "mov"; |
| 382 | immediate_bytes = 4; |
| 383 | reg_in_opcode = true; |
| 384 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 385 | case 0xC0: case 0xC1: |
Elliott Hughes | 16b5c29 | 2012-04-16 20:37:16 -0700 | [diff] [blame^] | 386 | case 0xD0: case 0xD1: |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 387 | static const char* shift_opcodes[] = |
| 388 | {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"}; |
| 389 | modrm_opcodes = shift_opcodes; |
| 390 | has_modrm = true; |
| 391 | reg_is_opcode = true; |
| 392 | store = true; |
Elliott Hughes | 16b5c29 | 2012-04-16 20:37:16 -0700 | [diff] [blame^] | 393 | immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 394 | byte_operand = *instr == 0xC0; |
| 395 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 396 | case 0xC3: opcode << "ret"; break; |
Elliott Hughes | 0589ca9 | 2012-04-09 18:26:20 -0700 | [diff] [blame] | 397 | case 0xC7: |
| 398 | static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"}; |
| 399 | modrm_opcodes = c7_opcodes; |
| 400 | store = true; |
| 401 | immediate_bytes = 4; |
| 402 | has_modrm = true; |
| 403 | reg_is_opcode = true; |
| 404 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 405 | case 0xCC: opcode << "int 3"; break; |
| 406 | case 0xE8: opcode << "call"; branch_bytes = 4; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 407 | case 0xE9: opcode << "jmp"; branch_bytes = 4; break; |
| 408 | case 0xEB: opcode << "jmp"; branch_bytes = 1; break; |
| 409 | case 0xFF: |
| 410 | static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"}; |
| 411 | modrm_opcodes = ff_opcodes; |
| 412 | has_modrm = true; |
| 413 | reg_is_opcode = true; |
| 414 | load = true; |
| 415 | break; |
| 416 | default: |
| 417 | opcode << StringPrintf("unknown opcode '%02X'", *instr); |
| 418 | break; |
| 419 | } |
| 420 | std::ostringstream args; |
| 421 | if (reg_in_opcode) { |
| 422 | DCHECK(!has_modrm); |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 423 | DumpReg(args, rex, *instr & 0x7, false, prefix[2], GPR); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 424 | } |
| 425 | instr++; |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 426 | uint32_t address_bits = 0; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 427 | if (has_modrm) { |
| 428 | uint8_t modrm = *instr; |
| 429 | instr++; |
| 430 | uint8_t mod = modrm >> 6; |
| 431 | uint8_t reg_or_opcode = (modrm >> 3) & 7; |
| 432 | uint8_t rm = modrm & 7; |
| 433 | std::ostringstream address; |
| 434 | if (mod == 0 && rm == 5) { // fixed address |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 435 | address_bits = *reinterpret_cast<const uint32_t*>(instr); |
| 436 | address << StringPrintf("[0x%x]", address_bits); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 437 | instr += 4; |
| 438 | } else if (rm == 4 && mod != 3) { // SIB |
| 439 | uint8_t sib = *instr; |
| 440 | instr++; |
| 441 | uint8_t ss = (sib >> 6) & 3; |
| 442 | uint8_t index = (sib >> 3) & 7; |
| 443 | uint8_t base = sib & 7; |
| 444 | address << "["; |
| 445 | if (base != 5 || mod != 0) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 446 | DumpBaseReg(address, rex, base); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 447 | if (index != 4) { |
| 448 | address << " + "; |
| 449 | } |
| 450 | } |
| 451 | if (index != 4) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 452 | DumpIndexReg(address, rex, index); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 453 | if (ss != 0) { |
| 454 | address << StringPrintf(" * %d", 1 << ss); |
| 455 | } |
| 456 | } |
| 457 | if (mod == 1) { |
| 458 | address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr)); |
| 459 | instr++; |
| 460 | } else if (mod == 2) { |
| 461 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 462 | instr += 4; |
| 463 | } |
| 464 | address << "]"; |
| 465 | } else { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 466 | if (mod == 3) { |
| 467 | DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file); |
| 468 | } else { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 469 | address << "["; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 470 | DumpBaseReg(address, rex, rm); |
| 471 | if (mod == 1) { |
| 472 | address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr)); |
| 473 | instr++; |
| 474 | } else if (mod == 2) { |
| 475 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 476 | instr += 4; |
| 477 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 478 | address << "]"; |
| 479 | } |
| 480 | } |
| 481 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 482 | if (reg_is_opcode && modrm_opcodes != NULL) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 483 | opcode << modrm_opcodes[reg_or_opcode]; |
| 484 | } |
| 485 | if (load) { |
| 486 | if (!reg_is_opcode) { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 487 | 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] | 488 | args << ", "; |
| 489 | } |
| 490 | DumpSegmentOverride(args, prefix[1]); |
| 491 | args << address.str(); |
| 492 | } else { |
| 493 | DCHECK(store); |
| 494 | DumpSegmentOverride(args, prefix[1]); |
| 495 | args << address.str(); |
| 496 | if (!reg_is_opcode) { |
| 497 | args << ", "; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 498 | 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] | 499 | } |
| 500 | } |
| 501 | } |
| 502 | if (ax) { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 503 | DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 504 | } |
| 505 | if (immediate_bytes > 0) { |
| 506 | if (has_modrm || reg_in_opcode || ax) { |
| 507 | args << ", "; |
| 508 | } |
| 509 | if (immediate_bytes == 1) { |
| 510 | args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr)); |
| 511 | instr++; |
| 512 | } else { |
| 513 | CHECK_EQ(immediate_bytes, 4u); |
| 514 | args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr)); |
| 515 | instr += 4; |
| 516 | } |
| 517 | } else if (branch_bytes > 0) { |
| 518 | DCHECK(!has_modrm); |
| 519 | int32_t displacement; |
| 520 | if (branch_bytes == 1) { |
| 521 | displacement = *reinterpret_cast<const int8_t*>(instr); |
| 522 | instr++; |
| 523 | } else { |
| 524 | CHECK_EQ(branch_bytes, 4u); |
| 525 | displacement = *reinterpret_cast<const int32_t*>(instr); |
| 526 | instr += 4; |
| 527 | } |
Elliott Hughes | 14178a9 | 2012-04-16 17:24:51 -0700 | [diff] [blame] | 528 | args << StringPrintf("%+d (%p)", displacement, instr + displacement); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 529 | } |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 530 | if (prefix[1] == kFs) { |
| 531 | args << " ; "; |
| 532 | Thread::DumpThreadOffset(args, address_bits, 4); |
| 533 | } |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 534 | std::stringstream hex; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 535 | for (size_t i = 0; begin_instr + i < instr; ++i) { |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 536 | hex << StringPrintf("%02X", begin_instr[i]); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 537 | } |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 538 | os << StringPrintf("\t\t\t%p: %22s \t%-7s ", begin_instr, hex.str().c_str(), opcode.str().c_str()) << args.str() << '\n'; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 539 | return instr - begin_instr; |
| 540 | } |
| 541 | |
| 542 | } // namespace x86 |
| 543 | } // namespace art |