blob: ce3aae8a2c69a888072801f3e890217fd7be9f71 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
35namespace disasm {
36
37enum 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//------------------------------------------------------------------
51struct ByteMnemonic {
52 int b; // -1 terminates, otherwise must be in range (0..255)
53 OperandType op_order_;
54 const char* mnem;
55};
56
57
58static 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
105static 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 Clarked91b9f72010-01-27 17:25:45 +0000117 { 0xA4, UNSET_OP_ORDER, "movs" },
118 { 0xA5, UNSET_OP_ORDER, "movs" },
119 { 0xA6, UNSET_OP_ORDER, "cmps" },
120 { 0xA7, UNSET_OP_ORDER, "cmps" },
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 { -1, UNSET_OP_ORDER, "" }
122};
123
124
125static ByteMnemonic call_jump_instr[] = {
126 { 0xE8, UNSET_OP_ORDER, "call" },
127 { 0xE9, UNSET_OP_ORDER, "jmp" },
128 { -1, UNSET_OP_ORDER, "" }
129};
130
131
132static 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
145static 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
151enum 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 Clarked91b9f72010-01-27 17:25:45 +0000164enum 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 Blocka7e24c12009-10-30 11:49:00 +0000174struct InstructionDesc {
175 const char* mnem;
176 InstructionType type;
177 OperandType op_order_;
178 bool byte_size_operation; // Fixed 8-bit operation.
179};
180
181
182class 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
200InstructionTable::InstructionTable() {
201 Clear();
202 Init();
203}
204
205
206void 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
216void 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
228void 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 Blockd0582a62009-12-15 09:54:21 +0000235 ASSERT_EQ(NO_INSTR, id->type); // Information not already entered
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 id->type = type;
237 id->byte_size_operation = ((op_order & BYTE_SIZE_OPERAND_FLAG) != 0);
238 }
239}
240
241
242void 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 Blockd0582a62009-12-15 09:54:21 +0000249 ASSERT_EQ(NO_INSTR, id->type); // Information not already entered
Steve Blocka7e24c12009-10-30 11:49:00 +0000250 id->mnem = mnem;
251 id->type = type;
252 id->byte_size_operation = byte_size;
253 }
254}
255
256
257void InstructionTable::AddJumpConditionalShort() {
258 for (byte b = 0x70; b <= 0x7F; b++) {
259 InstructionDesc* id = &instructions_[b];
Steve Blockd0582a62009-12-15 09:54:21 +0000260 ASSERT_EQ(NO_INSTR, id->type); // Information not already entered
Steve Blocka7e24c12009-10-30 11:49:00 +0000261 id->mnem = NULL; // Computed depending on condition code.
262 id->type = JUMP_CONDITIONAL_SHORT_INSTR;
263 }
264}
265
266
267static InstructionTable instruction_table;
268
269static 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
291enum 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.
298class 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 Blockd0582a62009-12-15 09:54:21 +0000410 int PrintRightXMMOperand(byte* modrmp);
Steve Blocka7e24c12009-10-30 11:49:00 +0000411 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 Blockd0582a62009-12-15 09:54:21 +0000418 int F6F7Instruction(byte* data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000419 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 Blockd0582a62009-12-15 09:54:21 +0000425 int MemoryFPUInstruction(int escape_opcode, int regop, byte* modrm_start);
426 int RegisterFPUInstruction(int escape_opcode, byte modrm_byte);
Steve Blocka7e24c12009-10-30 11:49:00 +0000427 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
439void 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
449int DisassemblerX64::PrintRightOperandHelper(
450 byte* modrmp,
451 RegisterNameMapping register_name) {
452 int mod, regop, rm;
453 get_modrm(*modrmp, &mod, &regop, &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
546int 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
576int DisassemblerX64::PrintRightOperand(byte* modrmp) {
577 return PrintRightOperandHelper(modrmp,
578 &DisassemblerX64::NameOfCPURegister);
579}
580
581
582int DisassemblerX64::PrintRightByteOperand(byte* modrmp) {
583 return PrintRightOperandHelper(modrmp,
584 &DisassemblerX64::NameOfByteCPURegister);
585}
586
587
Steve Blockd0582a62009-12-15 09:54:21 +0000588int DisassemblerX64::PrintRightXMMOperand(byte* modrmp) {
589 return PrintRightOperandHelper(modrmp,
590 &DisassemblerX64::NameOfXMMRegister);
591}
592
593
Steve Blocka7e24c12009-10-30 11:49:00 +0000594// Returns number of bytes used including the current *data.
595// Writes instruction's mnemonic, left and right operands to 'tmp_buffer_'.
596int 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, &regop, &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_'.
633int 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, &regop, &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 Blockd0582a62009-12-15 09:54:21 +0000674int DisassemblerX64::F6F7Instruction(byte* data) {
675 ASSERT(*data == 0xF7 || *data == 0xF6);
Steve Blocka7e24c12009-10-30 11:49:00 +0000676 byte modrm = *(data + 1);
677 int mod, regop, rm;
678 get_modrm(modrm, &mod, &regop, &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 Blocka7e24c12009-10-30 11:49:00 +0000702 } else if (regop == 0) {
703 AppendToBuffer("test%c ", operand_size_code());
Steve Blockd0582a62009-12-15 09:54:21 +0000704 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 Blocka7e24c12009-10-30 11:49:00 +0000708 } else {
709 UnimplementedInstruction();
710 return 2;
711 }
712}
713
714
715int 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, &regop, &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 Blockd0582a62009-12-15 09:54:21 +0000758 ASSERT_NE(NULL, mnem);
Steve Blocka7e24c12009-10-30 11:49:00 +0000759 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.
780int DisassemblerX64::JumpShort(byte* data) {
Steve Blockd0582a62009-12-15 09:54:21 +0000781 ASSERT_EQ(0xEB, *data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000782 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.
790int DisassemblerX64::JumpConditional(byte* data) {
Steve Blockd0582a62009-12-15 09:54:21 +0000791 ASSERT_EQ(0x0F, *data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000792 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.
801int 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.
812int DisassemblerX64::SetCC(byte* data) {
Steve Blockd0582a62009-12-15 09:54:21 +0000813 ASSERT_EQ(0x0F, *data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000814 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.
823int DisassemblerX64::FPUInstruction(byte* data) {
Steve Blockd0582a62009-12-15 09:54:21 +0000824 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 Blocka7e24c12009-10-30 11:49:00 +0000832 }
Steve Blockd0582a62009-12-15 09:54:21 +0000833}
834
835int 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
879int 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 Blocka7e24c12009-10-30 11:49:00 +0000983 return 2;
984}
985
986
Steve Blockd0582a62009-12-15 09:54:21 +0000987
Steve Blocka7e24c12009-10-30 11:49:00 +0000988// 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.
991int 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);
996 if (opcode == 0x1F) {
997 // NOP
998 int mod, regop, rm;
999 get_modrm(*current, &mod, &regop, &rm);
1000 current++;
1001 if (regop == 4) { // SIB byte present.
1002 current++;
1003 }
1004 if (mod == 1) { // Byte displacement.
1005 current += 1;
1006 } else if (mod == 2) { // 32-bit displacement.
1007 current += 4;
1008 } // else no immediate displacement.
1009 AppendToBuffer("nop");
1010
1011 } else if (opcode == 0xA2 || opcode == 0x31) {
1012 // RDTSC or CPUID
1013 AppendToBuffer("%s", mnemonic);
1014
1015 } else if ((opcode & 0xF0) == 0x40) {
1016 // CMOVcc: conditional move.
1017 int condition = opcode & 0x0F;
1018 const InstructionDesc& idesc = cmov_instructions[condition];
1019 byte_size_operand_ = idesc.byte_size_operation;
1020 current += PrintOperands(idesc.mnem, idesc.op_order_, current);
1021
1022 } else if ((opcode & 0xF0) == 0x80) {
1023 // Jcc: Conditional jump (branch).
1024 current = data + JumpConditional(data);
1025
1026 } else if (opcode == 0xBE || opcode == 0xBF || opcode == 0xB6 ||
1027 opcode == 0xB7 || opcode == 0xAF) {
1028 // Size-extending moves, IMUL.
1029 current += PrintOperands(mnemonic, REG_OPER_OP_ORDER, current);
1030
1031 } else if ((opcode & 0xF0) == 0x90) {
1032 // SETcc: Set byte on condition. Needs pointer to beginning of instruction.
1033 current = data + SetCC(data);
1034
1035 } else if (opcode == 0xAB || opcode == 0xA5 || opcode == 0xAD) {
1036 // SHLD, SHRD (double-precision shift), BTS (bit set).
1037 AppendToBuffer("%s ", mnemonic);
1038 int mod, regop, rm;
1039 get_modrm(*current, &mod, &regop, &rm);
1040 current += PrintRightOperand(current);
1041 if (opcode == 0xAB) {
1042 AppendToBuffer(",%s", NameOfCPURegister(regop));
1043 } else {
1044 AppendToBuffer(",%s,cl", NameOfCPURegister(regop));
1045 }
1046 } else if (group_1_prefix_ == 0xF2) {
1047 // Beginning of instructions with prefix 0xF2.
1048
1049 if (opcode == 0x11 || opcode == 0x10) {
1050 // MOVSD: Move scalar double-precision fp to/from/between XMM registers.
1051 AppendToBuffer("movsd ");
1052 int mod, regop, rm;
1053 get_modrm(*current, &mod, &regop, &rm);
1054 if (opcode == 0x11) {
1055 current += PrintRightOperand(current);
1056 AppendToBuffer(",%s", NameOfXMMRegister(regop));
1057 } else {
1058 AppendToBuffer("%s,", NameOfXMMRegister(regop));
1059 current += PrintRightOperand(current);
1060 }
1061 } else if (opcode == 0x2A) {
1062 // CVTSI2SD: integer to XMM double conversion.
1063 int mod, regop, rm;
1064 get_modrm(*current, &mod, &regop, &rm);
1065 AppendToBuffer("%s %s,", mnemonic, NameOfXMMRegister(regop));
Steve Blockd0582a62009-12-15 09:54:21 +00001066 current += PrintRightOperand(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00001067 } else if ((opcode & 0xF8) == 0x58) {
1068 // XMM arithmetic. Mnemonic was retrieved at the start of this function.
1069 int mod, regop, rm;
1070 get_modrm(*current, &mod, &regop, &rm);
Steve Blockd0582a62009-12-15 09:54:21 +00001071 AppendToBuffer("%s %s,", mnemonic, NameOfXMMRegister(regop));
1072 current += PrintRightXMMOperand(current);
Steve Blocka7e24c12009-10-30 11:49:00 +00001073 } else {
1074 UnimplementedInstruction();
1075 }
1076 } else if (opcode == 0x2C && group_1_prefix_ == 0xF3) {
1077 // Instruction with prefix 0xF3.
1078
1079 // CVTTSS2SI: Convert scalar single-precision FP to dword integer.
1080 // Assert that mod is not 3, so source is memory, not an XMM register.
Steve Blockd0582a62009-12-15 09:54:21 +00001081 ASSERT_NE(0xC0, *current & 0xC0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001082 current += PrintOperands("cvttss2si", REG_OPER_OP_ORDER, current);
1083 } else {
1084 UnimplementedInstruction();
1085 }
Steve Blockd0582a62009-12-15 09:54:21 +00001086 return static_cast<int>(current - data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001087}
1088
1089
1090// Mnemonics for two-byte opcode instructions starting with 0x0F.
1091// The argument is the second byte of the two-byte opcode.
1092// Returns NULL if the instruction is not handled here.
1093const char* DisassemblerX64::TwoByteMnemonic(byte opcode) {
1094 switch (opcode) {
1095 case 0x1F:
1096 return "nop";
1097 case 0x2A: // F2 prefix.
1098 return "cvtsi2sd";
1099 case 0x31:
1100 return "rdtsc";
1101 case 0x58: // F2 prefix.
1102 return "addsd";
1103 case 0x59: // F2 prefix.
1104 return "mulsd";
1105 case 0x5C: // F2 prefix.
1106 return "subsd";
1107 case 0x5E: // F2 prefix.
1108 return "divsd";
1109 case 0xA2:
1110 return "cpuid";
1111 case 0xA5:
1112 return "shld";
1113 case 0xAB:
1114 return "bts";
1115 case 0xAD:
1116 return "shrd";
1117 case 0xAF:
1118 return "imul";
1119 case 0xB6:
1120 return "movzxb";
1121 case 0xB7:
1122 return "movzxw";
1123 case 0xBE:
1124 return "movsxb";
1125 case 0xBF:
1126 return "movsxw";
1127 default:
1128 return NULL;
1129 }
1130}
1131
1132
1133// Disassembles the instruction at instr, and writes it into out_buffer.
1134int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
1135 byte* instr) {
1136 tmp_buffer_pos_ = 0; // starting to write as position 0
1137 byte* data = instr;
1138 bool processed = true; // Will be set to false if the current instruction
1139 // is not in 'instructions' table.
1140 byte current;
1141
1142 // Scan for prefixes.
1143 while (true) {
1144 current = *data;
Leon Clarked91b9f72010-01-27 17:25:45 +00001145 if (current == OPERAND_SIZE_OVERRIDE_PREFIX) { // Group 3 prefix.
Steve Blocka7e24c12009-10-30 11:49:00 +00001146 operand_size_ = current;
1147 } else if ((current & 0xF0) == 0x40) { // REX prefix.
1148 setRex(current);
1149 if (rex_w()) AppendToBuffer("REX.W ");
Leon Clarked91b9f72010-01-27 17:25:45 +00001150 } else if ((current & 0xFE) == 0xF2) { // Group 1 prefix (0xF2 or 0xF3).
Steve Blocka7e24c12009-10-30 11:49:00 +00001151 group_1_prefix_ = current;
1152 } else { // Not a prefix - an opcode.
1153 break;
1154 }
1155 data++;
1156 }
1157
1158 const InstructionDesc& idesc = instruction_table.Get(current);
1159 byte_size_operand_ = idesc.byte_size_operation;
1160 switch (idesc.type) {
1161 case ZERO_OPERANDS_INSTR:
Leon Clarked91b9f72010-01-27 17:25:45 +00001162 if (current >= 0xA4 && current <= 0xA7) {
1163 // String move or compare operations.
1164 if (group_1_prefix_ == REP_PREFIX) {
1165 // REP.
1166 AppendToBuffer("rep ");
1167 }
1168 if (rex_w()) AppendToBuffer("REX.W ");
1169 AppendToBuffer("%s%c", idesc.mnem, operand_size_code());
1170 } else {
1171 AppendToBuffer("%s", idesc.mnem, operand_size_code());
1172 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001173 data++;
1174 break;
1175
1176 case TWO_OPERANDS_INSTR:
1177 data++;
1178 data += PrintOperands(idesc.mnem, idesc.op_order_, data);
1179 break;
1180
1181 case JUMP_CONDITIONAL_SHORT_INSTR:
1182 data += JumpConditionalShort(data);
1183 break;
1184
1185 case REGISTER_INSTR:
1186 AppendToBuffer("%s%c %s",
1187 idesc.mnem,
1188 operand_size_code(),
1189 NameOfCPURegister(base_reg(current & 0x07)));
1190 data++;
1191 break;
1192 case PUSHPOP_INSTR:
1193 AppendToBuffer("%s %s",
1194 idesc.mnem,
1195 NameOfCPURegister(base_reg(current & 0x07)));
1196 data++;
1197 break;
1198 case MOVE_REG_INSTR: {
1199 byte* addr = NULL;
1200 switch (operand_size()) {
1201 case WORD_SIZE:
1202 addr = reinterpret_cast<byte*>(*reinterpret_cast<int16_t*>(data + 1));
1203 data += 3;
1204 break;
1205 case DOUBLEWORD_SIZE:
1206 addr = reinterpret_cast<byte*>(*reinterpret_cast<int32_t*>(data + 1));
1207 data += 5;
1208 break;
1209 case QUADWORD_SIZE:
1210 addr = reinterpret_cast<byte*>(*reinterpret_cast<int64_t*>(data + 1));
1211 data += 9;
1212 break;
1213 default:
1214 UNREACHABLE();
1215 }
1216 AppendToBuffer("mov%c %s,%s",
1217 operand_size_code(),
1218 NameOfCPURegister(base_reg(current & 0x07)),
1219 NameOfAddress(addr));
1220 break;
1221 }
1222
1223 case CALL_JUMP_INSTR: {
1224 byte* addr = data + *reinterpret_cast<int32_t*>(data + 1) + 5;
1225 AppendToBuffer("%s %s", idesc.mnem, NameOfAddress(addr));
1226 data += 5;
1227 break;
1228 }
1229
1230 case SHORT_IMMEDIATE_INSTR: {
1231 byte* addr =
1232 reinterpret_cast<byte*>(*reinterpret_cast<int32_t*>(data + 1));
1233 AppendToBuffer("%s rax, %s", idesc.mnem, NameOfAddress(addr));
1234 data += 5;
1235 break;
1236 }
1237
1238 case NO_INSTR:
1239 processed = false;
1240 break;
1241
1242 default:
1243 UNIMPLEMENTED(); // This type is not implemented.
1244 }
1245
1246 // The first byte didn't match any of the simple opcodes, so we
1247 // need to do special processing on it.
1248 if (!processed) {
1249 switch (*data) {
1250 case 0xC2:
1251 AppendToBuffer("ret 0x%x", *reinterpret_cast<uint16_t*>(data + 1));
1252 data += 3;
1253 break;
1254
1255 case 0x69: // fall through
1256 case 0x6B: {
1257 int mod, regop, rm;
1258 get_modrm(*(data + 1), &mod, &regop, &rm);
1259 int32_t imm = *data == 0x6B ? *(data + 2)
1260 : *reinterpret_cast<int32_t*>(data + 2);
1261 AppendToBuffer("imul %s,%s,0x%x", NameOfCPURegister(regop),
1262 NameOfCPURegister(rm), imm);
1263 data += 2 + (*data == 0x6B ? 1 : 4);
1264 break;
1265 }
1266
Steve Blocka7e24c12009-10-30 11:49:00 +00001267 case 0x81: // fall through
1268 case 0x83: // 0x81 with sign extension bit set
1269 data += PrintImmediateOp(data);
1270 break;
1271
1272 case 0x0F:
1273 data += TwoByteOpcodeInstruction(data);
1274 break;
1275
1276 case 0x8F: {
1277 data++;
1278 int mod, regop, rm;
1279 get_modrm(*data, &mod, &regop, &rm);
1280 if (regop == 0) {
1281 AppendToBuffer("pop ");
1282 data += PrintRightOperand(data);
1283 }
1284 }
1285 break;
1286
1287 case 0xFF: {
1288 data++;
1289 int mod, regop, rm;
1290 get_modrm(*data, &mod, &regop, &rm);
1291 const char* mnem = NULL;
1292 switch (regop) {
1293 case 0:
1294 mnem = "inc";
1295 break;
1296 case 1:
1297 mnem = "dec";
1298 break;
1299 case 2:
1300 mnem = "call";
1301 break;
1302 case 4:
1303 mnem = "jmp";
1304 break;
1305 case 6:
1306 mnem = "push";
1307 break;
1308 default:
1309 mnem = "???";
1310 }
1311 AppendToBuffer(((regop <= 1) ? "%s%c " : "%s "),
1312 mnem,
1313 operand_size_code());
1314 data += PrintRightOperand(data);
1315 }
1316 break;
1317
1318 case 0xC7: // imm32, fall through
1319 case 0xC6: // imm8
1320 {
1321 bool is_byte = *data == 0xC6;
1322 data++;
1323
1324 AppendToBuffer("mov%c ", is_byte ? 'b' : operand_size_code());
1325 data += PrintRightOperand(data);
1326 int32_t imm = is_byte ? *data : *reinterpret_cast<int32_t*>(data);
1327 AppendToBuffer(",0x%x", imm);
1328 data += is_byte ? 1 : 4;
1329 }
1330 break;
1331
1332 case 0x80: {
1333 data++;
1334 AppendToBuffer("cmpb ");
1335 data += PrintRightOperand(data);
1336 int32_t imm = *data;
1337 AppendToBuffer(",0x%x", imm);
1338 data++;
1339 }
1340 break;
1341
1342 case 0x88: // 8bit, fall through
1343 case 0x89: // 32bit
1344 {
1345 bool is_byte = *data == 0x88;
1346 int mod, regop, rm;
1347 data++;
1348 get_modrm(*data, &mod, &regop, &rm);
1349 AppendToBuffer("mov%c ", is_byte ? 'b' : operand_size_code());
1350 data += PrintRightOperand(data);
1351 AppendToBuffer(",%s", NameOfCPURegister(regop));
1352 }
1353 break;
1354
1355 case 0x90:
1356 case 0x91:
1357 case 0x92:
1358 case 0x93:
1359 case 0x94:
1360 case 0x95:
1361 case 0x96:
1362 case 0x97: {
Steve Blockd0582a62009-12-15 09:54:21 +00001363 int reg = (*data & 0x7) | (rex_b() ? 8 : 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001364 if (reg == 0) {
1365 AppendToBuffer("nop"); // Common name for xchg rax,rax.
1366 } else {
1367 AppendToBuffer("xchg%c rax, %s",
1368 operand_size_code(),
1369 NameOfCPURegister(reg));
1370 }
Steve Blockd0582a62009-12-15 09:54:21 +00001371 data++;
Steve Blocka7e24c12009-10-30 11:49:00 +00001372 }
Steve Blockd0582a62009-12-15 09:54:21 +00001373 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001374
1375 case 0xFE: {
1376 data++;
1377 int mod, regop, rm;
1378 get_modrm(*data, &mod, &regop, &rm);
1379 if (mod == 3 && regop == 1) {
1380 AppendToBuffer("decb %s", NameOfCPURegister(rm));
1381 } else {
1382 UnimplementedInstruction();
1383 }
1384 data++;
1385 }
1386 break;
1387
1388 case 0x68:
1389 AppendToBuffer("push 0x%x", *reinterpret_cast<int32_t*>(data + 1));
1390 data += 5;
1391 break;
1392
1393 case 0x6A:
1394 AppendToBuffer("push 0x%x", *reinterpret_cast<int8_t*>(data + 1));
1395 data += 2;
1396 break;
1397
1398 case 0xA1: // Fall through.
1399 case 0xA3:
1400 switch (operand_size()) {
1401 case DOUBLEWORD_SIZE: {
1402 const char* memory_location = NameOfAddress(
1403 reinterpret_cast<byte*>(
1404 *reinterpret_cast<int32_t*>(data + 1)));
1405 if (*data == 0xA1) { // Opcode 0xA1
1406 AppendToBuffer("movzxlq rax,(%s)", memory_location);
1407 } else { // Opcode 0xA3
1408 AppendToBuffer("movzxlq (%s),rax", memory_location);
1409 }
1410 data += 5;
1411 break;
1412 }
1413 case QUADWORD_SIZE: {
1414 // New x64 instruction mov rax,(imm_64).
1415 const char* memory_location = NameOfAddress(
1416 *reinterpret_cast<byte**>(data + 1));
1417 if (*data == 0xA1) { // Opcode 0xA1
1418 AppendToBuffer("movq rax,(%s)", memory_location);
1419 } else { // Opcode 0xA3
1420 AppendToBuffer("movq (%s),rax", memory_location);
1421 }
1422 data += 9;
1423 break;
1424 }
1425 default:
1426 UnimplementedInstruction();
1427 data += 2;
1428 }
1429 break;
1430
1431 case 0xA8:
1432 AppendToBuffer("test al,0x%x", *reinterpret_cast<uint8_t*>(data + 1));
1433 data += 2;
1434 break;
1435
1436 case 0xA9: {
1437 int64_t value = 0;
1438 switch (operand_size()) {
1439 case WORD_SIZE:
1440 value = *reinterpret_cast<uint16_t*>(data + 1);
1441 data += 3;
1442 break;
1443 case DOUBLEWORD_SIZE:
1444 value = *reinterpret_cast<uint32_t*>(data + 1);
1445 data += 5;
1446 break;
1447 case QUADWORD_SIZE:
1448 value = *reinterpret_cast<int32_t*>(data + 1);
1449 data += 5;
1450 break;
1451 default:
1452 UNREACHABLE();
1453 }
1454 AppendToBuffer("test%c rax,0x%"V8_PTR_PREFIX"x",
1455 operand_size_code(),
1456 value);
1457 break;
1458 }
1459 case 0xD1: // fall through
1460 case 0xD3: // fall through
1461 case 0xC1:
1462 data += ShiftInstruction(data);
1463 break;
1464 case 0xD0: // fall through
1465 case 0xD2: // fall through
1466 case 0xC0:
1467 byte_size_operand_ = true;
1468 data += ShiftInstruction(data);
1469 break;
1470
1471 case 0xD9: // fall through
1472 case 0xDA: // fall through
1473 case 0xDB: // fall through
1474 case 0xDC: // fall through
1475 case 0xDD: // fall through
1476 case 0xDE: // fall through
1477 case 0xDF:
1478 data += FPUInstruction(data);
1479 break;
1480
1481 case 0xEB:
1482 data += JumpShort(data);
1483 break;
1484
Steve Blockd0582a62009-12-15 09:54:21 +00001485 case 0xF6:
1486 byte_size_operand_ = true; // fall through
Steve Blocka7e24c12009-10-30 11:49:00 +00001487 case 0xF7:
Steve Blockd0582a62009-12-15 09:54:21 +00001488 data += F6F7Instruction(data);
Steve Blocka7e24c12009-10-30 11:49:00 +00001489 break;
1490
1491 default:
1492 UnimplementedInstruction();
1493 data += 1;
1494 }
1495 } // !processed
1496
1497 if (tmp_buffer_pos_ < sizeof tmp_buffer_) {
1498 tmp_buffer_[tmp_buffer_pos_] = '\0';
1499 }
1500
Steve Blockd0582a62009-12-15 09:54:21 +00001501 int instr_len = static_cast<int>(data - instr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001502 ASSERT(instr_len > 0); // Ensure progress.
1503
1504 int outp = 0;
1505 // Instruction bytes.
1506 for (byte* bp = instr; bp < data; bp++) {
1507 outp += v8::internal::OS::SNPrintF(out_buffer + outp, "%02x", *bp);
1508 }
1509 for (int i = 6 - instr_len; i >= 0; i--) {
1510 outp += v8::internal::OS::SNPrintF(out_buffer + outp, " ");
1511 }
1512
1513 outp += v8::internal::OS::SNPrintF(out_buffer + outp, " %s",
1514 tmp_buffer_.start());
1515 return instr_len;
1516}
1517
1518//------------------------------------------------------------------------------
1519
1520
1521static const char* cpu_regs[16] = {
1522 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
1523 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
1524};
1525
1526
1527static const char* byte_cpu_regs[16] = {
1528 "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
1529 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l"
1530};
1531
1532
1533static const char* xmm_regs[16] = {
1534 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
1535 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"
1536};
1537
1538
1539const char* NameConverter::NameOfAddress(byte* addr) const {
1540 static v8::internal::EmbeddedVector<char, 32> tmp_buffer;
1541 v8::internal::OS::SNPrintF(tmp_buffer, "%p", addr);
1542 return tmp_buffer.start();
1543}
1544
1545
1546const char* NameConverter::NameOfConstant(byte* addr) const {
1547 return NameOfAddress(addr);
1548}
1549
1550
1551const char* NameConverter::NameOfCPURegister(int reg) const {
1552 if (0 <= reg && reg < 16)
1553 return cpu_regs[reg];
1554 return "noreg";
1555}
1556
1557
1558const char* NameConverter::NameOfByteCPURegister(int reg) const {
1559 if (0 <= reg && reg < 16)
1560 return byte_cpu_regs[reg];
1561 return "noreg";
1562}
1563
1564
1565const char* NameConverter::NameOfXMMRegister(int reg) const {
1566 if (0 <= reg && reg < 16)
1567 return xmm_regs[reg];
1568 return "noxmmreg";
1569}
1570
1571
1572const char* NameConverter::NameInCode(byte* addr) const {
1573 // X64 does not embed debug strings at the moment.
1574 UNREACHABLE();
1575 return "";
1576}
1577
1578//------------------------------------------------------------------------------
1579
1580Disassembler::Disassembler(const NameConverter& converter)
1581 : converter_(converter) { }
1582
1583Disassembler::~Disassembler() { }
1584
1585
1586int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
1587 byte* instruction) {
1588 DisassemblerX64 d(converter_, CONTINUE_ON_UNIMPLEMENTED_OPCODE);
1589 return d.InstructionDecode(buffer, instruction);
1590}
1591
1592
1593// The X64 assembler does not use constant pools.
1594int Disassembler::ConstantPoolSizeAt(byte* instruction) {
1595 return -1;
1596}
1597
1598
1599void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) {
1600 NameConverter converter;
1601 Disassembler d(converter);
1602 for (byte* pc = begin; pc < end;) {
1603 v8::internal::EmbeddedVector<char, 128> buffer;
1604 buffer[0] = '\0';
1605 byte* prev_pc = pc;
1606 pc += d.InstructionDecode(buffer, pc);
1607 fprintf(f, "%p", prev_pc);
1608 fprintf(f, " ");
1609
1610 for (byte* bp = prev_pc; bp < pc; bp++) {
1611 fprintf(f, "%02x", *bp);
1612 }
Steve Blockd0582a62009-12-15 09:54:21 +00001613 for (int i = 6 - static_cast<int>(pc - prev_pc); i >= 0; i--) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001614 fprintf(f, " ");
1615 }
1616 fprintf(f, " %s\n", buffer.start());
1617 }
1618}
1619
1620} // namespace disasm