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" }; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 39 | static const char* gReg16Names[] = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" }; |
| 40 | static const char* gReg32Names[] = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" }; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 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 |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 148 | bool cx = false; // implicit use of cx |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 149 | bool reg_in_opcode = false; // low 3-bits of opcode encode register parameter |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 150 | bool no_ops = false; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 151 | RegFile src_reg_file = GPR; |
| 152 | RegFile dst_reg_file = GPR; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 153 | switch (*instr) { |
| 154 | #define DISASSEMBLER_ENTRY(opname, \ |
| 155 | rm8_r8, rm32_r32, \ |
| 156 | r8_rm8, r32_rm32, \ |
| 157 | ax8_i8, ax32_i32) \ |
| 158 | case rm8_r8: opcode << #opname; store = true; has_modrm = true; byte_operand = true; break; \ |
| 159 | case rm32_r32: opcode << #opname; store = true; has_modrm = true; break; \ |
| 160 | case r8_rm8: opcode << #opname; load = true; has_modrm = true; byte_operand = true; break; \ |
| 161 | case r32_rm32: opcode << #opname; load = true; has_modrm = true; break; \ |
| 162 | case ax8_i8: opcode << #opname; ax = true; immediate_bytes = 1; byte_operand = true; break; \ |
| 163 | case ax32_i32: opcode << #opname; ax = true; immediate_bytes = 4; break; |
| 164 | |
| 165 | DISASSEMBLER_ENTRY(add, |
| 166 | 0x00 /* RegMem8/Reg8 */, 0x01 /* RegMem32/Reg32 */, |
| 167 | 0x02 /* Reg8/RegMem8 */, 0x03 /* Reg32/RegMem32 */, |
| 168 | 0x04 /* Rax8/imm8 opcode */, 0x05 /* Rax32/imm32 */) |
| 169 | DISASSEMBLER_ENTRY(or, |
| 170 | 0x08 /* RegMem8/Reg8 */, 0x09 /* RegMem32/Reg32 */, |
| 171 | 0x0A /* Reg8/RegMem8 */, 0x0B /* Reg32/RegMem32 */, |
| 172 | 0x0C /* Rax8/imm8 opcode */, 0x0D /* Rax32/imm32 */) |
| 173 | DISASSEMBLER_ENTRY(adc, |
| 174 | 0x10 /* RegMem8/Reg8 */, 0x11 /* RegMem32/Reg32 */, |
| 175 | 0x12 /* Reg8/RegMem8 */, 0x13 /* Reg32/RegMem32 */, |
| 176 | 0x14 /* Rax8/imm8 opcode */, 0x15 /* Rax32/imm32 */) |
| 177 | DISASSEMBLER_ENTRY(sbb, |
| 178 | 0x18 /* RegMem8/Reg8 */, 0x19 /* RegMem32/Reg32 */, |
| 179 | 0x1A /* Reg8/RegMem8 */, 0x1B /* Reg32/RegMem32 */, |
| 180 | 0x1C /* Rax8/imm8 opcode */, 0x1D /* Rax32/imm32 */) |
| 181 | DISASSEMBLER_ENTRY(and, |
| 182 | 0x20 /* RegMem8/Reg8 */, 0x21 /* RegMem32/Reg32 */, |
| 183 | 0x22 /* Reg8/RegMem8 */, 0x23 /* Reg32/RegMem32 */, |
| 184 | 0x24 /* Rax8/imm8 opcode */, 0x25 /* Rax32/imm32 */) |
| 185 | DISASSEMBLER_ENTRY(sub, |
| 186 | 0x28 /* RegMem8/Reg8 */, 0x29 /* RegMem32/Reg32 */, |
| 187 | 0x2A /* Reg8/RegMem8 */, 0x2B /* Reg32/RegMem32 */, |
| 188 | 0x2C /* Rax8/imm8 opcode */, 0x2D /* Rax32/imm32 */) |
| 189 | DISASSEMBLER_ENTRY(xor, |
| 190 | 0x30 /* RegMem8/Reg8 */, 0x31 /* RegMem32/Reg32 */, |
| 191 | 0x32 /* Reg8/RegMem8 */, 0x33 /* Reg32/RegMem32 */, |
| 192 | 0x34 /* Rax8/imm8 opcode */, 0x35 /* Rax32/imm32 */) |
| 193 | DISASSEMBLER_ENTRY(cmp, |
| 194 | 0x38 /* RegMem8/Reg8 */, 0x39 /* RegMem32/Reg32 */, |
| 195 | 0x3A /* Reg8/RegMem8 */, 0x3B /* Reg32/RegMem32 */, |
| 196 | 0x3C /* Rax8/imm8 opcode */, 0x3D /* Rax32/imm32 */) |
| 197 | |
| 198 | #undef DISASSEMBLER_ENTRY |
| 199 | case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: |
| 200 | opcode << "push"; |
| 201 | reg_in_opcode = true; |
| 202 | break; |
| 203 | case 0x58: case 0x59: case 0x5A: case 0x5B: case 0x5C: case 0x5D: case 0x5E: case 0x5F: |
| 204 | opcode << "pop"; |
| 205 | reg_in_opcode = true; |
| 206 | break; |
| 207 | case 0x68: opcode << "push"; immediate_bytes = 4; break; |
| 208 | case 0x6A: opcode << "push"; immediate_bytes = 1; break; |
| 209 | case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77: |
| 210 | case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F: |
| 211 | static const char* condition_codes[] = |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 212 | {"o", "no", "b/nae/c", "nb/ae/nc", "z/eq", "nz/ne", "be/na", "nbe/a", |
| 213 | "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] | 214 | }; |
| 215 | opcode << "j" << condition_codes[*instr & 0xF]; |
| 216 | branch_bytes = 1; |
| 217 | break; |
| 218 | case 0x88: opcode << "mov"; store = true; has_modrm = true; byte_operand = true; break; |
| 219 | case 0x89: opcode << "mov"; store = true; has_modrm = true; break; |
| 220 | case 0x8A: opcode << "mov"; load = true; has_modrm = true; byte_operand = true; break; |
| 221 | case 0x8B: opcode << "mov"; load = true; has_modrm = true; break; |
| 222 | |
| 223 | case 0x0F: // 2 byte extended opcode |
| 224 | instr++; |
| 225 | switch (*instr) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 226 | case 0x10: case 0x11: |
| 227 | if (prefix[0] == 0xF2) { |
| 228 | opcode << "movsd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 229 | 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] | 230 | } else if (prefix[0] == 0xF3) { |
| 231 | opcode << "movss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 232 | 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] | 233 | } else if (prefix[2] == 0x66) { |
| 234 | opcode << "movupd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 235 | 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] | 236 | } else { |
| 237 | opcode << "movups"; |
| 238 | } |
| 239 | has_modrm = true; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 240 | src_reg_file = dst_reg_file = SSE; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 241 | load = *instr == 0x10; |
| 242 | store = !load; |
| 243 | break; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 244 | case 0x2A: |
| 245 | if (prefix[2] == 0x66) { |
| 246 | opcode << "cvtpi2pd"; |
| 247 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 248 | } else if (prefix[0] == 0xF2) { |
| 249 | opcode << "cvtsi2sd"; |
| 250 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 251 | } else if (prefix[0] == 0xF3) { |
| 252 | opcode << "cvtsi2ss"; |
| 253 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 254 | } else { |
| 255 | opcode << "cvtpi2ps"; |
| 256 | } |
| 257 | load = true; |
| 258 | has_modrm = true; |
| 259 | dst_reg_file = SSE; |
| 260 | break; |
| 261 | case 0x2C: |
| 262 | if (prefix[2] == 0x66) { |
| 263 | opcode << "cvttpd2pi"; |
| 264 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 265 | } else if (prefix[0] == 0xF2) { |
| 266 | opcode << "cvttsd2si"; |
| 267 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 268 | } else if (prefix[0] == 0xF3) { |
| 269 | opcode << "cvttss2si"; |
| 270 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 271 | } else { |
| 272 | opcode << "cvttps2pi"; |
| 273 | } |
| 274 | load = true; |
| 275 | has_modrm = true; |
| 276 | src_reg_file = SSE; |
| 277 | break; |
| 278 | case 0x2D: |
| 279 | if (prefix[2] == 0x66) { |
| 280 | opcode << "cvtpd2pi"; |
| 281 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 282 | } else if (prefix[0] == 0xF2) { |
| 283 | opcode << "cvtsd2si"; |
| 284 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 285 | } else if (prefix[0] == 0xF3) { |
| 286 | opcode << "cvtss2si"; |
| 287 | prefix[0] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 288 | } else { |
| 289 | opcode << "cvtps2pi"; |
| 290 | } |
| 291 | load = true; |
| 292 | has_modrm = true; |
| 293 | src_reg_file = SSE; |
| 294 | break; |
| 295 | case 0x2E: |
| 296 | opcode << "u"; |
| 297 | // FALLTHROUGH |
| 298 | case 0x2F: |
| 299 | if (prefix[2] == 0x66) { |
| 300 | opcode << "comisd"; |
| 301 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 302 | } else { |
| 303 | opcode << "comiss"; |
| 304 | } |
| 305 | has_modrm = true; |
| 306 | load = true; |
| 307 | src_reg_file = dst_reg_file = SSE; |
| 308 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 309 | case 0x38: // 3 byte extended opcode |
| 310 | opcode << StringPrintf("unknown opcode '0F 38 %02X'", *instr); |
| 311 | break; |
| 312 | case 0x3A: // 3 byte extended opcode |
| 313 | opcode << StringPrintf("unknown opcode '0F 3A %02X'", *instr); |
| 314 | break; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 315 | case 0x50: case 0x51: case 0x52: case 0x53: case 0x54: case 0x55: case 0x56: case 0x57: |
| 316 | case 0x58: case 0x59: case 0x5C: case 0x5D: case 0x5E: case 0x5F: { |
| 317 | switch (*instr) { |
| 318 | case 0x50: opcode << "movmsk"; break; |
| 319 | case 0x51: opcode << "sqrt"; break; |
| 320 | case 0x52: opcode << "rsqrt"; break; |
| 321 | case 0x53: opcode << "rcp"; break; |
| 322 | case 0x54: opcode << "and"; break; |
| 323 | case 0x55: opcode << "andn"; break; |
| 324 | case 0x56: opcode << "or"; break; |
| 325 | case 0x57: opcode << "xor"; break; |
| 326 | case 0x58: opcode << "add"; break; |
| 327 | case 0x59: opcode << "mul"; break; |
| 328 | case 0x5C: opcode << "sub"; break; |
| 329 | case 0x5D: opcode << "min"; break; |
| 330 | case 0x5E: opcode << "div"; break; |
| 331 | case 0x5F: opcode << "max"; break; |
| 332 | default: LOG(FATAL) << "Unreachable"; |
| 333 | } |
| 334 | if (prefix[2] == 0x66) { |
| 335 | opcode << "pd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 336 | 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] | 337 | } else if (prefix[0] == 0xF2) { |
| 338 | opcode << "sd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 339 | 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] | 340 | } else if (prefix[0] == 0xF3) { |
| 341 | opcode << "ss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 342 | 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] | 343 | } else { |
| 344 | opcode << "ps"; |
| 345 | } |
| 346 | load = true; |
| 347 | has_modrm = true; |
| 348 | src_reg_file = dst_reg_file = SSE; |
| 349 | break; |
| 350 | } |
| 351 | case 0x5A: |
| 352 | if (prefix[2] == 0x66) { |
| 353 | opcode << "cvtpd2ps"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 354 | 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] | 355 | } else if (prefix[0] == 0xF2) { |
| 356 | opcode << "cvtsd2ss"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 357 | 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] | 358 | } else if (prefix[0] == 0xF3) { |
| 359 | opcode << "cvtss2sd"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 360 | 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] | 361 | } else { |
| 362 | opcode << "cvtps2pd"; |
| 363 | } |
| 364 | load = true; |
| 365 | has_modrm = true; |
| 366 | src_reg_file = dst_reg_file = SSE; |
| 367 | break; |
| 368 | case 0x5B: |
| 369 | if (prefix[2] == 0x66) { |
| 370 | opcode << "cvtps2dq"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 371 | 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] | 372 | } else if (prefix[0] == 0xF2) { |
| 373 | opcode << "bad opcode F2 0F 5B"; |
| 374 | } else if (prefix[0] == 0xF3) { |
| 375 | opcode << "cvttps2dq"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 376 | 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] | 377 | } else { |
| 378 | opcode << "cvtdq2ps"; |
| 379 | } |
| 380 | load = true; |
| 381 | has_modrm = true; |
| 382 | src_reg_file = dst_reg_file = SSE; |
| 383 | break; |
| 384 | case 0x6E: |
| 385 | if (prefix[2] == 0x66) { |
| 386 | dst_reg_file = SSE; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 387 | 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] | 388 | } else { |
| 389 | dst_reg_file = MMX; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 390 | } |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 391 | opcode << "movd"; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 392 | load = true; |
| 393 | has_modrm = true; |
| 394 | break; |
| 395 | case 0x6F: |
| 396 | if (prefix[2] == 0x66) { |
| 397 | dst_reg_file = SSE; |
| 398 | opcode << "movdqa"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 399 | 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] | 400 | } else if (prefix[0] == 0xF3) { |
| 401 | dst_reg_file = SSE; |
| 402 | opcode << "movdqu"; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 403 | 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] | 404 | } else { |
| 405 | dst_reg_file = MMX; |
| 406 | opcode << "movq"; |
| 407 | } |
| 408 | load = true; |
| 409 | has_modrm = true; |
| 410 | break; |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 411 | case 0x71: |
| 412 | if (prefix[2] == 0x66) { |
| 413 | dst_reg_file = SSE; |
| 414 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 415 | } else { |
| 416 | dst_reg_file = MMX; |
| 417 | } |
| 418 | static const char* x71_opcodes[] = {"unknown-71", "unknown-71", "psrlw", "unknown-71", "psraw", "unknown-71", "psllw", "unknown-71"}; |
| 419 | modrm_opcodes = x71_opcodes; |
| 420 | reg_is_opcode = true; |
| 421 | has_modrm = true; |
| 422 | store = true; |
| 423 | immediate_bytes = 1; |
| 424 | break; |
| 425 | case 0x72: |
| 426 | if (prefix[2] == 0x66) { |
| 427 | dst_reg_file = SSE; |
| 428 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 429 | } else { |
| 430 | dst_reg_file = MMX; |
| 431 | } |
| 432 | static const char* x72_opcodes[] = {"unknown-72", "unknown-72", "psrld", "unknown-72", "psrad", "unknown-72", "pslld", "unknown-72"}; |
| 433 | modrm_opcodes = x72_opcodes; |
| 434 | reg_is_opcode = true; |
| 435 | has_modrm = true; |
| 436 | store = true; |
| 437 | immediate_bytes = 1; |
| 438 | break; |
| 439 | case 0x73: |
| 440 | if (prefix[2] == 0x66) { |
| 441 | dst_reg_file = SSE; |
| 442 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 443 | } else { |
| 444 | dst_reg_file = MMX; |
| 445 | } |
| 446 | static const char* x73_opcodes[] = {"unknown-73", "unknown-73", "psrlq", "unknown-73", "unknown-73", "unknown-73", "psllq", "unknown-73"}; |
| 447 | modrm_opcodes = x73_opcodes; |
| 448 | reg_is_opcode = true; |
| 449 | has_modrm = true; |
| 450 | store = true; |
| 451 | immediate_bytes = 1; |
| 452 | break; |
| 453 | case 0x7E: |
| 454 | if (prefix[2] == 0x66) { |
| 455 | src_reg_file = SSE; |
| 456 | prefix[2] = 0; // clear prefix now it's served its purpose as part of the opcode |
| 457 | } else { |
| 458 | src_reg_file = MMX; |
| 459 | } |
| 460 | opcode << "movd"; |
| 461 | has_modrm = true; |
| 462 | store = true; |
| 463 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 464 | case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87: |
| 465 | case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F: |
| 466 | opcode << "j" << condition_codes[*instr & 0xF]; |
| 467 | branch_bytes = 4; |
| 468 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 469 | case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97: |
| 470 | case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F: |
| 471 | opcode << "set" << condition_codes[*instr & 0xF]; |
| 472 | modrm_opcodes = NULL; |
| 473 | reg_is_opcode = true; |
| 474 | has_modrm = true; |
| 475 | store = true; |
| 476 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 477 | case 0xAE: |
| 478 | if (prefix[0] == 0xF3) { |
| 479 | static const char* xAE_opcodes[] = {"rdfsbase", "rdgsbase", "wrfsbase", "wrgsbase", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE"}; |
| 480 | modrm_opcodes = xAE_opcodes; |
| 481 | reg_is_opcode = true; |
| 482 | has_modrm = true; |
| 483 | uint8_t reg_or_opcode = (instr[1] >> 3) & 7; |
| 484 | switch (reg_or_opcode) { |
| 485 | case 0: |
| 486 | prefix[1] = kFs; |
| 487 | load = true; |
| 488 | break; |
| 489 | case 1: |
| 490 | prefix[1] = kGs; |
| 491 | load = true; |
| 492 | break; |
| 493 | case 2: |
| 494 | prefix[1] = kFs; |
| 495 | store = true; |
| 496 | break; |
| 497 | case 3: |
| 498 | prefix[1] = kGs; |
| 499 | store = true; |
| 500 | break; |
| 501 | default: |
| 502 | load = true; |
| 503 | break; |
| 504 | } |
| 505 | } else { |
| 506 | static const char* xAE_opcodes[] = {"unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "unknown-AE", "lfence", "mfence", "sfence"}; |
| 507 | modrm_opcodes = xAE_opcodes; |
| 508 | reg_is_opcode = true; |
| 509 | has_modrm = true; |
| 510 | load = true; |
| 511 | no_ops = true; |
| 512 | } |
| 513 | break; |
jeffhao | 8302576 | 2012-08-02 11:08:56 -0700 | [diff] [blame] | 514 | case 0xB1: opcode << "cmpxchg"; has_modrm = true; store = true; break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 515 | case 0xB6: opcode << "movzxb"; has_modrm = true; load = true; break; |
| 516 | case 0xB7: opcode << "movzxw"; has_modrm = true; load = true; break; |
jeffhao | 854029c | 2012-07-23 17:31:30 -0700 | [diff] [blame] | 517 | case 0xBE: opcode << "movsxb"; has_modrm = true; load = true; break; |
| 518 | case 0xBF: opcode << "movsxw"; has_modrm = true; load = true; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 519 | default: |
| 520 | opcode << StringPrintf("unknown opcode '0F %02X'", *instr); |
| 521 | break; |
| 522 | } |
| 523 | break; |
| 524 | case 0x80: case 0x81: case 0x82: case 0x83: |
| 525 | static const char* x80_opcodes[] = {"add", "or", "adc", "sbb", "and", "sub", "xor", "cmp"}; |
| 526 | modrm_opcodes = x80_opcodes; |
| 527 | has_modrm = true; |
| 528 | reg_is_opcode = true; |
| 529 | store = true; |
| 530 | byte_operand = (*instr & 1) == 0; |
| 531 | immediate_bytes = *instr == 0x81 ? 4 : 1; |
| 532 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 533 | case 0x84: case 0x85: |
| 534 | opcode << "test"; |
| 535 | has_modrm = true; |
| 536 | load = true; |
| 537 | byte_operand = (*instr & 1) == 0; |
| 538 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 539 | case 0x8D: |
| 540 | opcode << "lea"; |
| 541 | has_modrm = true; |
| 542 | load = true; |
| 543 | break; |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 544 | case 0x8F: |
| 545 | opcode << "pop"; |
| 546 | has_modrm = true; |
| 547 | reg_is_opcode = true; |
| 548 | store = true; |
| 549 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 550 | case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7: |
| 551 | opcode << "mov"; |
| 552 | immediate_bytes = 1; |
| 553 | reg_in_opcode = true; |
| 554 | break; |
| 555 | case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF: |
| 556 | opcode << "mov"; |
| 557 | immediate_bytes = 4; |
| 558 | reg_in_opcode = true; |
| 559 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 560 | case 0xC0: case 0xC1: |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 561 | case 0xD0: case 0xD1: case 0xD2: case 0xD3: |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 562 | static const char* shift_opcodes[] = |
| 563 | {"rol", "ror", "rcl", "rcr", "shl", "shr", "unknown-shift", "sar"}; |
| 564 | modrm_opcodes = shift_opcodes; |
| 565 | has_modrm = true; |
| 566 | reg_is_opcode = true; |
| 567 | store = true; |
Elliott Hughes | 16b5c29 | 2012-04-16 20:37:16 -0700 | [diff] [blame] | 568 | immediate_bytes = ((*instr & 0xf0) == 0xc0) ? 1 : 0; |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 569 | cx = (*instr == 0xD2) || (*instr == 0xD3); |
| 570 | byte_operand = (*instr == 0xC0); |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 571 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 572 | case 0xC3: opcode << "ret"; break; |
Elliott Hughes | 0589ca9 | 2012-04-09 18:26:20 -0700 | [diff] [blame] | 573 | case 0xC7: |
| 574 | static const char* c7_opcodes[] = {"mov", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7", "unknown-c7"}; |
| 575 | modrm_opcodes = c7_opcodes; |
| 576 | store = true; |
| 577 | immediate_bytes = 4; |
| 578 | has_modrm = true; |
| 579 | reg_is_opcode = true; |
| 580 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 581 | case 0xCC: opcode << "int 3"; break; |
| 582 | case 0xE8: opcode << "call"; branch_bytes = 4; break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 583 | case 0xE9: opcode << "jmp"; branch_bytes = 4; break; |
| 584 | case 0xEB: opcode << "jmp"; branch_bytes = 1; break; |
jeffhao | 77ae36b | 2012-08-07 14:18:16 -0700 | [diff] [blame] | 585 | case 0xF5: opcode << "cmc"; break; |
jeffhao | 174651d | 2012-04-19 15:27:22 -0700 | [diff] [blame] | 586 | case 0xF6: case 0xF7: |
| 587 | 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 /"}; |
| 588 | modrm_opcodes = f7_opcodes; |
| 589 | has_modrm = true; |
| 590 | reg_is_opcode = true; |
| 591 | store = true; |
| 592 | immediate_bytes = ((instr[1] & 0x38) == 0) ? 1 : 0; |
| 593 | break; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 594 | case 0xFF: |
| 595 | static const char* ff_opcodes[] = {"inc", "dec", "call", "call", "jmp", "jmp", "push", "unknown-ff"}; |
| 596 | modrm_opcodes = ff_opcodes; |
| 597 | has_modrm = true; |
| 598 | reg_is_opcode = true; |
| 599 | load = true; |
| 600 | break; |
| 601 | default: |
| 602 | opcode << StringPrintf("unknown opcode '%02X'", *instr); |
| 603 | break; |
| 604 | } |
| 605 | std::ostringstream args; |
| 606 | if (reg_in_opcode) { |
| 607 | DCHECK(!has_modrm); |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 608 | DumpReg(args, rex, *instr & 0x7, false, prefix[2], GPR); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 609 | } |
| 610 | instr++; |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 611 | uint32_t address_bits = 0; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 612 | if (has_modrm) { |
| 613 | uint8_t modrm = *instr; |
| 614 | instr++; |
| 615 | uint8_t mod = modrm >> 6; |
| 616 | uint8_t reg_or_opcode = (modrm >> 3) & 7; |
| 617 | uint8_t rm = modrm & 7; |
| 618 | std::ostringstream address; |
| 619 | if (mod == 0 && rm == 5) { // fixed address |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 620 | address_bits = *reinterpret_cast<const uint32_t*>(instr); |
| 621 | address << StringPrintf("[0x%x]", address_bits); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 622 | instr += 4; |
| 623 | } else if (rm == 4 && mod != 3) { // SIB |
| 624 | uint8_t sib = *instr; |
| 625 | instr++; |
| 626 | uint8_t ss = (sib >> 6) & 3; |
| 627 | uint8_t index = (sib >> 3) & 7; |
| 628 | uint8_t base = sib & 7; |
| 629 | address << "["; |
| 630 | if (base != 5 || mod != 0) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 631 | DumpBaseReg(address, rex, base); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 632 | if (index != 4) { |
| 633 | address << " + "; |
| 634 | } |
| 635 | } |
| 636 | if (index != 4) { |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 637 | DumpIndexReg(address, rex, index); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 638 | if (ss != 0) { |
| 639 | address << StringPrintf(" * %d", 1 << ss); |
| 640 | } |
| 641 | } |
| 642 | if (mod == 1) { |
| 643 | address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr)); |
| 644 | instr++; |
| 645 | } else if (mod == 2) { |
| 646 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 647 | instr += 4; |
| 648 | } |
| 649 | address << "]"; |
| 650 | } else { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 651 | if (mod == 3) { |
jeffhao | 703f2cd | 2012-07-13 17:25:52 -0700 | [diff] [blame] | 652 | if (!no_ops) { |
| 653 | DumpReg(address, rex, rm, byte_operand, prefix[2], load ? src_reg_file : dst_reg_file); |
| 654 | } |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 655 | } else { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 656 | address << "["; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 657 | DumpBaseReg(address, rex, rm); |
| 658 | if (mod == 1) { |
| 659 | address << StringPrintf(" + %d", *reinterpret_cast<const int8_t*>(instr)); |
| 660 | instr++; |
| 661 | } else if (mod == 2) { |
| 662 | address << StringPrintf(" + %d", *reinterpret_cast<const int32_t*>(instr)); |
| 663 | instr += 4; |
| 664 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 665 | address << "]"; |
| 666 | } |
| 667 | } |
| 668 | |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 669 | if (reg_is_opcode && modrm_opcodes != NULL) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 670 | opcode << modrm_opcodes[reg_or_opcode]; |
| 671 | } |
| 672 | if (load) { |
| 673 | if (!reg_is_opcode) { |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 674 | 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] | 675 | args << ", "; |
| 676 | } |
| 677 | DumpSegmentOverride(args, prefix[1]); |
| 678 | args << address.str(); |
| 679 | } else { |
| 680 | DCHECK(store); |
| 681 | DumpSegmentOverride(args, prefix[1]); |
| 682 | args << address.str(); |
| 683 | if (!reg_is_opcode) { |
| 684 | args << ", "; |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 685 | 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] | 686 | } |
| 687 | } |
| 688 | } |
| 689 | if (ax) { |
jeffhao | fdffdf8 | 2012-07-11 16:08:43 -0700 | [diff] [blame] | 690 | // If this opcode implicitly uses ax, ax is always the first arg. |
Ian Rogers | bf98980 | 2012-04-16 16:07:49 -0700 | [diff] [blame] | 691 | DumpReg(args, rex, 0 /* EAX */, byte_operand, prefix[2], GPR); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 692 | } |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 693 | if (cx) { |
| 694 | args << ", "; |
| 695 | DumpReg(args, rex, 1 /* ECX */, true, prefix[2], GPR); |
| 696 | } |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 697 | if (immediate_bytes > 0) { |
jeffhao | e296248 | 2012-06-28 11:29:57 -0700 | [diff] [blame] | 698 | if (has_modrm || reg_in_opcode || ax || cx) { |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 699 | args << ", "; |
| 700 | } |
| 701 | if (immediate_bytes == 1) { |
| 702 | args << StringPrintf("%d", *reinterpret_cast<const int8_t*>(instr)); |
| 703 | instr++; |
| 704 | } else { |
| 705 | CHECK_EQ(immediate_bytes, 4u); |
| 706 | args << StringPrintf("%d", *reinterpret_cast<const int32_t*>(instr)); |
| 707 | instr += 4; |
| 708 | } |
| 709 | } else if (branch_bytes > 0) { |
| 710 | DCHECK(!has_modrm); |
| 711 | int32_t displacement; |
| 712 | if (branch_bytes == 1) { |
| 713 | displacement = *reinterpret_cast<const int8_t*>(instr); |
| 714 | instr++; |
| 715 | } else { |
| 716 | CHECK_EQ(branch_bytes, 4u); |
| 717 | displacement = *reinterpret_cast<const int32_t*>(instr); |
| 718 | instr += 4; |
| 719 | } |
Elliott Hughes | 14178a9 | 2012-04-16 17:24:51 -0700 | [diff] [blame] | 720 | args << StringPrintf("%+d (%p)", displacement, instr + displacement); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 721 | } |
Elliott Hughes | 92301d9 | 2012-04-10 15:57:52 -0700 | [diff] [blame] | 722 | if (prefix[1] == kFs) { |
| 723 | args << " ; "; |
| 724 | Thread::DumpThreadOffset(args, address_bits, 4); |
| 725 | } |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 726 | std::stringstream hex; |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 727 | for (size_t i = 0; begin_instr + i < instr; ++i) { |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 728 | hex << StringPrintf("%02X", begin_instr[i]); |
Ian Rogers | 706a10e | 2012-03-23 17:00:55 -0700 | [diff] [blame] | 729 | } |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 730 | 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] | 731 | return instr - begin_instr; |
| 732 | } |
| 733 | |
| 734 | } // namespace x86 |
| 735 | } // namespace art |