Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <assert.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdarg.h> |
| 31 | |
| 32 | #include "v8.h" |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame^] | 33 | |
| 34 | #if defined(V8_TARGET_ARCH_X64) |
| 35 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | #include "disasm.h" |
| 37 | |
| 38 | namespace disasm { |
| 39 | |
| 40 | enum OperandType { |
| 41 | UNSET_OP_ORDER = 0, |
| 42 | // Operand size decides between 16, 32 and 64 bit operands. |
| 43 | REG_OPER_OP_ORDER = 1, // Register destination, operand source. |
| 44 | OPER_REG_OP_ORDER = 2, // Operand destination, register source. |
| 45 | // Fixed 8-bit operands. |
| 46 | BYTE_SIZE_OPERAND_FLAG = 4, |
| 47 | BYTE_REG_OPER_OP_ORDER = REG_OPER_OP_ORDER | BYTE_SIZE_OPERAND_FLAG, |
| 48 | BYTE_OPER_REG_OP_ORDER = OPER_REG_OP_ORDER | BYTE_SIZE_OPERAND_FLAG |
| 49 | }; |
| 50 | |
| 51 | //------------------------------------------------------------------ |
| 52 | // Tables |
| 53 | //------------------------------------------------------------------ |
| 54 | struct ByteMnemonic { |
| 55 | int b; // -1 terminates, otherwise must be in range (0..255) |
| 56 | OperandType op_order_; |
| 57 | const char* mnem; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | static ByteMnemonic two_operands_instr[] = { |
| 62 | { 0x00, BYTE_OPER_REG_OP_ORDER, "add" }, |
| 63 | { 0x01, OPER_REG_OP_ORDER, "add" }, |
| 64 | { 0x02, BYTE_REG_OPER_OP_ORDER, "add" }, |
| 65 | { 0x03, REG_OPER_OP_ORDER, "add" }, |
| 66 | { 0x08, BYTE_OPER_REG_OP_ORDER, "or" }, |
| 67 | { 0x09, OPER_REG_OP_ORDER, "or" }, |
| 68 | { 0x0A, BYTE_REG_OPER_OP_ORDER, "or" }, |
| 69 | { 0x0B, REG_OPER_OP_ORDER, "or" }, |
| 70 | { 0x10, BYTE_OPER_REG_OP_ORDER, "adc" }, |
| 71 | { 0x11, OPER_REG_OP_ORDER, "adc" }, |
| 72 | { 0x12, BYTE_REG_OPER_OP_ORDER, "adc" }, |
| 73 | { 0x13, REG_OPER_OP_ORDER, "adc" }, |
| 74 | { 0x18, BYTE_OPER_REG_OP_ORDER, "sbb" }, |
| 75 | { 0x19, OPER_REG_OP_ORDER, "sbb" }, |
| 76 | { 0x1A, BYTE_REG_OPER_OP_ORDER, "sbb" }, |
| 77 | { 0x1B, REG_OPER_OP_ORDER, "sbb" }, |
| 78 | { 0x20, BYTE_OPER_REG_OP_ORDER, "and" }, |
| 79 | { 0x21, OPER_REG_OP_ORDER, "and" }, |
| 80 | { 0x22, BYTE_REG_OPER_OP_ORDER, "and" }, |
| 81 | { 0x23, REG_OPER_OP_ORDER, "and" }, |
| 82 | { 0x28, BYTE_OPER_REG_OP_ORDER, "sub" }, |
| 83 | { 0x29, OPER_REG_OP_ORDER, "sub" }, |
| 84 | { 0x2A, BYTE_REG_OPER_OP_ORDER, "sub" }, |
| 85 | { 0x2B, REG_OPER_OP_ORDER, "sub" }, |
| 86 | { 0x30, BYTE_OPER_REG_OP_ORDER, "xor" }, |
| 87 | { 0x31, OPER_REG_OP_ORDER, "xor" }, |
| 88 | { 0x32, BYTE_REG_OPER_OP_ORDER, "xor" }, |
| 89 | { 0x33, REG_OPER_OP_ORDER, "xor" }, |
| 90 | { 0x38, BYTE_OPER_REG_OP_ORDER, "cmp" }, |
| 91 | { 0x39, OPER_REG_OP_ORDER, "cmp" }, |
| 92 | { 0x3A, BYTE_REG_OPER_OP_ORDER, "cmp" }, |
| 93 | { 0x3B, REG_OPER_OP_ORDER, "cmp" }, |
| 94 | { 0x63, REG_OPER_OP_ORDER, "movsxlq" }, |
| 95 | { 0x84, BYTE_REG_OPER_OP_ORDER, "test" }, |
| 96 | { 0x85, REG_OPER_OP_ORDER, "test" }, |
| 97 | { 0x86, BYTE_REG_OPER_OP_ORDER, "xchg" }, |
| 98 | { 0x87, REG_OPER_OP_ORDER, "xchg" }, |
| 99 | { 0x88, BYTE_OPER_REG_OP_ORDER, "mov" }, |
| 100 | { 0x89, OPER_REG_OP_ORDER, "mov" }, |
| 101 | { 0x8A, BYTE_REG_OPER_OP_ORDER, "mov" }, |
| 102 | { 0x8B, REG_OPER_OP_ORDER, "mov" }, |
| 103 | { 0x8D, REG_OPER_OP_ORDER, "lea" }, |
| 104 | { -1, UNSET_OP_ORDER, "" } |
| 105 | }; |
| 106 | |
| 107 | |
| 108 | static ByteMnemonic zero_operands_instr[] = { |
| 109 | { 0xC3, UNSET_OP_ORDER, "ret" }, |
| 110 | { 0xC9, UNSET_OP_ORDER, "leave" }, |
| 111 | { 0xF4, UNSET_OP_ORDER, "hlt" }, |
| 112 | { 0xCC, UNSET_OP_ORDER, "int3" }, |
| 113 | { 0x60, UNSET_OP_ORDER, "pushad" }, |
| 114 | { 0x61, UNSET_OP_ORDER, "popad" }, |
| 115 | { 0x9C, UNSET_OP_ORDER, "pushfd" }, |
| 116 | { 0x9D, UNSET_OP_ORDER, "popfd" }, |
| 117 | { 0x9E, UNSET_OP_ORDER, "sahf" }, |
| 118 | { 0x99, UNSET_OP_ORDER, "cdq" }, |
| 119 | { 0x9B, UNSET_OP_ORDER, "fwait" }, |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 120 | { 0xA4, UNSET_OP_ORDER, "movs" }, |
| 121 | { 0xA5, UNSET_OP_ORDER, "movs" }, |
| 122 | { 0xA6, UNSET_OP_ORDER, "cmps" }, |
| 123 | { 0xA7, UNSET_OP_ORDER, "cmps" }, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | { -1, UNSET_OP_ORDER, "" } |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | static ByteMnemonic call_jump_instr[] = { |
| 129 | { 0xE8, UNSET_OP_ORDER, "call" }, |
| 130 | { 0xE9, UNSET_OP_ORDER, "jmp" }, |
| 131 | { -1, UNSET_OP_ORDER, "" } |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | static ByteMnemonic short_immediate_instr[] = { |
| 136 | { 0x05, UNSET_OP_ORDER, "add" }, |
| 137 | { 0x0D, UNSET_OP_ORDER, "or" }, |
| 138 | { 0x15, UNSET_OP_ORDER, "adc" }, |
| 139 | { 0x1D, UNSET_OP_ORDER, "sbb" }, |
| 140 | { 0x25, UNSET_OP_ORDER, "and" }, |
| 141 | { 0x2D, UNSET_OP_ORDER, "sub" }, |
| 142 | { 0x35, UNSET_OP_ORDER, "xor" }, |
| 143 | { 0x3D, UNSET_OP_ORDER, "cmp" }, |
| 144 | { -1, UNSET_OP_ORDER, "" } |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | static const char* conditional_code_suffix[] = { |
| 149 | "o", "no", "c", "nc", "z", "nz", "na", "a", |
| 150 | "s", "ns", "pe", "po", "l", "ge", "le", "g" |
| 151 | }; |
| 152 | |
| 153 | |
| 154 | enum InstructionType { |
| 155 | NO_INSTR, |
| 156 | ZERO_OPERANDS_INSTR, |
| 157 | TWO_OPERANDS_INSTR, |
| 158 | JUMP_CONDITIONAL_SHORT_INSTR, |
| 159 | REGISTER_INSTR, |
| 160 | PUSHPOP_INSTR, // Has implicit 64-bit operand size. |
| 161 | MOVE_REG_INSTR, |
| 162 | CALL_JUMP_INSTR, |
| 163 | SHORT_IMMEDIATE_INSTR |
| 164 | }; |
| 165 | |
| 166 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 167 | enum Prefixes { |
| 168 | ESCAPE_PREFIX = 0x0F, |
| 169 | OPERAND_SIZE_OVERRIDE_PREFIX = 0x66, |
| 170 | ADDRESS_SIZE_OVERRIDE_PREFIX = 0x67, |
| 171 | REPNE_PREFIX = 0xF2, |
| 172 | REP_PREFIX = 0xF3, |
| 173 | REPEQ_PREFIX = REP_PREFIX |
| 174 | }; |
| 175 | |
| 176 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | struct InstructionDesc { |
| 178 | const char* mnem; |
| 179 | InstructionType type; |
| 180 | OperandType op_order_; |
| 181 | bool byte_size_operation; // Fixed 8-bit operation. |
| 182 | }; |
| 183 | |
| 184 | |
| 185 | class InstructionTable { |
| 186 | public: |
| 187 | InstructionTable(); |
| 188 | const InstructionDesc& Get(byte x) const { |
| 189 | return instructions_[x]; |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | InstructionDesc instructions_[256]; |
| 194 | void Clear(); |
| 195 | void Init(); |
| 196 | void CopyTable(ByteMnemonic bm[], InstructionType type); |
| 197 | void SetTableRange(InstructionType type, byte start, byte end, bool byte_size, |
| 198 | const char* mnem); |
| 199 | void AddJumpConditionalShort(); |
| 200 | }; |
| 201 | |
| 202 | |
| 203 | InstructionTable::InstructionTable() { |
| 204 | Clear(); |
| 205 | Init(); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | void InstructionTable::Clear() { |
| 210 | for (int i = 0; i < 256; i++) { |
| 211 | instructions_[i].mnem = "(bad)"; |
| 212 | instructions_[i].type = NO_INSTR; |
| 213 | instructions_[i].op_order_ = UNSET_OP_ORDER; |
| 214 | instructions_[i].byte_size_operation = false; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void InstructionTable::Init() { |
| 220 | CopyTable(two_operands_instr, TWO_OPERANDS_INSTR); |
| 221 | CopyTable(zero_operands_instr, ZERO_OPERANDS_INSTR); |
| 222 | CopyTable(call_jump_instr, CALL_JUMP_INSTR); |
| 223 | CopyTable(short_immediate_instr, SHORT_IMMEDIATE_INSTR); |
| 224 | AddJumpConditionalShort(); |
| 225 | SetTableRange(PUSHPOP_INSTR, 0x50, 0x57, false, "push"); |
| 226 | SetTableRange(PUSHPOP_INSTR, 0x58, 0x5F, false, "pop"); |
| 227 | SetTableRange(MOVE_REG_INSTR, 0xB8, 0xBF, false, "mov"); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | void InstructionTable::CopyTable(ByteMnemonic bm[], InstructionType type) { |
| 232 | for (int i = 0; bm[i].b >= 0; i++) { |
| 233 | InstructionDesc* id = &instructions_[bm[i].b]; |
| 234 | id->mnem = bm[i].mnem; |
| 235 | OperandType op_order = bm[i].op_order_; |
| 236 | id->op_order_ = |
| 237 | static_cast<OperandType>(op_order & ~BYTE_SIZE_OPERAND_FLAG); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 238 | ASSERT_EQ(NO_INSTR, id->type); // Information not already entered |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 239 | id->type = type; |
| 240 | id->byte_size_operation = ((op_order & BYTE_SIZE_OPERAND_FLAG) != 0); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | void InstructionTable::SetTableRange(InstructionType type, |
| 246 | byte start, |
| 247 | byte end, |
| 248 | bool byte_size, |
| 249 | const char* mnem) { |
| 250 | for (byte b = start; b <= end; b++) { |
| 251 | InstructionDesc* id = &instructions_[b]; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 252 | ASSERT_EQ(NO_INSTR, id->type); // Information not already entered |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 253 | id->mnem = mnem; |
| 254 | id->type = type; |
| 255 | id->byte_size_operation = byte_size; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
| 260 | void InstructionTable::AddJumpConditionalShort() { |
| 261 | for (byte b = 0x70; b <= 0x7F; b++) { |
| 262 | InstructionDesc* id = &instructions_[b]; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 263 | ASSERT_EQ(NO_INSTR, id->type); // Information not already entered |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | id->mnem = NULL; // Computed depending on condition code. |
| 265 | id->type = JUMP_CONDITIONAL_SHORT_INSTR; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | |
| 270 | static InstructionTable instruction_table; |
| 271 | |
| 272 | static InstructionDesc cmov_instructions[16] = { |
| 273 | {"cmovo", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 274 | {"cmovno", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 275 | {"cmovc", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 276 | {"cmovnc", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 277 | {"cmovz", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 278 | {"cmovnz", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 279 | {"cmovna", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 280 | {"cmova", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 281 | {"cmovs", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 282 | {"cmovns", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 283 | {"cmovpe", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 284 | {"cmovpo", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 285 | {"cmovl", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 286 | {"cmovge", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 287 | {"cmovle", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, |
| 288 | {"cmovg", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false} |
| 289 | }; |
| 290 | |
| 291 | //------------------------------------------------------------------------------ |
| 292 | // DisassemblerX64 implementation. |
| 293 | |
| 294 | enum UnimplementedOpcodeAction { |
| 295 | CONTINUE_ON_UNIMPLEMENTED_OPCODE, |
| 296 | ABORT_ON_UNIMPLEMENTED_OPCODE |
| 297 | }; |
| 298 | |
| 299 | // A new DisassemblerX64 object is created to disassemble each instruction. |
| 300 | // The object can only disassemble a single instruction. |
| 301 | class DisassemblerX64 { |
| 302 | public: |
| 303 | DisassemblerX64(const NameConverter& converter, |
| 304 | UnimplementedOpcodeAction unimplemented_action = |
| 305 | ABORT_ON_UNIMPLEMENTED_OPCODE) |
| 306 | : converter_(converter), |
| 307 | tmp_buffer_pos_(0), |
| 308 | abort_on_unimplemented_( |
| 309 | unimplemented_action == ABORT_ON_UNIMPLEMENTED_OPCODE), |
| 310 | rex_(0), |
| 311 | operand_size_(0), |
| 312 | group_1_prefix_(0), |
| 313 | byte_size_operand_(false) { |
| 314 | tmp_buffer_[0] = '\0'; |
| 315 | } |
| 316 | |
| 317 | virtual ~DisassemblerX64() { |
| 318 | } |
| 319 | |
| 320 | // Writes one disassembled instruction into 'buffer' (0-terminated). |
| 321 | // Returns the length of the disassembled machine instruction in bytes. |
| 322 | int InstructionDecode(v8::internal::Vector<char> buffer, byte* instruction); |
| 323 | |
| 324 | private: |
| 325 | enum OperandSize { |
| 326 | BYTE_SIZE = 0, |
| 327 | WORD_SIZE = 1, |
| 328 | DOUBLEWORD_SIZE = 2, |
| 329 | QUADWORD_SIZE = 3 |
| 330 | }; |
| 331 | |
| 332 | const NameConverter& converter_; |
| 333 | v8::internal::EmbeddedVector<char, 128> tmp_buffer_; |
| 334 | unsigned int tmp_buffer_pos_; |
| 335 | bool abort_on_unimplemented_; |
| 336 | // Prefixes parsed |
| 337 | byte rex_; |
| 338 | byte operand_size_; // 0x66 or (if no group 3 prefix is present) 0x0. |
| 339 | byte group_1_prefix_; // 0xF2, 0xF3, or (if no group 1 prefix is present) 0. |
| 340 | // Byte size operand override. |
| 341 | bool byte_size_operand_; |
| 342 | |
| 343 | void setRex(byte rex) { |
| 344 | ASSERT_EQ(0x40, rex & 0xF0); |
| 345 | rex_ = rex; |
| 346 | } |
| 347 | |
| 348 | bool rex() { return rex_ != 0; } |
| 349 | |
| 350 | bool rex_b() { return (rex_ & 0x01) != 0; } |
| 351 | |
| 352 | // Actual number of base register given the low bits and the rex.b state. |
| 353 | int base_reg(int low_bits) { return low_bits | ((rex_ & 0x01) << 3); } |
| 354 | |
| 355 | bool rex_x() { return (rex_ & 0x02) != 0; } |
| 356 | |
| 357 | bool rex_r() { return (rex_ & 0x04) != 0; } |
| 358 | |
| 359 | bool rex_w() { return (rex_ & 0x08) != 0; } |
| 360 | |
| 361 | OperandSize operand_size() { |
| 362 | if (byte_size_operand_) return BYTE_SIZE; |
| 363 | if (rex_w()) return QUADWORD_SIZE; |
| 364 | if (operand_size_ != 0) return WORD_SIZE; |
| 365 | return DOUBLEWORD_SIZE; |
| 366 | } |
| 367 | |
| 368 | char operand_size_code() { |
| 369 | return "bwlq"[operand_size()]; |
| 370 | } |
| 371 | |
| 372 | const char* NameOfCPURegister(int reg) const { |
| 373 | return converter_.NameOfCPURegister(reg); |
| 374 | } |
| 375 | |
| 376 | const char* NameOfByteCPURegister(int reg) const { |
| 377 | return converter_.NameOfByteCPURegister(reg); |
| 378 | } |
| 379 | |
| 380 | const char* NameOfXMMRegister(int reg) const { |
| 381 | return converter_.NameOfXMMRegister(reg); |
| 382 | } |
| 383 | |
| 384 | const char* NameOfAddress(byte* addr) const { |
| 385 | return converter_.NameOfAddress(addr); |
| 386 | } |
| 387 | |
| 388 | // Disassembler helper functions. |
| 389 | void get_modrm(byte data, |
| 390 | int* mod, |
| 391 | int* regop, |
| 392 | int* rm) { |
| 393 | *mod = (data >> 6) & 3; |
| 394 | *regop = ((data & 0x38) >> 3) | (rex_r() ? 8 : 0); |
| 395 | *rm = (data & 7) | (rex_b() ? 8 : 0); |
| 396 | } |
| 397 | |
| 398 | void get_sib(byte data, |
| 399 | int* scale, |
| 400 | int* index, |
| 401 | int* base) { |
| 402 | *scale = (data >> 6) & 3; |
| 403 | *index = ((data >> 3) & 7) | (rex_x() ? 8 : 0); |
| 404 | *base = (data & 7) | (rex_b() ? 8 : 0); |
| 405 | } |
| 406 | |
| 407 | typedef const char* (DisassemblerX64::*RegisterNameMapping)(int reg) const; |
| 408 | |
| 409 | int PrintRightOperandHelper(byte* modrmp, |
| 410 | RegisterNameMapping register_name); |
| 411 | int PrintRightOperand(byte* modrmp); |
| 412 | int PrintRightByteOperand(byte* modrmp); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 413 | int PrintRightXMMOperand(byte* modrmp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 414 | int PrintOperands(const char* mnem, |
| 415 | OperandType op_order, |
| 416 | byte* data); |
| 417 | int PrintImmediate(byte* data, OperandSize size); |
| 418 | int PrintImmediateOp(byte* data); |
| 419 | const char* TwoByteMnemonic(byte opcode); |
| 420 | int TwoByteOpcodeInstruction(byte* data); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 421 | int F6F7Instruction(byte* data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 422 | int ShiftInstruction(byte* data); |
| 423 | int JumpShort(byte* data); |
| 424 | int JumpConditional(byte* data); |
| 425 | int JumpConditionalShort(byte* data); |
| 426 | int SetCC(byte* data); |
| 427 | int FPUInstruction(byte* data); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 428 | int MemoryFPUInstruction(int escape_opcode, int regop, byte* modrm_start); |
| 429 | int RegisterFPUInstruction(int escape_opcode, byte modrm_byte); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 430 | void AppendToBuffer(const char* format, ...); |
| 431 | |
| 432 | void UnimplementedInstruction() { |
| 433 | if (abort_on_unimplemented_) { |
| 434 | CHECK(false); |
| 435 | } else { |
| 436 | AppendToBuffer("'Unimplemented Instruction'"); |
| 437 | } |
| 438 | } |
| 439 | }; |
| 440 | |
| 441 | |
| 442 | void DisassemblerX64::AppendToBuffer(const char* format, ...) { |
| 443 | v8::internal::Vector<char> buf = tmp_buffer_ + tmp_buffer_pos_; |
| 444 | va_list args; |
| 445 | va_start(args, format); |
| 446 | int result = v8::internal::OS::VSNPrintF(buf, format, args); |
| 447 | va_end(args); |
| 448 | tmp_buffer_pos_ += result; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | int DisassemblerX64::PrintRightOperandHelper( |
| 453 | byte* modrmp, |
| 454 | RegisterNameMapping register_name) { |
| 455 | int mod, regop, rm; |
| 456 | get_modrm(*modrmp, &mod, ®op, &rm); |
| 457 | switch (mod) { |
| 458 | case 0: |
| 459 | if ((rm & 7) == 5) { |
| 460 | int32_t disp = *reinterpret_cast<int32_t*>(modrmp + 1); |
| 461 | AppendToBuffer("[0x%x]", disp); |
| 462 | return 5; |
| 463 | } else if ((rm & 7) == 4) { |
| 464 | // Codes for SIB byte. |
| 465 | byte sib = *(modrmp + 1); |
| 466 | int scale, index, base; |
| 467 | get_sib(sib, &scale, &index, &base); |
| 468 | if (index == 4 && (base & 7) == 4 && scale == 0 /*times_1*/) { |
| 469 | // index == rsp means no index. Only use sib byte with no index for |
| 470 | // rsp and r12 base. |
| 471 | AppendToBuffer("[%s]", (this->*register_name)(base)); |
| 472 | return 2; |
| 473 | } else if (base == 5) { |
| 474 | // base == rbp means no base register (when mod == 0). |
| 475 | int32_t disp = *reinterpret_cast<int32_t*>(modrmp + 2); |
| 476 | AppendToBuffer("[%s*%d+0x%x]", |
| 477 | (this->*register_name)(index), |
| 478 | 1 << scale, disp); |
| 479 | return 6; |
| 480 | } else if (index != 4 && base != 5) { |
| 481 | // [base+index*scale] |
| 482 | AppendToBuffer("[%s+%s*%d]", |
| 483 | (this->*register_name)(base), |
| 484 | (this->*register_name)(index), |
| 485 | 1 << scale); |
| 486 | return 2; |
| 487 | } else { |
| 488 | UnimplementedInstruction(); |
| 489 | return 1; |
| 490 | } |
| 491 | } else { |
| 492 | AppendToBuffer("[%s]", (this->*register_name)(rm)); |
| 493 | return 1; |
| 494 | } |
| 495 | break; |
| 496 | case 1: // fall through |
| 497 | case 2: |
| 498 | if ((rm & 7) == 4) { |
| 499 | byte sib = *(modrmp + 1); |
| 500 | int scale, index, base; |
| 501 | get_sib(sib, &scale, &index, &base); |
| 502 | int disp = (mod == 2) ? *reinterpret_cast<int32_t*>(modrmp + 2) |
| 503 | : *reinterpret_cast<char*>(modrmp + 2); |
| 504 | if (index == 4 && (base & 7) == 4 && scale == 0 /*times_1*/) { |
| 505 | if (-disp > 0) { |
| 506 | AppendToBuffer("[%s-0x%x]", (this->*register_name)(base), -disp); |
| 507 | } else { |
| 508 | AppendToBuffer("[%s+0x%x]", (this->*register_name)(base), disp); |
| 509 | } |
| 510 | } else { |
| 511 | if (-disp > 0) { |
| 512 | AppendToBuffer("[%s+%s*%d-0x%x]", |
| 513 | (this->*register_name)(base), |
| 514 | (this->*register_name)(index), |
| 515 | 1 << scale, |
| 516 | -disp); |
| 517 | } else { |
| 518 | AppendToBuffer("[%s+%s*%d+0x%x]", |
| 519 | (this->*register_name)(base), |
| 520 | (this->*register_name)(index), |
| 521 | 1 << scale, |
| 522 | disp); |
| 523 | } |
| 524 | } |
| 525 | return mod == 2 ? 6 : 3; |
| 526 | } else { |
| 527 | // No sib. |
| 528 | int disp = (mod == 2) ? *reinterpret_cast<int32_t*>(modrmp + 1) |
| 529 | : *reinterpret_cast<char*>(modrmp + 1); |
| 530 | if (-disp > 0) { |
| 531 | AppendToBuffer("[%s-0x%x]", (this->*register_name)(rm), -disp); |
| 532 | } else { |
| 533 | AppendToBuffer("[%s+0x%x]", (this->*register_name)(rm), disp); |
| 534 | } |
| 535 | return (mod == 2) ? 5 : 2; |
| 536 | } |
| 537 | break; |
| 538 | case 3: |
| 539 | AppendToBuffer("%s", (this->*register_name)(rm)); |
| 540 | return 1; |
| 541 | default: |
| 542 | UnimplementedInstruction(); |
| 543 | return 1; |
| 544 | } |
| 545 | UNREACHABLE(); |
| 546 | } |
| 547 | |
| 548 | |
| 549 | int DisassemblerX64::PrintImmediate(byte* data, OperandSize size) { |
| 550 | int64_t value; |
| 551 | int count; |
| 552 | switch (size) { |
| 553 | case BYTE_SIZE: |
| 554 | value = *data; |
| 555 | count = 1; |
| 556 | break; |
| 557 | case WORD_SIZE: |
| 558 | value = *reinterpret_cast<int16_t*>(data); |
| 559 | count = 2; |
| 560 | break; |
| 561 | case DOUBLEWORD_SIZE: |
| 562 | value = *reinterpret_cast<uint32_t*>(data); |
| 563 | count = 4; |
| 564 | break; |
| 565 | case QUADWORD_SIZE: |
| 566 | value = *reinterpret_cast<int32_t*>(data); |
| 567 | count = 4; |
| 568 | break; |
| 569 | default: |
| 570 | UNREACHABLE(); |
| 571 | value = 0; // Initialize variables on all paths to satisfy the compiler. |
| 572 | count = 0; |
| 573 | } |
| 574 | AppendToBuffer("%" V8_PTR_PREFIX "x", value); |
| 575 | return count; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | int DisassemblerX64::PrintRightOperand(byte* modrmp) { |
| 580 | return PrintRightOperandHelper(modrmp, |
| 581 | &DisassemblerX64::NameOfCPURegister); |
| 582 | } |
| 583 | |
| 584 | |
| 585 | int DisassemblerX64::PrintRightByteOperand(byte* modrmp) { |
| 586 | return PrintRightOperandHelper(modrmp, |
| 587 | &DisassemblerX64::NameOfByteCPURegister); |
| 588 | } |
| 589 | |
| 590 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 591 | int DisassemblerX64::PrintRightXMMOperand(byte* modrmp) { |
| 592 | return PrintRightOperandHelper(modrmp, |
| 593 | &DisassemblerX64::NameOfXMMRegister); |
| 594 | } |
| 595 | |
| 596 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 597 | // Returns number of bytes used including the current *data. |
| 598 | // Writes instruction's mnemonic, left and right operands to 'tmp_buffer_'. |
| 599 | int DisassemblerX64::PrintOperands(const char* mnem, |
| 600 | OperandType op_order, |
| 601 | byte* data) { |
| 602 | byte modrm = *data; |
| 603 | int mod, regop, rm; |
| 604 | get_modrm(modrm, &mod, ®op, &rm); |
| 605 | int advance = 0; |
| 606 | const char* register_name = |
| 607 | byte_size_operand_ ? NameOfByteCPURegister(regop) |
| 608 | : NameOfCPURegister(regop); |
| 609 | switch (op_order) { |
| 610 | case REG_OPER_OP_ORDER: { |
| 611 | AppendToBuffer("%s%c %s,", |
| 612 | mnem, |
| 613 | operand_size_code(), |
| 614 | register_name); |
| 615 | advance = byte_size_operand_ ? PrintRightByteOperand(data) |
| 616 | : PrintRightOperand(data); |
| 617 | break; |
| 618 | } |
| 619 | case OPER_REG_OP_ORDER: { |
| 620 | AppendToBuffer("%s%c ", mnem, operand_size_code()); |
| 621 | advance = byte_size_operand_ ? PrintRightByteOperand(data) |
| 622 | : PrintRightOperand(data); |
| 623 | AppendToBuffer(",%s", register_name); |
| 624 | break; |
| 625 | } |
| 626 | default: |
| 627 | UNREACHABLE(); |
| 628 | break; |
| 629 | } |
| 630 | return advance; |
| 631 | } |
| 632 | |
| 633 | |
| 634 | // Returns number of bytes used by machine instruction, including *data byte. |
| 635 | // Writes immediate instructions to 'tmp_buffer_'. |
| 636 | int DisassemblerX64::PrintImmediateOp(byte* data) { |
| 637 | bool byte_size_immediate = (*data & 0x02) != 0; |
| 638 | byte modrm = *(data + 1); |
| 639 | int mod, regop, rm; |
| 640 | get_modrm(modrm, &mod, ®op, &rm); |
| 641 | const char* mnem = "Imm???"; |
| 642 | switch (regop) { |
| 643 | case 0: |
| 644 | mnem = "add"; |
| 645 | break; |
| 646 | case 1: |
| 647 | mnem = "or"; |
| 648 | break; |
| 649 | case 2: |
| 650 | mnem = "adc"; |
| 651 | break; |
| 652 | case 4: |
| 653 | mnem = "and"; |
| 654 | break; |
| 655 | case 5: |
| 656 | mnem = "sub"; |
| 657 | break; |
| 658 | case 6: |
| 659 | mnem = "xor"; |
| 660 | break; |
| 661 | case 7: |
| 662 | mnem = "cmp"; |
| 663 | break; |
| 664 | default: |
| 665 | UnimplementedInstruction(); |
| 666 | } |
| 667 | AppendToBuffer("%s%c ", mnem, operand_size_code()); |
| 668 | int count = PrintRightOperand(data + 1); |
| 669 | AppendToBuffer(",0x"); |
| 670 | OperandSize immediate_size = byte_size_immediate ? BYTE_SIZE : operand_size(); |
| 671 | count += PrintImmediate(data + 1 + count, immediate_size); |
| 672 | return 1 + count; |
| 673 | } |
| 674 | |
| 675 | |
| 676 | // Returns number of bytes used, including *data. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 677 | int DisassemblerX64::F6F7Instruction(byte* data) { |
| 678 | ASSERT(*data == 0xF7 || *data == 0xF6); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 679 | byte modrm = *(data + 1); |
| 680 | int mod, regop, rm; |
| 681 | get_modrm(modrm, &mod, ®op, &rm); |
| 682 | if (mod == 3 && regop != 0) { |
| 683 | const char* mnem = NULL; |
| 684 | switch (regop) { |
| 685 | case 2: |
| 686 | mnem = "not"; |
| 687 | break; |
| 688 | case 3: |
| 689 | mnem = "neg"; |
| 690 | break; |
| 691 | case 4: |
| 692 | mnem = "mul"; |
| 693 | break; |
| 694 | case 7: |
| 695 | mnem = "idiv"; |
| 696 | break; |
| 697 | default: |
| 698 | UnimplementedInstruction(); |
| 699 | } |
| 700 | AppendToBuffer("%s%c %s", |
| 701 | mnem, |
| 702 | operand_size_code(), |
| 703 | NameOfCPURegister(rm)); |
| 704 | return 2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 705 | } else if (regop == 0) { |
| 706 | AppendToBuffer("test%c ", operand_size_code()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 707 | int count = PrintRightOperand(data + 1); // Use name of 64-bit register. |
| 708 | AppendToBuffer(",0x"); |
| 709 | count += PrintImmediate(data + 1 + count, operand_size()); |
| 710 | return 1 + count; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 711 | } else { |
| 712 | UnimplementedInstruction(); |
| 713 | return 2; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | |
| 718 | int DisassemblerX64::ShiftInstruction(byte* data) { |
| 719 | byte op = *data & (~1); |
| 720 | if (op != 0xD0 && op != 0xD2 && op != 0xC0) { |
| 721 | UnimplementedInstruction(); |
| 722 | return 1; |
| 723 | } |
| 724 | byte modrm = *(data + 1); |
| 725 | int mod, regop, rm; |
| 726 | get_modrm(modrm, &mod, ®op, &rm); |
| 727 | regop &= 0x7; // The REX.R bit does not affect the operation. |
| 728 | int imm8 = -1; |
| 729 | int num_bytes = 2; |
| 730 | if (mod != 3) { |
| 731 | UnimplementedInstruction(); |
| 732 | return num_bytes; |
| 733 | } |
| 734 | const char* mnem = NULL; |
| 735 | switch (regop) { |
| 736 | case 0: |
| 737 | mnem = "rol"; |
| 738 | break; |
| 739 | case 1: |
| 740 | mnem = "ror"; |
| 741 | break; |
| 742 | case 2: |
| 743 | mnem = "rcl"; |
| 744 | break; |
| 745 | case 3: |
| 746 | mnem = "rcr"; |
| 747 | break; |
| 748 | case 4: |
| 749 | mnem = "shl"; |
| 750 | break; |
| 751 | case 5: |
| 752 | mnem = "shr"; |
| 753 | break; |
| 754 | case 7: |
| 755 | mnem = "sar"; |
| 756 | break; |
| 757 | default: |
| 758 | UnimplementedInstruction(); |
| 759 | return num_bytes; |
| 760 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 761 | ASSERT_NE(NULL, mnem); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 762 | if (op == 0xD0) { |
| 763 | imm8 = 1; |
| 764 | } else if (op == 0xC0) { |
| 765 | imm8 = *(data + 2); |
| 766 | num_bytes = 3; |
| 767 | } |
| 768 | AppendToBuffer("%s%c %s,", |
| 769 | mnem, |
| 770 | operand_size_code(), |
| 771 | byte_size_operand_ ? NameOfByteCPURegister(rm) |
| 772 | : NameOfCPURegister(rm)); |
| 773 | if (op == 0xD2) { |
| 774 | AppendToBuffer("cl"); |
| 775 | } else { |
| 776 | AppendToBuffer("%d", imm8); |
| 777 | } |
| 778 | return num_bytes; |
| 779 | } |
| 780 | |
| 781 | |
| 782 | // Returns number of bytes used, including *data. |
| 783 | int DisassemblerX64::JumpShort(byte* data) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 784 | ASSERT_EQ(0xEB, *data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 785 | byte b = *(data + 1); |
| 786 | byte* dest = data + static_cast<int8_t>(b) + 2; |
| 787 | AppendToBuffer("jmp %s", NameOfAddress(dest)); |
| 788 | return 2; |
| 789 | } |
| 790 | |
| 791 | |
| 792 | // Returns number of bytes used, including *data. |
| 793 | int DisassemblerX64::JumpConditional(byte* data) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 794 | ASSERT_EQ(0x0F, *data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 795 | byte cond = *(data + 1) & 0x0F; |
| 796 | byte* dest = data + *reinterpret_cast<int32_t*>(data + 2) + 6; |
| 797 | const char* mnem = conditional_code_suffix[cond]; |
| 798 | AppendToBuffer("j%s %s", mnem, NameOfAddress(dest)); |
| 799 | return 6; // includes 0x0F |
| 800 | } |
| 801 | |
| 802 | |
| 803 | // Returns number of bytes used, including *data. |
| 804 | int DisassemblerX64::JumpConditionalShort(byte* data) { |
| 805 | byte cond = *data & 0x0F; |
| 806 | byte b = *(data + 1); |
| 807 | byte* dest = data + static_cast<int8_t>(b) + 2; |
| 808 | const char* mnem = conditional_code_suffix[cond]; |
| 809 | AppendToBuffer("j%s %s", mnem, NameOfAddress(dest)); |
| 810 | return 2; |
| 811 | } |
| 812 | |
| 813 | |
| 814 | // Returns number of bytes used, including *data. |
| 815 | int DisassemblerX64::SetCC(byte* data) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 816 | ASSERT_EQ(0x0F, *data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 817 | byte cond = *(data + 1) & 0x0F; |
| 818 | const char* mnem = conditional_code_suffix[cond]; |
| 819 | AppendToBuffer("set%s%c ", mnem, operand_size_code()); |
| 820 | PrintRightByteOperand(data + 2); |
| 821 | return 3; // includes 0x0F |
| 822 | } |
| 823 | |
| 824 | |
| 825 | // Returns number of bytes used, including *data. |
| 826 | int DisassemblerX64::FPUInstruction(byte* data) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 827 | byte escape_opcode = *data; |
| 828 | ASSERT_EQ(0xD8, escape_opcode & 0xF8); |
| 829 | byte modrm_byte = *(data+1); |
| 830 | |
| 831 | if (modrm_byte >= 0xC0) { |
| 832 | return RegisterFPUInstruction(escape_opcode, modrm_byte); |
| 833 | } else { |
| 834 | return MemoryFPUInstruction(escape_opcode, modrm_byte, data+1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 835 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | int DisassemblerX64::MemoryFPUInstruction(int escape_opcode, |
| 839 | int modrm_byte, |
| 840 | byte* modrm_start) { |
| 841 | const char* mnem = "?"; |
| 842 | int regop = (modrm_byte >> 3) & 0x7; // reg/op field of modrm byte. |
| 843 | switch (escape_opcode) { |
| 844 | case 0xD9: switch (regop) { |
| 845 | case 0: mnem = "fld_s"; break; |
| 846 | case 3: mnem = "fstp_s"; break; |
| 847 | case 7: mnem = "fstcw"; break; |
| 848 | default: UnimplementedInstruction(); |
| 849 | } |
| 850 | break; |
| 851 | |
| 852 | case 0xDB: switch (regop) { |
| 853 | case 0: mnem = "fild_s"; break; |
| 854 | case 1: mnem = "fisttp_s"; break; |
| 855 | case 2: mnem = "fist_s"; break; |
| 856 | case 3: mnem = "fistp_s"; break; |
| 857 | default: UnimplementedInstruction(); |
| 858 | } |
| 859 | break; |
| 860 | |
| 861 | case 0xDD: switch (regop) { |
| 862 | case 0: mnem = "fld_d"; break; |
| 863 | case 3: mnem = "fstp_d"; break; |
| 864 | default: UnimplementedInstruction(); |
| 865 | } |
| 866 | break; |
| 867 | |
| 868 | case 0xDF: switch (regop) { |
| 869 | case 5: mnem = "fild_d"; break; |
| 870 | case 7: mnem = "fistp_d"; break; |
| 871 | default: UnimplementedInstruction(); |
| 872 | } |
| 873 | break; |
| 874 | |
| 875 | default: UnimplementedInstruction(); |
| 876 | } |
| 877 | AppendToBuffer("%s ", mnem); |
| 878 | int count = PrintRightOperand(modrm_start); |
| 879 | return count + 1; |
| 880 | } |
| 881 | |
| 882 | int DisassemblerX64::RegisterFPUInstruction(int escape_opcode, |
| 883 | byte modrm_byte) { |
| 884 | bool has_register = false; // Is the FPU register encoded in modrm_byte? |
| 885 | const char* mnem = "?"; |
| 886 | |
| 887 | switch (escape_opcode) { |
| 888 | case 0xD8: |
| 889 | UnimplementedInstruction(); |
| 890 | break; |
| 891 | |
| 892 | case 0xD9: |
| 893 | switch (modrm_byte & 0xF8) { |
| 894 | case 0xC8: |
| 895 | mnem = "fxch"; |
| 896 | has_register = true; |
| 897 | break; |
| 898 | default: |
| 899 | switch (modrm_byte) { |
| 900 | case 0xE0: mnem = "fchs"; break; |
| 901 | case 0xE1: mnem = "fabs"; break; |
| 902 | case 0xE4: mnem = "ftst"; break; |
| 903 | case 0xE8: mnem = "fld1"; break; |
| 904 | case 0xEE: mnem = "fldz"; break; |
| 905 | case 0xF5: mnem = "fprem1"; break; |
| 906 | case 0xF7: mnem = "fincstp"; break; |
| 907 | case 0xF8: mnem = "fprem"; break; |
| 908 | case 0xFE: mnem = "fsin"; break; |
| 909 | case 0xFF: mnem = "fcos"; break; |
| 910 | default: UnimplementedInstruction(); |
| 911 | } |
| 912 | } |
| 913 | break; |
| 914 | |
| 915 | case 0xDA: |
| 916 | if (modrm_byte == 0xE9) { |
| 917 | mnem = "fucompp"; |
| 918 | } else { |
| 919 | UnimplementedInstruction(); |
| 920 | } |
| 921 | break; |
| 922 | |
| 923 | case 0xDB: |
| 924 | if ((modrm_byte & 0xF8) == 0xE8) { |
| 925 | mnem = "fucomi"; |
| 926 | has_register = true; |
| 927 | } else if (modrm_byte == 0xE2) { |
| 928 | mnem = "fclex"; |
| 929 | } else { |
| 930 | UnimplementedInstruction(); |
| 931 | } |
| 932 | break; |
| 933 | |
| 934 | case 0xDC: |
| 935 | has_register = true; |
| 936 | switch (modrm_byte & 0xF8) { |
| 937 | case 0xC0: mnem = "fadd"; break; |
| 938 | case 0xE8: mnem = "fsub"; break; |
| 939 | case 0xC8: mnem = "fmul"; break; |
| 940 | case 0xF8: mnem = "fdiv"; break; |
| 941 | default: UnimplementedInstruction(); |
| 942 | } |
| 943 | break; |
| 944 | |
| 945 | case 0xDD: |
| 946 | has_register = true; |
| 947 | switch (modrm_byte & 0xF8) { |
| 948 | case 0xC0: mnem = "ffree"; break; |
| 949 | case 0xD8: mnem = "fstp"; break; |
| 950 | default: UnimplementedInstruction(); |
| 951 | } |
| 952 | break; |
| 953 | |
| 954 | case 0xDE: |
| 955 | if (modrm_byte == 0xD9) { |
| 956 | mnem = "fcompp"; |
| 957 | } else { |
| 958 | has_register = true; |
| 959 | switch (modrm_byte & 0xF8) { |
| 960 | case 0xC0: mnem = "faddp"; break; |
| 961 | case 0xE8: mnem = "fsubp"; break; |
| 962 | case 0xC8: mnem = "fmulp"; break; |
| 963 | case 0xF8: mnem = "fdivp"; break; |
| 964 | default: UnimplementedInstruction(); |
| 965 | } |
| 966 | } |
| 967 | break; |
| 968 | |
| 969 | case 0xDF: |
| 970 | if (modrm_byte == 0xE0) { |
| 971 | mnem = "fnstsw_ax"; |
| 972 | } else if ((modrm_byte & 0xF8) == 0xE8) { |
| 973 | mnem = "fucomip"; |
| 974 | has_register = true; |
| 975 | } |
| 976 | break; |
| 977 | |
| 978 | default: UnimplementedInstruction(); |
| 979 | } |
| 980 | |
| 981 | if (has_register) { |
| 982 | AppendToBuffer("%s st%d", mnem, modrm_byte & 0x7); |
| 983 | } else { |
| 984 | AppendToBuffer("%s", mnem); |
| 985 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 986 | return 2; |
| 987 | } |
| 988 | |
| 989 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 990 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 991 | // Handle all two-byte opcodes, which start with 0x0F. |
| 992 | // These instructions may be affected by an 0x66, 0xF2, or 0xF3 prefix. |
| 993 | // We do not use any three-byte opcodes, which start with 0x0F38 or 0x0F3A. |
| 994 | int DisassemblerX64::TwoByteOpcodeInstruction(byte* data) { |
| 995 | byte opcode = *(data + 1); |
| 996 | byte* current = data + 2; |
| 997 | // At return, "current" points to the start of the next instruction. |
| 998 | const char* mnemonic = TwoByteMnemonic(opcode); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 999 | if (operand_size_ == 0x66) { |
| 1000 | // 0x66 0x0F prefix. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1001 | int mod, regop, rm; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1002 | if (opcode == 0x3A) { |
| 1003 | byte third_byte = *current; |
| 1004 | current = data + 3; |
| 1005 | if (third_byte == 0x17) { |
| 1006 | get_modrm(*current, &mod, ®op, &rm); |
| 1007 | AppendToBuffer("extractps "); // reg/m32, xmm, imm8 |
| 1008 | current += PrintRightOperand(current); |
| 1009 | AppendToBuffer(", %s, %d", NameOfCPURegister(regop), (*current) & 3); |
| 1010 | current += 1; |
| 1011 | } else { |
| 1012 | UnimplementedInstruction(); |
| 1013 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1014 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1015 | get_modrm(*current, &mod, ®op, &rm); |
| 1016 | if (opcode == 0x6E) { |
| 1017 | AppendToBuffer("mov%c %s,", |
| 1018 | rex_w() ? 'q' : 'd', |
| 1019 | NameOfXMMRegister(regop)); |
| 1020 | current += PrintRightOperand(current); |
| 1021 | } else if (opcode == 0x7E) { |
| 1022 | AppendToBuffer("mov%c %s,", |
| 1023 | rex_w() ? 'q' : 'd', |
| 1024 | NameOfCPURegister(regop)); |
| 1025 | current += PrintRightXMMOperand(current); |
| 1026 | } else { |
| 1027 | const char* mnemonic = "?"; |
| 1028 | if (opcode == 0x57) { |
| 1029 | mnemonic = "xorpd"; |
| 1030 | } else if (opcode == 0x2E) { |
| 1031 | mnemonic = "comisd"; |
| 1032 | } else if (opcode == 0x2F) { |
| 1033 | mnemonic = "ucomisd"; |
| 1034 | } else { |
| 1035 | UnimplementedInstruction(); |
| 1036 | } |
| 1037 | AppendToBuffer("%s %s,", mnemonic, NameOfXMMRegister(regop)); |
| 1038 | current += PrintRightXMMOperand(current); |
| 1039 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1040 | } |
| 1041 | } else if (group_1_prefix_ == 0xF2) { |
| 1042 | // Beginning of instructions with prefix 0xF2. |
| 1043 | |
| 1044 | if (opcode == 0x11 || opcode == 0x10) { |
| 1045 | // MOVSD: Move scalar double-precision fp to/from/between XMM registers. |
| 1046 | AppendToBuffer("movsd "); |
| 1047 | int mod, regop, rm; |
| 1048 | get_modrm(*current, &mod, ®op, &rm); |
| 1049 | if (opcode == 0x11) { |
| 1050 | current += PrintRightOperand(current); |
| 1051 | AppendToBuffer(",%s", NameOfXMMRegister(regop)); |
| 1052 | } else { |
| 1053 | AppendToBuffer("%s,", NameOfXMMRegister(regop)); |
| 1054 | current += PrintRightOperand(current); |
| 1055 | } |
| 1056 | } else if (opcode == 0x2A) { |
| 1057 | // CVTSI2SD: integer to XMM double conversion. |
| 1058 | int mod, regop, rm; |
| 1059 | get_modrm(*current, &mod, ®op, &rm); |
| 1060 | AppendToBuffer("%s %s,", mnemonic, NameOfXMMRegister(regop)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1061 | current += PrintRightOperand(current); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1062 | } else if ((opcode & 0xF8) == 0x58 || opcode == 0x51) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1063 | // XMM arithmetic. Mnemonic was retrieved at the start of this function. |
| 1064 | int mod, regop, rm; |
| 1065 | get_modrm(*current, &mod, ®op, &rm); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1066 | AppendToBuffer("%s %s,", mnemonic, NameOfXMMRegister(regop)); |
| 1067 | current += PrintRightXMMOperand(current); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1068 | } else { |
| 1069 | UnimplementedInstruction(); |
| 1070 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1071 | } else if (group_1_prefix_ == 0xF3) { |
| 1072 | // Instructions with prefix 0xF3. |
| 1073 | if (opcode == 0x2C) { |
| 1074 | // CVTTSS2SI: Convert scalar single-precision FP to dword integer. |
| 1075 | // Assert that mod is not 3, so source is memory, not an XMM register. |
| 1076 | ASSERT_NE(0xC0, *current & 0xC0); |
| 1077 | current += PrintOperands("cvttss2si", REG_OPER_OP_ORDER, current); |
| 1078 | } else if (opcode == 0x5A) { |
| 1079 | int mod, regop, rm; |
| 1080 | get_modrm(*current, &mod, ®op, &rm); |
| 1081 | AppendToBuffer("cvtss2sd %s,", NameOfXMMRegister(regop)); |
| 1082 | current += PrintRightXMMOperand(current); |
| 1083 | } else { |
| 1084 | UnimplementedInstruction(); |
| 1085 | } |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1086 | } else if (opcode == 0x1F) { |
| 1087 | // NOP |
| 1088 | int mod, regop, rm; |
| 1089 | get_modrm(*current, &mod, ®op, &rm); |
| 1090 | current++; |
| 1091 | if (regop == 4) { // SIB byte present. |
| 1092 | current++; |
| 1093 | } |
| 1094 | if (mod == 1) { // Byte displacement. |
| 1095 | current += 1; |
| 1096 | } else if (mod == 2) { // 32-bit displacement. |
| 1097 | current += 4; |
| 1098 | } // else no immediate displacement. |
| 1099 | AppendToBuffer("nop"); |
| 1100 | } else if (opcode == 0xA2 || opcode == 0x31) { |
| 1101 | // RDTSC or CPUID |
| 1102 | AppendToBuffer("%s", mnemonic); |
| 1103 | |
| 1104 | } else if ((opcode & 0xF0) == 0x40) { |
| 1105 | // CMOVcc: conditional move. |
| 1106 | int condition = opcode & 0x0F; |
| 1107 | const InstructionDesc& idesc = cmov_instructions[condition]; |
| 1108 | byte_size_operand_ = idesc.byte_size_operation; |
| 1109 | current += PrintOperands(idesc.mnem, idesc.op_order_, current); |
| 1110 | |
| 1111 | } else if ((opcode & 0xF0) == 0x80) { |
| 1112 | // Jcc: Conditional jump (branch). |
| 1113 | current = data + JumpConditional(data); |
| 1114 | |
| 1115 | } else if (opcode == 0xBE || opcode == 0xBF || opcode == 0xB6 || |
| 1116 | opcode == 0xB7 || opcode == 0xAF) { |
| 1117 | // Size-extending moves, IMUL. |
| 1118 | current += PrintOperands(mnemonic, REG_OPER_OP_ORDER, current); |
| 1119 | |
| 1120 | } else if ((opcode & 0xF0) == 0x90) { |
| 1121 | // SETcc: Set byte on condition. Needs pointer to beginning of instruction. |
| 1122 | current = data + SetCC(data); |
| 1123 | |
| 1124 | } else if (opcode == 0xAB || opcode == 0xA5 || opcode == 0xAD) { |
| 1125 | // SHLD, SHRD (double-precision shift), BTS (bit set). |
| 1126 | AppendToBuffer("%s ", mnemonic); |
| 1127 | int mod, regop, rm; |
| 1128 | get_modrm(*current, &mod, ®op, &rm); |
| 1129 | current += PrintRightOperand(current); |
| 1130 | if (opcode == 0xAB) { |
| 1131 | AppendToBuffer(",%s", NameOfCPURegister(regop)); |
| 1132 | } else { |
| 1133 | AppendToBuffer(",%s,cl", NameOfCPURegister(regop)); |
| 1134 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1135 | } else { |
| 1136 | UnimplementedInstruction(); |
| 1137 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1138 | return static_cast<int>(current - data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | |
| 1142 | // Mnemonics for two-byte opcode instructions starting with 0x0F. |
| 1143 | // The argument is the second byte of the two-byte opcode. |
| 1144 | // Returns NULL if the instruction is not handled here. |
| 1145 | const char* DisassemblerX64::TwoByteMnemonic(byte opcode) { |
| 1146 | switch (opcode) { |
| 1147 | case 0x1F: |
| 1148 | return "nop"; |
| 1149 | case 0x2A: // F2 prefix. |
| 1150 | return "cvtsi2sd"; |
| 1151 | case 0x31: |
| 1152 | return "rdtsc"; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1153 | case 0x51: // F2 prefix. |
| 1154 | return "sqrtsd"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1155 | case 0x58: // F2 prefix. |
| 1156 | return "addsd"; |
| 1157 | case 0x59: // F2 prefix. |
| 1158 | return "mulsd"; |
| 1159 | case 0x5C: // F2 prefix. |
| 1160 | return "subsd"; |
| 1161 | case 0x5E: // F2 prefix. |
| 1162 | return "divsd"; |
| 1163 | case 0xA2: |
| 1164 | return "cpuid"; |
| 1165 | case 0xA5: |
| 1166 | return "shld"; |
| 1167 | case 0xAB: |
| 1168 | return "bts"; |
| 1169 | case 0xAD: |
| 1170 | return "shrd"; |
| 1171 | case 0xAF: |
| 1172 | return "imul"; |
| 1173 | case 0xB6: |
| 1174 | return "movzxb"; |
| 1175 | case 0xB7: |
| 1176 | return "movzxw"; |
| 1177 | case 0xBE: |
| 1178 | return "movsxb"; |
| 1179 | case 0xBF: |
| 1180 | return "movsxw"; |
| 1181 | default: |
| 1182 | return NULL; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | // Disassembles the instruction at instr, and writes it into out_buffer. |
| 1188 | int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, |
| 1189 | byte* instr) { |
| 1190 | tmp_buffer_pos_ = 0; // starting to write as position 0 |
| 1191 | byte* data = instr; |
| 1192 | bool processed = true; // Will be set to false if the current instruction |
| 1193 | // is not in 'instructions' table. |
| 1194 | byte current; |
| 1195 | |
| 1196 | // Scan for prefixes. |
| 1197 | while (true) { |
| 1198 | current = *data; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1199 | if (current == OPERAND_SIZE_OVERRIDE_PREFIX) { // Group 3 prefix. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1200 | operand_size_ = current; |
| 1201 | } else if ((current & 0xF0) == 0x40) { // REX prefix. |
| 1202 | setRex(current); |
| 1203 | if (rex_w()) AppendToBuffer("REX.W "); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1204 | } else if ((current & 0xFE) == 0xF2) { // Group 1 prefix (0xF2 or 0xF3). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1205 | group_1_prefix_ = current; |
| 1206 | } else { // Not a prefix - an opcode. |
| 1207 | break; |
| 1208 | } |
| 1209 | data++; |
| 1210 | } |
| 1211 | |
| 1212 | const InstructionDesc& idesc = instruction_table.Get(current); |
| 1213 | byte_size_operand_ = idesc.byte_size_operation; |
| 1214 | switch (idesc.type) { |
| 1215 | case ZERO_OPERANDS_INSTR: |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1216 | if (current >= 0xA4 && current <= 0xA7) { |
| 1217 | // String move or compare operations. |
| 1218 | if (group_1_prefix_ == REP_PREFIX) { |
| 1219 | // REP. |
| 1220 | AppendToBuffer("rep "); |
| 1221 | } |
| 1222 | if (rex_w()) AppendToBuffer("REX.W "); |
| 1223 | AppendToBuffer("%s%c", idesc.mnem, operand_size_code()); |
| 1224 | } else { |
| 1225 | AppendToBuffer("%s", idesc.mnem, operand_size_code()); |
| 1226 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1227 | data++; |
| 1228 | break; |
| 1229 | |
| 1230 | case TWO_OPERANDS_INSTR: |
| 1231 | data++; |
| 1232 | data += PrintOperands(idesc.mnem, idesc.op_order_, data); |
| 1233 | break; |
| 1234 | |
| 1235 | case JUMP_CONDITIONAL_SHORT_INSTR: |
| 1236 | data += JumpConditionalShort(data); |
| 1237 | break; |
| 1238 | |
| 1239 | case REGISTER_INSTR: |
| 1240 | AppendToBuffer("%s%c %s", |
| 1241 | idesc.mnem, |
| 1242 | operand_size_code(), |
| 1243 | NameOfCPURegister(base_reg(current & 0x07))); |
| 1244 | data++; |
| 1245 | break; |
| 1246 | case PUSHPOP_INSTR: |
| 1247 | AppendToBuffer("%s %s", |
| 1248 | idesc.mnem, |
| 1249 | NameOfCPURegister(base_reg(current & 0x07))); |
| 1250 | data++; |
| 1251 | break; |
| 1252 | case MOVE_REG_INSTR: { |
| 1253 | byte* addr = NULL; |
| 1254 | switch (operand_size()) { |
| 1255 | case WORD_SIZE: |
| 1256 | addr = reinterpret_cast<byte*>(*reinterpret_cast<int16_t*>(data + 1)); |
| 1257 | data += 3; |
| 1258 | break; |
| 1259 | case DOUBLEWORD_SIZE: |
| 1260 | addr = reinterpret_cast<byte*>(*reinterpret_cast<int32_t*>(data + 1)); |
| 1261 | data += 5; |
| 1262 | break; |
| 1263 | case QUADWORD_SIZE: |
| 1264 | addr = reinterpret_cast<byte*>(*reinterpret_cast<int64_t*>(data + 1)); |
| 1265 | data += 9; |
| 1266 | break; |
| 1267 | default: |
| 1268 | UNREACHABLE(); |
| 1269 | } |
| 1270 | AppendToBuffer("mov%c %s,%s", |
| 1271 | operand_size_code(), |
| 1272 | NameOfCPURegister(base_reg(current & 0x07)), |
| 1273 | NameOfAddress(addr)); |
| 1274 | break; |
| 1275 | } |
| 1276 | |
| 1277 | case CALL_JUMP_INSTR: { |
| 1278 | byte* addr = data + *reinterpret_cast<int32_t*>(data + 1) + 5; |
| 1279 | AppendToBuffer("%s %s", idesc.mnem, NameOfAddress(addr)); |
| 1280 | data += 5; |
| 1281 | break; |
| 1282 | } |
| 1283 | |
| 1284 | case SHORT_IMMEDIATE_INSTR: { |
| 1285 | byte* addr = |
| 1286 | reinterpret_cast<byte*>(*reinterpret_cast<int32_t*>(data + 1)); |
| 1287 | AppendToBuffer("%s rax, %s", idesc.mnem, NameOfAddress(addr)); |
| 1288 | data += 5; |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | case NO_INSTR: |
| 1293 | processed = false; |
| 1294 | break; |
| 1295 | |
| 1296 | default: |
| 1297 | UNIMPLEMENTED(); // This type is not implemented. |
| 1298 | } |
| 1299 | |
| 1300 | // The first byte didn't match any of the simple opcodes, so we |
| 1301 | // need to do special processing on it. |
| 1302 | if (!processed) { |
| 1303 | switch (*data) { |
| 1304 | case 0xC2: |
| 1305 | AppendToBuffer("ret 0x%x", *reinterpret_cast<uint16_t*>(data + 1)); |
| 1306 | data += 3; |
| 1307 | break; |
| 1308 | |
| 1309 | case 0x69: // fall through |
| 1310 | case 0x6B: { |
| 1311 | int mod, regop, rm; |
| 1312 | get_modrm(*(data + 1), &mod, ®op, &rm); |
| 1313 | int32_t imm = *data == 0x6B ? *(data + 2) |
| 1314 | : *reinterpret_cast<int32_t*>(data + 2); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1315 | AppendToBuffer("imul%c %s,%s,0x%x", |
| 1316 | operand_size_code(), |
| 1317 | NameOfCPURegister(regop), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1318 | NameOfCPURegister(rm), imm); |
| 1319 | data += 2 + (*data == 0x6B ? 1 : 4); |
| 1320 | break; |
| 1321 | } |
| 1322 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1323 | case 0x81: // fall through |
| 1324 | case 0x83: // 0x81 with sign extension bit set |
| 1325 | data += PrintImmediateOp(data); |
| 1326 | break; |
| 1327 | |
| 1328 | case 0x0F: |
| 1329 | data += TwoByteOpcodeInstruction(data); |
| 1330 | break; |
| 1331 | |
| 1332 | case 0x8F: { |
| 1333 | data++; |
| 1334 | int mod, regop, rm; |
| 1335 | get_modrm(*data, &mod, ®op, &rm); |
| 1336 | if (regop == 0) { |
| 1337 | AppendToBuffer("pop "); |
| 1338 | data += PrintRightOperand(data); |
| 1339 | } |
| 1340 | } |
| 1341 | break; |
| 1342 | |
| 1343 | case 0xFF: { |
| 1344 | data++; |
| 1345 | int mod, regop, rm; |
| 1346 | get_modrm(*data, &mod, ®op, &rm); |
| 1347 | const char* mnem = NULL; |
| 1348 | switch (regop) { |
| 1349 | case 0: |
| 1350 | mnem = "inc"; |
| 1351 | break; |
| 1352 | case 1: |
| 1353 | mnem = "dec"; |
| 1354 | break; |
| 1355 | case 2: |
| 1356 | mnem = "call"; |
| 1357 | break; |
| 1358 | case 4: |
| 1359 | mnem = "jmp"; |
| 1360 | break; |
| 1361 | case 6: |
| 1362 | mnem = "push"; |
| 1363 | break; |
| 1364 | default: |
| 1365 | mnem = "???"; |
| 1366 | } |
| 1367 | AppendToBuffer(((regop <= 1) ? "%s%c " : "%s "), |
| 1368 | mnem, |
| 1369 | operand_size_code()); |
| 1370 | data += PrintRightOperand(data); |
| 1371 | } |
| 1372 | break; |
| 1373 | |
| 1374 | case 0xC7: // imm32, fall through |
| 1375 | case 0xC6: // imm8 |
| 1376 | { |
| 1377 | bool is_byte = *data == 0xC6; |
| 1378 | data++; |
| 1379 | |
| 1380 | AppendToBuffer("mov%c ", is_byte ? 'b' : operand_size_code()); |
| 1381 | data += PrintRightOperand(data); |
| 1382 | int32_t imm = is_byte ? *data : *reinterpret_cast<int32_t*>(data); |
| 1383 | AppendToBuffer(",0x%x", imm); |
| 1384 | data += is_byte ? 1 : 4; |
| 1385 | } |
| 1386 | break; |
| 1387 | |
| 1388 | case 0x80: { |
| 1389 | data++; |
| 1390 | AppendToBuffer("cmpb "); |
| 1391 | data += PrintRightOperand(data); |
| 1392 | int32_t imm = *data; |
| 1393 | AppendToBuffer(",0x%x", imm); |
| 1394 | data++; |
| 1395 | } |
| 1396 | break; |
| 1397 | |
| 1398 | case 0x88: // 8bit, fall through |
| 1399 | case 0x89: // 32bit |
| 1400 | { |
| 1401 | bool is_byte = *data == 0x88; |
| 1402 | int mod, regop, rm; |
| 1403 | data++; |
| 1404 | get_modrm(*data, &mod, ®op, &rm); |
| 1405 | AppendToBuffer("mov%c ", is_byte ? 'b' : operand_size_code()); |
| 1406 | data += PrintRightOperand(data); |
| 1407 | AppendToBuffer(",%s", NameOfCPURegister(regop)); |
| 1408 | } |
| 1409 | break; |
| 1410 | |
| 1411 | case 0x90: |
| 1412 | case 0x91: |
| 1413 | case 0x92: |
| 1414 | case 0x93: |
| 1415 | case 0x94: |
| 1416 | case 0x95: |
| 1417 | case 0x96: |
| 1418 | case 0x97: { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1419 | int reg = (*data & 0x7) | (rex_b() ? 8 : 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1420 | if (reg == 0) { |
| 1421 | AppendToBuffer("nop"); // Common name for xchg rax,rax. |
| 1422 | } else { |
| 1423 | AppendToBuffer("xchg%c rax, %s", |
| 1424 | operand_size_code(), |
| 1425 | NameOfCPURegister(reg)); |
| 1426 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1427 | data++; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1428 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1429 | break; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1430 | |
| 1431 | case 0xFE: { |
| 1432 | data++; |
| 1433 | int mod, regop, rm; |
| 1434 | get_modrm(*data, &mod, ®op, &rm); |
| 1435 | if (mod == 3 && regop == 1) { |
| 1436 | AppendToBuffer("decb %s", NameOfCPURegister(rm)); |
| 1437 | } else { |
| 1438 | UnimplementedInstruction(); |
| 1439 | } |
| 1440 | data++; |
| 1441 | } |
| 1442 | break; |
| 1443 | |
| 1444 | case 0x68: |
| 1445 | AppendToBuffer("push 0x%x", *reinterpret_cast<int32_t*>(data + 1)); |
| 1446 | data += 5; |
| 1447 | break; |
| 1448 | |
| 1449 | case 0x6A: |
| 1450 | AppendToBuffer("push 0x%x", *reinterpret_cast<int8_t*>(data + 1)); |
| 1451 | data += 2; |
| 1452 | break; |
| 1453 | |
| 1454 | case 0xA1: // Fall through. |
| 1455 | case 0xA3: |
| 1456 | switch (operand_size()) { |
| 1457 | case DOUBLEWORD_SIZE: { |
| 1458 | const char* memory_location = NameOfAddress( |
| 1459 | reinterpret_cast<byte*>( |
| 1460 | *reinterpret_cast<int32_t*>(data + 1))); |
| 1461 | if (*data == 0xA1) { // Opcode 0xA1 |
| 1462 | AppendToBuffer("movzxlq rax,(%s)", memory_location); |
| 1463 | } else { // Opcode 0xA3 |
| 1464 | AppendToBuffer("movzxlq (%s),rax", memory_location); |
| 1465 | } |
| 1466 | data += 5; |
| 1467 | break; |
| 1468 | } |
| 1469 | case QUADWORD_SIZE: { |
| 1470 | // New x64 instruction mov rax,(imm_64). |
| 1471 | const char* memory_location = NameOfAddress( |
| 1472 | *reinterpret_cast<byte**>(data + 1)); |
| 1473 | if (*data == 0xA1) { // Opcode 0xA1 |
| 1474 | AppendToBuffer("movq rax,(%s)", memory_location); |
| 1475 | } else { // Opcode 0xA3 |
| 1476 | AppendToBuffer("movq (%s),rax", memory_location); |
| 1477 | } |
| 1478 | data += 9; |
| 1479 | break; |
| 1480 | } |
| 1481 | default: |
| 1482 | UnimplementedInstruction(); |
| 1483 | data += 2; |
| 1484 | } |
| 1485 | break; |
| 1486 | |
| 1487 | case 0xA8: |
| 1488 | AppendToBuffer("test al,0x%x", *reinterpret_cast<uint8_t*>(data + 1)); |
| 1489 | data += 2; |
| 1490 | break; |
| 1491 | |
| 1492 | case 0xA9: { |
| 1493 | int64_t value = 0; |
| 1494 | switch (operand_size()) { |
| 1495 | case WORD_SIZE: |
| 1496 | value = *reinterpret_cast<uint16_t*>(data + 1); |
| 1497 | data += 3; |
| 1498 | break; |
| 1499 | case DOUBLEWORD_SIZE: |
| 1500 | value = *reinterpret_cast<uint32_t*>(data + 1); |
| 1501 | data += 5; |
| 1502 | break; |
| 1503 | case QUADWORD_SIZE: |
| 1504 | value = *reinterpret_cast<int32_t*>(data + 1); |
| 1505 | data += 5; |
| 1506 | break; |
| 1507 | default: |
| 1508 | UNREACHABLE(); |
| 1509 | } |
| 1510 | AppendToBuffer("test%c rax,0x%"V8_PTR_PREFIX"x", |
| 1511 | operand_size_code(), |
| 1512 | value); |
| 1513 | break; |
| 1514 | } |
| 1515 | case 0xD1: // fall through |
| 1516 | case 0xD3: // fall through |
| 1517 | case 0xC1: |
| 1518 | data += ShiftInstruction(data); |
| 1519 | break; |
| 1520 | case 0xD0: // fall through |
| 1521 | case 0xD2: // fall through |
| 1522 | case 0xC0: |
| 1523 | byte_size_operand_ = true; |
| 1524 | data += ShiftInstruction(data); |
| 1525 | break; |
| 1526 | |
| 1527 | case 0xD9: // fall through |
| 1528 | case 0xDA: // fall through |
| 1529 | case 0xDB: // fall through |
| 1530 | case 0xDC: // fall through |
| 1531 | case 0xDD: // fall through |
| 1532 | case 0xDE: // fall through |
| 1533 | case 0xDF: |
| 1534 | data += FPUInstruction(data); |
| 1535 | break; |
| 1536 | |
| 1537 | case 0xEB: |
| 1538 | data += JumpShort(data); |
| 1539 | break; |
| 1540 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1541 | case 0xF6: |
| 1542 | byte_size_operand_ = true; // fall through |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1543 | case 0xF7: |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1544 | data += F6F7Instruction(data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1545 | break; |
| 1546 | |
| 1547 | default: |
| 1548 | UnimplementedInstruction(); |
| 1549 | data += 1; |
| 1550 | } |
| 1551 | } // !processed |
| 1552 | |
| 1553 | if (tmp_buffer_pos_ < sizeof tmp_buffer_) { |
| 1554 | tmp_buffer_[tmp_buffer_pos_] = '\0'; |
| 1555 | } |
| 1556 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1557 | int instr_len = static_cast<int>(data - instr); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1558 | ASSERT(instr_len > 0); // Ensure progress. |
| 1559 | |
| 1560 | int outp = 0; |
| 1561 | // Instruction bytes. |
| 1562 | for (byte* bp = instr; bp < data; bp++) { |
| 1563 | outp += v8::internal::OS::SNPrintF(out_buffer + outp, "%02x", *bp); |
| 1564 | } |
| 1565 | for (int i = 6 - instr_len; i >= 0; i--) { |
| 1566 | outp += v8::internal::OS::SNPrintF(out_buffer + outp, " "); |
| 1567 | } |
| 1568 | |
| 1569 | outp += v8::internal::OS::SNPrintF(out_buffer + outp, " %s", |
| 1570 | tmp_buffer_.start()); |
| 1571 | return instr_len; |
| 1572 | } |
| 1573 | |
| 1574 | //------------------------------------------------------------------------------ |
| 1575 | |
| 1576 | |
| 1577 | static const char* cpu_regs[16] = { |
| 1578 | "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", |
| 1579 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
| 1580 | }; |
| 1581 | |
| 1582 | |
| 1583 | static const char* byte_cpu_regs[16] = { |
| 1584 | "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", |
| 1585 | "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l" |
| 1586 | }; |
| 1587 | |
| 1588 | |
| 1589 | static const char* xmm_regs[16] = { |
| 1590 | "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", |
| 1591 | "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15" |
| 1592 | }; |
| 1593 | |
| 1594 | |
| 1595 | const char* NameConverter::NameOfAddress(byte* addr) const { |
| 1596 | static v8::internal::EmbeddedVector<char, 32> tmp_buffer; |
| 1597 | v8::internal::OS::SNPrintF(tmp_buffer, "%p", addr); |
| 1598 | return tmp_buffer.start(); |
| 1599 | } |
| 1600 | |
| 1601 | |
| 1602 | const char* NameConverter::NameOfConstant(byte* addr) const { |
| 1603 | return NameOfAddress(addr); |
| 1604 | } |
| 1605 | |
| 1606 | |
| 1607 | const char* NameConverter::NameOfCPURegister(int reg) const { |
| 1608 | if (0 <= reg && reg < 16) |
| 1609 | return cpu_regs[reg]; |
| 1610 | return "noreg"; |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | const char* NameConverter::NameOfByteCPURegister(int reg) const { |
| 1615 | if (0 <= reg && reg < 16) |
| 1616 | return byte_cpu_regs[reg]; |
| 1617 | return "noreg"; |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | const char* NameConverter::NameOfXMMRegister(int reg) const { |
| 1622 | if (0 <= reg && reg < 16) |
| 1623 | return xmm_regs[reg]; |
| 1624 | return "noxmmreg"; |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | const char* NameConverter::NameInCode(byte* addr) const { |
| 1629 | // X64 does not embed debug strings at the moment. |
| 1630 | UNREACHABLE(); |
| 1631 | return ""; |
| 1632 | } |
| 1633 | |
| 1634 | //------------------------------------------------------------------------------ |
| 1635 | |
| 1636 | Disassembler::Disassembler(const NameConverter& converter) |
| 1637 | : converter_(converter) { } |
| 1638 | |
| 1639 | Disassembler::~Disassembler() { } |
| 1640 | |
| 1641 | |
| 1642 | int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer, |
| 1643 | byte* instruction) { |
| 1644 | DisassemblerX64 d(converter_, CONTINUE_ON_UNIMPLEMENTED_OPCODE); |
| 1645 | return d.InstructionDecode(buffer, instruction); |
| 1646 | } |
| 1647 | |
| 1648 | |
| 1649 | // The X64 assembler does not use constant pools. |
| 1650 | int Disassembler::ConstantPoolSizeAt(byte* instruction) { |
| 1651 | return -1; |
| 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) { |
| 1656 | NameConverter converter; |
| 1657 | Disassembler d(converter); |
| 1658 | for (byte* pc = begin; pc < end;) { |
| 1659 | v8::internal::EmbeddedVector<char, 128> buffer; |
| 1660 | buffer[0] = '\0'; |
| 1661 | byte* prev_pc = pc; |
| 1662 | pc += d.InstructionDecode(buffer, pc); |
| 1663 | fprintf(f, "%p", prev_pc); |
| 1664 | fprintf(f, " "); |
| 1665 | |
| 1666 | for (byte* bp = prev_pc; bp < pc; bp++) { |
| 1667 | fprintf(f, "%02x", *bp); |
| 1668 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1669 | for (int i = 6 - static_cast<int>(pc - prev_pc); i >= 0; i--) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1670 | fprintf(f, " "); |
| 1671 | } |
| 1672 | fprintf(f, " %s\n", buffer.start()); |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | } // namespace disasm |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame^] | 1677 | |
| 1678 | #endif // V8_TARGET_ARCH_X64 |