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