Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / v8 / src / x64 / disasm-x64.cc
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <assert.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8
9 #include "src/v8.h"
10
11 #if V8_TARGET_ARCH_X64
12
13 #include "src/base/lazy-instance.h"
14 #include "src/disasm.h"
15
16 namespace disasm {
17
18 enum OperandType {
19   UNSET_OP_ORDER = 0,
20   // Operand size decides between 16, 32 and 64 bit operands.
21   REG_OPER_OP_ORDER = 1,  // Register destination, operand source.
22   OPER_REG_OP_ORDER = 2,  // Operand destination, register source.
23   // Fixed 8-bit operands.
24   BYTE_SIZE_OPERAND_FLAG = 4,
25   BYTE_REG_OPER_OP_ORDER = REG_OPER_OP_ORDER | BYTE_SIZE_OPERAND_FLAG,
26   BYTE_OPER_REG_OP_ORDER = OPER_REG_OP_ORDER | BYTE_SIZE_OPERAND_FLAG
27 };
28
29
30 //------------------------------------------------------------------
31 // Tables
32 //------------------------------------------------------------------
33 struct ByteMnemonic {
34   int b;  // -1 terminates, otherwise must be in range (0..255)
35   OperandType op_order_;
36   const char* mnem;
37 };
38
39
40 static const ByteMnemonic two_operands_instr[] = {
41   { 0x00, BYTE_OPER_REG_OP_ORDER, "add" },
42   { 0x01, OPER_REG_OP_ORDER,      "add" },
43   { 0x02, BYTE_REG_OPER_OP_ORDER, "add" },
44   { 0x03, REG_OPER_OP_ORDER,      "add" },
45   { 0x08, BYTE_OPER_REG_OP_ORDER, "or" },
46   { 0x09, OPER_REG_OP_ORDER,      "or" },
47   { 0x0A, BYTE_REG_OPER_OP_ORDER, "or" },
48   { 0x0B, REG_OPER_OP_ORDER,      "or" },
49   { 0x10, BYTE_OPER_REG_OP_ORDER, "adc" },
50   { 0x11, OPER_REG_OP_ORDER,      "adc" },
51   { 0x12, BYTE_REG_OPER_OP_ORDER, "adc" },
52   { 0x13, REG_OPER_OP_ORDER,      "adc" },
53   { 0x18, BYTE_OPER_REG_OP_ORDER, "sbb" },
54   { 0x19, OPER_REG_OP_ORDER,      "sbb" },
55   { 0x1A, BYTE_REG_OPER_OP_ORDER, "sbb" },
56   { 0x1B, REG_OPER_OP_ORDER,      "sbb" },
57   { 0x20, BYTE_OPER_REG_OP_ORDER, "and" },
58   { 0x21, OPER_REG_OP_ORDER,      "and" },
59   { 0x22, BYTE_REG_OPER_OP_ORDER, "and" },
60   { 0x23, REG_OPER_OP_ORDER,      "and" },
61   { 0x28, BYTE_OPER_REG_OP_ORDER, "sub" },
62   { 0x29, OPER_REG_OP_ORDER,      "sub" },
63   { 0x2A, BYTE_REG_OPER_OP_ORDER, "sub" },
64   { 0x2B, REG_OPER_OP_ORDER,      "sub" },
65   { 0x30, BYTE_OPER_REG_OP_ORDER, "xor" },
66   { 0x31, OPER_REG_OP_ORDER,      "xor" },
67   { 0x32, BYTE_REG_OPER_OP_ORDER, "xor" },
68   { 0x33, REG_OPER_OP_ORDER,      "xor" },
69   { 0x38, BYTE_OPER_REG_OP_ORDER, "cmp" },
70   { 0x39, OPER_REG_OP_ORDER,      "cmp" },
71   { 0x3A, BYTE_REG_OPER_OP_ORDER, "cmp" },
72   { 0x3B, REG_OPER_OP_ORDER,      "cmp" },
73   { 0x63, REG_OPER_OP_ORDER,      "movsxl" },
74   { 0x84, BYTE_REG_OPER_OP_ORDER, "test" },
75   { 0x85, REG_OPER_OP_ORDER,      "test" },
76   { 0x86, BYTE_REG_OPER_OP_ORDER, "xchg" },
77   { 0x87, REG_OPER_OP_ORDER,      "xchg" },
78   { 0x88, BYTE_OPER_REG_OP_ORDER, "mov" },
79   { 0x89, OPER_REG_OP_ORDER,      "mov" },
80   { 0x8A, BYTE_REG_OPER_OP_ORDER, "mov" },
81   { 0x8B, REG_OPER_OP_ORDER,      "mov" },
82   { 0x8D, REG_OPER_OP_ORDER,      "lea" },
83   { -1, UNSET_OP_ORDER, "" }
84 };
85
86
87 static const ByteMnemonic zero_operands_instr[] = {
88   { 0xC3, UNSET_OP_ORDER, "ret" },
89   { 0xC9, UNSET_OP_ORDER, "leave" },
90   { 0xF4, UNSET_OP_ORDER, "hlt" },
91   { 0xFC, UNSET_OP_ORDER, "cld" },
92   { 0xCC, UNSET_OP_ORDER, "int3" },
93   { 0x60, UNSET_OP_ORDER, "pushad" },
94   { 0x61, UNSET_OP_ORDER, "popad" },
95   { 0x9C, UNSET_OP_ORDER, "pushfd" },
96   { 0x9D, UNSET_OP_ORDER, "popfd" },
97   { 0x9E, UNSET_OP_ORDER, "sahf" },
98   { 0x99, UNSET_OP_ORDER, "cdq" },
99   { 0x9B, UNSET_OP_ORDER, "fwait" },
100   { 0xA4, UNSET_OP_ORDER, "movs" },
101   { 0xA5, UNSET_OP_ORDER, "movs" },
102   { 0xA6, UNSET_OP_ORDER, "cmps" },
103   { 0xA7, UNSET_OP_ORDER, "cmps" },
104   { -1, UNSET_OP_ORDER, "" }
105 };
106
107
108 static const ByteMnemonic call_jump_instr[] = {
109   { 0xE8, UNSET_OP_ORDER, "call" },
110   { 0xE9, UNSET_OP_ORDER, "jmp" },
111   { -1, UNSET_OP_ORDER, "" }
112 };
113
114
115 static const ByteMnemonic short_immediate_instr[] = {
116   { 0x05, UNSET_OP_ORDER, "add" },
117   { 0x0D, UNSET_OP_ORDER, "or" },
118   { 0x15, UNSET_OP_ORDER, "adc" },
119   { 0x1D, UNSET_OP_ORDER, "sbb" },
120   { 0x25, UNSET_OP_ORDER, "and" },
121   { 0x2D, UNSET_OP_ORDER, "sub" },
122   { 0x35, UNSET_OP_ORDER, "xor" },
123   { 0x3D, UNSET_OP_ORDER, "cmp" },
124   { -1, UNSET_OP_ORDER, "" }
125 };
126
127
128 static const char* const conditional_code_suffix[] = {
129   "o", "no", "c", "nc", "z", "nz", "na", "a",
130   "s", "ns", "pe", "po", "l", "ge", "le", "g"
131 };
132
133
134 enum InstructionType {
135   NO_INSTR,
136   ZERO_OPERANDS_INSTR,
137   TWO_OPERANDS_INSTR,
138   JUMP_CONDITIONAL_SHORT_INSTR,
139   REGISTER_INSTR,
140   PUSHPOP_INSTR,  // Has implicit 64-bit operand size.
141   MOVE_REG_INSTR,
142   CALL_JUMP_INSTR,
143   SHORT_IMMEDIATE_INSTR
144 };
145
146
147 enum Prefixes {
148   ESCAPE_PREFIX = 0x0F,
149   OPERAND_SIZE_OVERRIDE_PREFIX = 0x66,
150   ADDRESS_SIZE_OVERRIDE_PREFIX = 0x67,
151   REPNE_PREFIX = 0xF2,
152   REP_PREFIX = 0xF3,
153   REPEQ_PREFIX = REP_PREFIX
154 };
155
156
157 struct InstructionDesc {
158   const char* mnem;
159   InstructionType type;
160   OperandType op_order_;
161   bool byte_size_operation;  // Fixed 8-bit operation.
162 };
163
164
165 class InstructionTable {
166  public:
167   InstructionTable();
168   const InstructionDesc& Get(byte x) const {
169     return instructions_[x];
170   }
171
172  private:
173   InstructionDesc instructions_[256];
174   void Clear();
175   void Init();
176   void CopyTable(const ByteMnemonic bm[], InstructionType type);
177   void SetTableRange(InstructionType type, byte start, byte end, bool byte_size,
178                      const char* mnem);
179   void AddJumpConditionalShort();
180 };
181
182
183 InstructionTable::InstructionTable() {
184   Clear();
185   Init();
186 }
187
188
189 void InstructionTable::Clear() {
190   for (int i = 0; i < 256; i++) {
191     instructions_[i].mnem = "(bad)";
192     instructions_[i].type = NO_INSTR;
193     instructions_[i].op_order_ = UNSET_OP_ORDER;
194     instructions_[i].byte_size_operation = false;
195   }
196 }
197
198
199 void InstructionTable::Init() {
200   CopyTable(two_operands_instr, TWO_OPERANDS_INSTR);
201   CopyTable(zero_operands_instr, ZERO_OPERANDS_INSTR);
202   CopyTable(call_jump_instr, CALL_JUMP_INSTR);
203   CopyTable(short_immediate_instr, SHORT_IMMEDIATE_INSTR);
204   AddJumpConditionalShort();
205   SetTableRange(PUSHPOP_INSTR, 0x50, 0x57, false, "push");
206   SetTableRange(PUSHPOP_INSTR, 0x58, 0x5F, false, "pop");
207   SetTableRange(MOVE_REG_INSTR, 0xB8, 0xBF, false, "mov");
208 }
209
210
211 void InstructionTable::CopyTable(const ByteMnemonic bm[],
212                                  InstructionType type) {
213   for (int i = 0; bm[i].b >= 0; i++) {
214     InstructionDesc* id = &instructions_[bm[i].b];
215     id->mnem = bm[i].mnem;
216     OperandType op_order = bm[i].op_order_;
217     id->op_order_ =
218         static_cast<OperandType>(op_order & ~BYTE_SIZE_OPERAND_FLAG);
219     DCHECK_EQ(NO_INSTR, id->type);  // Information not already entered
220     id->type = type;
221     id->byte_size_operation = ((op_order & BYTE_SIZE_OPERAND_FLAG) != 0);
222   }
223 }
224
225
226 void InstructionTable::SetTableRange(InstructionType type,
227                                      byte start,
228                                      byte end,
229                                      bool byte_size,
230                                      const char* mnem) {
231   for (byte b = start; b <= end; b++) {
232     InstructionDesc* id = &instructions_[b];
233     DCHECK_EQ(NO_INSTR, id->type);  // Information not already entered
234     id->mnem = mnem;
235     id->type = type;
236     id->byte_size_operation = byte_size;
237   }
238 }
239
240
241 void InstructionTable::AddJumpConditionalShort() {
242   for (byte b = 0x70; b <= 0x7F; b++) {
243     InstructionDesc* id = &instructions_[b];
244     DCHECK_EQ(NO_INSTR, id->type);  // Information not already entered
245     id->mnem = NULL;  // Computed depending on condition code.
246     id->type = JUMP_CONDITIONAL_SHORT_INSTR;
247   }
248 }
249
250
251 static v8::base::LazyInstance<InstructionTable>::type instruction_table =
252     LAZY_INSTANCE_INITIALIZER;
253
254
255 static InstructionDesc cmov_instructions[16] = {
256   {"cmovo", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
257   {"cmovno", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
258   {"cmovc", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
259   {"cmovnc", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
260   {"cmovz", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
261   {"cmovnz", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
262   {"cmovna", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
263   {"cmova", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
264   {"cmovs", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
265   {"cmovns", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
266   {"cmovpe", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
267   {"cmovpo", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
268   {"cmovl", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
269   {"cmovge", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
270   {"cmovle", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
271   {"cmovg", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}
272 };
273
274
275 //------------------------------------------------------------------------------
276 // DisassemblerX64 implementation.
277
278 enum UnimplementedOpcodeAction {
279   CONTINUE_ON_UNIMPLEMENTED_OPCODE,
280   ABORT_ON_UNIMPLEMENTED_OPCODE
281 };
282
283
284 // A new DisassemblerX64 object is created to disassemble each instruction.
285 // The object can only disassemble a single instruction.
286 class DisassemblerX64 {
287  public:
288   DisassemblerX64(const NameConverter& converter,
289                   UnimplementedOpcodeAction unimplemented_action =
290                       ABORT_ON_UNIMPLEMENTED_OPCODE)
291       : converter_(converter),
292         tmp_buffer_pos_(0),
293         abort_on_unimplemented_(
294             unimplemented_action == ABORT_ON_UNIMPLEMENTED_OPCODE),
295         rex_(0),
296         operand_size_(0),
297         group_1_prefix_(0),
298         byte_size_operand_(false),
299         instruction_table_(instruction_table.Pointer()) {
300     tmp_buffer_[0] = '\0';
301   }
302
303   virtual ~DisassemblerX64() {
304   }
305
306   // Writes one disassembled instruction into 'buffer' (0-terminated).
307   // Returns the length of the disassembled machine instruction in bytes.
308   int InstructionDecode(v8::internal::Vector<char> buffer, byte* instruction);
309
310  private:
311   enum OperandSize {
312     OPERAND_BYTE_SIZE = 0,
313     OPERAND_WORD_SIZE = 1,
314     OPERAND_DOUBLEWORD_SIZE = 2,
315     OPERAND_QUADWORD_SIZE = 3
316   };
317
318   enum {
319     rax = 0,
320     rcx = 1,
321     rdx = 2,
322     rbx = 3,
323     rsp = 4,
324     rbp = 5,
325     rsi = 6,
326     rdi = 7
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   const InstructionTable* const instruction_table_;
340
341   void setRex(byte rex) {
342     DCHECK_EQ(0x40, rex & 0xF0);
343     rex_ = rex;
344   }
345
346   bool rex() { return rex_ != 0; }
347
348   bool rex_b() { return (rex_ & 0x01) != 0; }
349
350   // Actual number of base register given the low bits and the rex.b state.
351   int base_reg(int low_bits) { return low_bits | ((rex_ & 0x01) << 3); }
352
353   bool rex_x() { return (rex_ & 0x02) != 0; }
354
355   bool rex_r() { return (rex_ & 0x04) != 0; }
356
357   bool rex_w() { return (rex_ & 0x08) != 0; }
358
359   OperandSize operand_size() {
360     if (byte_size_operand_) return OPERAND_BYTE_SIZE;
361     if (rex_w()) return OPERAND_QUADWORD_SIZE;
362     if (operand_size_ != 0) return OPERAND_WORD_SIZE;
363     return OPERAND_DOUBLEWORD_SIZE;
364   }
365
366   char operand_size_code() {
367     return "bwlq"[operand_size()];
368   }
369
370   const char* NameOfCPURegister(int reg) const {
371     return converter_.NameOfCPURegister(reg);
372   }
373
374   const char* NameOfByteCPURegister(int reg) const {
375     return converter_.NameOfByteCPURegister(reg);
376   }
377
378   const char* NameOfXMMRegister(int reg) const {
379     return converter_.NameOfXMMRegister(reg);
380   }
381
382   const char* NameOfAddress(byte* addr) const {
383     return converter_.NameOfAddress(addr);
384   }
385
386   // Disassembler helper functions.
387   void get_modrm(byte data,
388                  int* mod,
389                  int* regop,
390                  int* rm) {
391     *mod = (data >> 6) & 3;
392     *regop = ((data & 0x38) >> 3) | (rex_r() ? 8 : 0);
393     *rm = (data & 7) | (rex_b() ? 8 : 0);
394   }
395
396   void get_sib(byte data,
397                int* scale,
398                int* index,
399                int* base) {
400     *scale = (data >> 6) & 3;
401     *index = ((data >> 3) & 7) | (rex_x() ? 8 : 0);
402     *base = (data & 7) | (rex_b() ? 8 : 0);
403   }
404
405   typedef const char* (DisassemblerX64::*RegisterNameMapping)(int reg) const;
406
407   int PrintRightOperandHelper(byte* modrmp,
408                               RegisterNameMapping register_name);
409   int PrintRightOperand(byte* modrmp);
410   int PrintRightByteOperand(byte* modrmp);
411   int PrintRightXMMOperand(byte* modrmp);
412   int PrintOperands(const char* mnem,
413                     OperandType op_order,
414                     byte* data);
415   int PrintImmediate(byte* data, OperandSize size);
416   int PrintImmediateOp(byte* data);
417   const char* TwoByteMnemonic(byte opcode);
418   int TwoByteOpcodeInstruction(byte* data);
419   int F6F7Instruction(byte* data);
420   int ShiftInstruction(byte* data);
421   int JumpShort(byte* data);
422   int JumpConditional(byte* data);
423   int JumpConditionalShort(byte* data);
424   int SetCC(byte* data);
425   int FPUInstruction(byte* data);
426   int MemoryFPUInstruction(int escape_opcode, int regop, byte* modrm_start);
427   int RegisterFPUInstruction(int escape_opcode, byte modrm_byte);
428   void AppendToBuffer(const char* format, ...);
429
430   void UnimplementedInstruction() {
431     if (abort_on_unimplemented_) {
432       CHECK(false);
433     } else {
434       AppendToBuffer("'Unimplemented Instruction'");
435     }
436   }
437 };
438
439
440 void DisassemblerX64::AppendToBuffer(const char* format, ...) {
441   v8::internal::Vector<char> buf = tmp_buffer_ + tmp_buffer_pos_;
442   va_list args;
443   va_start(args, format);
444   int result = v8::internal::VSNPrintF(buf, format, args);
445   va_end(args);
446   tmp_buffer_pos_ += result;
447 }
448
449
450 int DisassemblerX64::PrintRightOperandHelper(
451     byte* modrmp,
452     RegisterNameMapping direct_register_name) {
453   int mod, regop, rm;
454   get_modrm(*modrmp, &mod, &regop, &rm);
455   RegisterNameMapping register_name = (mod == 3) ? direct_register_name :
456       &DisassemblerX64::NameOfCPURegister;
457   switch (mod) {
458     case 0:
459       if ((rm & 7) == 5) {
460         int32_t disp = *reinterpret_cast<int32_t*>(modrmp + 1);
461         AppendToBuffer("[0x%x]", disp);
462         return 5;
463       } else if ((rm & 7) == 4) {
464         // Codes for SIB byte.
465         byte sib = *(modrmp + 1);
466         int scale, index, base;
467         get_sib(sib, &scale, &index, &base);
468         if (index == 4 && (base & 7) == 4 && scale == 0 /*times_1*/) {
469           // index == rsp means no index. Only use sib byte with no index for
470           // rsp and r12 base.
471           AppendToBuffer("[%s]", NameOfCPURegister(base));
472           return 2;
473         } else if (base == 5) {
474           // base == rbp means no base register (when mod == 0).
475           int32_t disp = *reinterpret_cast<int32_t*>(modrmp + 2);
476           AppendToBuffer("[%s*%d%s0x%x]",
477                          NameOfCPURegister(index),
478                          1 << scale,
479                          disp < 0 ? "-" : "+",
480                          disp < 0 ? -disp : disp);
481           return 6;
482         } else if (index != 4 && base != 5) {
483           // [base+index*scale]
484           AppendToBuffer("[%s+%s*%d]",
485                          NameOfCPURegister(base),
486                          NameOfCPURegister(index),
487                          1 << scale);
488           return 2;
489         } else {
490           UnimplementedInstruction();
491           return 1;
492         }
493       } else {
494         AppendToBuffer("[%s]", NameOfCPURegister(rm));
495         return 1;
496       }
497       break;
498     case 1:  // fall through
499     case 2:
500       if ((rm & 7) == 4) {
501         byte sib = *(modrmp + 1);
502         int scale, index, base;
503         get_sib(sib, &scale, &index, &base);
504         int disp = (mod == 2) ? *reinterpret_cast<int32_t*>(modrmp + 2)
505                               : *reinterpret_cast<int8_t*>(modrmp + 2);
506         if (index == 4 && (base & 7) == 4 && scale == 0 /*times_1*/) {
507           AppendToBuffer("[%s%s0x%x]",
508                          NameOfCPURegister(base),
509                          disp < 0 ? "-" : "+",
510                          disp < 0 ? -disp : disp);
511         } else {
512           AppendToBuffer("[%s+%s*%d%s0x%x]",
513                          NameOfCPURegister(base),
514                          NameOfCPURegister(index),
515                          1 << scale,
516                          disp < 0 ? "-" : "+",
517                          disp < 0 ? -disp : disp);
518         }
519         return mod == 2 ? 6 : 3;
520       } else {
521         // No sib.
522         int disp = (mod == 2) ? *reinterpret_cast<int32_t*>(modrmp + 1)
523                               : *reinterpret_cast<int8_t*>(modrmp + 1);
524         AppendToBuffer("[%s%s0x%x]",
525                        NameOfCPURegister(rm),
526                        disp < 0 ? "-" : "+",
527                        disp < 0 ? -disp : disp);
528         return (mod == 2) ? 5 : 2;
529       }
530       break;
531     case 3:
532       AppendToBuffer("%s", (this->*register_name)(rm));
533       return 1;
534     default:
535       UnimplementedInstruction();
536       return 1;
537   }
538   UNREACHABLE();
539 }
540
541
542 int DisassemblerX64::PrintImmediate(byte* data, OperandSize size) {
543   int64_t value;
544   int count;
545   switch (size) {
546     case OPERAND_BYTE_SIZE:
547       value = *data;
548       count = 1;
549       break;
550     case OPERAND_WORD_SIZE:
551       value = *reinterpret_cast<int16_t*>(data);
552       count = 2;
553       break;
554     case OPERAND_DOUBLEWORD_SIZE:
555       value = *reinterpret_cast<uint32_t*>(data);
556       count = 4;
557       break;
558     case OPERAND_QUADWORD_SIZE:
559       value = *reinterpret_cast<int32_t*>(data);
560       count = 4;
561       break;
562     default:
563       UNREACHABLE();
564       value = 0;  // Initialize variables on all paths to satisfy the compiler.
565       count = 0;
566   }
567   AppendToBuffer("%" V8_PTR_PREFIX "x", value);
568   return count;
569 }
570
571
572 int DisassemblerX64::PrintRightOperand(byte* modrmp) {
573   return PrintRightOperandHelper(modrmp,
574                                  &DisassemblerX64::NameOfCPURegister);
575 }
576
577
578 int DisassemblerX64::PrintRightByteOperand(byte* modrmp) {
579   return PrintRightOperandHelper(modrmp,
580                                  &DisassemblerX64::NameOfByteCPURegister);
581 }
582
583
584 int DisassemblerX64::PrintRightXMMOperand(byte* modrmp) {
585   return PrintRightOperandHelper(modrmp,
586                                  &DisassemblerX64::NameOfXMMRegister);
587 }
588
589
590 // Returns number of bytes used including the current *data.
591 // Writes instruction's mnemonic, left and right operands to 'tmp_buffer_'.
592 int DisassemblerX64::PrintOperands(const char* mnem,
593                                    OperandType op_order,
594                                    byte* data) {
595   byte modrm = *data;
596   int mod, regop, rm;
597   get_modrm(modrm, &mod, &regop, &rm);
598   int advance = 0;
599   const char* register_name =
600       byte_size_operand_ ? NameOfByteCPURegister(regop)
601                          : NameOfCPURegister(regop);
602   switch (op_order) {
603     case REG_OPER_OP_ORDER: {
604       AppendToBuffer("%s%c %s,",
605                      mnem,
606                      operand_size_code(),
607                      register_name);
608       advance = byte_size_operand_ ? PrintRightByteOperand(data)
609                                    : PrintRightOperand(data);
610       break;
611     }
612     case OPER_REG_OP_ORDER: {
613       AppendToBuffer("%s%c ", mnem, operand_size_code());
614       advance = byte_size_operand_ ? PrintRightByteOperand(data)
615                                    : PrintRightOperand(data);
616       AppendToBuffer(",%s", register_name);
617       break;
618     }
619     default:
620       UNREACHABLE();
621       break;
622   }
623   return advance;
624 }
625
626
627 // Returns number of bytes used by machine instruction, including *data byte.
628 // Writes immediate instructions to 'tmp_buffer_'.
629 int DisassemblerX64::PrintImmediateOp(byte* data) {
630   bool byte_size_immediate = (*data & 0x02) != 0;
631   byte modrm = *(data + 1);
632   int mod, regop, rm;
633   get_modrm(modrm, &mod, &regop, &rm);
634   const char* mnem = "Imm???";
635   switch (regop) {
636     case 0:
637       mnem = "add";
638       break;
639     case 1:
640       mnem = "or";
641       break;
642     case 2:
643       mnem = "adc";
644       break;
645     case 3:
646       mnem = "sbb";
647       break;
648     case 4:
649       mnem = "and";
650       break;
651     case 5:
652       mnem = "sub";
653       break;
654     case 6:
655       mnem = "xor";
656       break;
657     case 7:
658       mnem = "cmp";
659       break;
660     default:
661       UnimplementedInstruction();
662   }
663   AppendToBuffer("%s%c ", mnem, operand_size_code());
664   int count = PrintRightOperand(data + 1);
665   AppendToBuffer(",0x");
666   OperandSize immediate_size =
667       byte_size_immediate ? OPERAND_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.
674 int DisassemblerX64::F6F7Instruction(byte* data) {
675   DCHECK(*data == 0xF7 || *data == 0xF6);
676   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 5:
692         mnem = "imul";
693         break;
694       case 6:
695         mnem = "div";
696         break;
697       case 7:
698         mnem = "idiv";
699         break;
700       default:
701         UnimplementedInstruction();
702     }
703     AppendToBuffer("%s%c %s",
704                    mnem,
705                    operand_size_code(),
706                    NameOfCPURegister(rm));
707     return 2;
708   } else if (regop == 0) {
709     AppendToBuffer("test%c ", operand_size_code());
710     int count = PrintRightOperand(data + 1);  // Use name of 64-bit register.
711     AppendToBuffer(",0x");
712     count += PrintImmediate(data + 1 + count, operand_size());
713     return 1 + count;
714   } else {
715     UnimplementedInstruction();
716     return 2;
717   }
718 }
719
720
721 int DisassemblerX64::ShiftInstruction(byte* data) {
722   byte op = *data & (~1);
723   if (op != 0xD0 && op != 0xD2 && op != 0xC0) {
724     UnimplementedInstruction();
725     return 1;
726   }
727   byte modrm = *(data + 1);
728   int mod, regop, rm;
729   get_modrm(modrm, &mod, &regop, &rm);
730   regop &= 0x7;  // The REX.R bit does not affect the operation.
731   int imm8 = -1;
732   int num_bytes = 2;
733   if (mod != 3) {
734     UnimplementedInstruction();
735     return num_bytes;
736   }
737   const char* mnem = NULL;
738   switch (regop) {
739     case 0:
740       mnem = "rol";
741       break;
742     case 1:
743       mnem = "ror";
744       break;
745     case 2:
746       mnem = "rcl";
747       break;
748     case 3:
749       mnem = "rcr";
750       break;
751     case 4:
752       mnem = "shl";
753       break;
754     case 5:
755       mnem = "shr";
756       break;
757     case 7:
758       mnem = "sar";
759       break;
760     default:
761       UnimplementedInstruction();
762       return num_bytes;
763   }
764   DCHECK_NE(NULL, mnem);
765   if (op == 0xD0) {
766     imm8 = 1;
767   } else if (op == 0xC0) {
768     imm8 = *(data + 2);
769     num_bytes = 3;
770   }
771   AppendToBuffer("%s%c %s,",
772                  mnem,
773                  operand_size_code(),
774                  byte_size_operand_ ? NameOfByteCPURegister(rm)
775                                     : NameOfCPURegister(rm));
776   if (op == 0xD2) {
777     AppendToBuffer("cl");
778   } else {
779     AppendToBuffer("%d", imm8);
780   }
781   return num_bytes;
782 }
783
784
785 // Returns number of bytes used, including *data.
786 int DisassemblerX64::JumpShort(byte* data) {
787   DCHECK_EQ(0xEB, *data);
788   byte b = *(data + 1);
789   byte* dest = data + static_cast<int8_t>(b) + 2;
790   AppendToBuffer("jmp %s", NameOfAddress(dest));
791   return 2;
792 }
793
794
795 // Returns number of bytes used, including *data.
796 int DisassemblerX64::JumpConditional(byte* data) {
797   DCHECK_EQ(0x0F, *data);
798   byte cond = *(data + 1) & 0x0F;
799   byte* dest = data + *reinterpret_cast<int32_t*>(data + 2) + 6;
800   const char* mnem = conditional_code_suffix[cond];
801   AppendToBuffer("j%s %s", mnem, NameOfAddress(dest));
802   return 6;  // includes 0x0F
803 }
804
805
806 // Returns number of bytes used, including *data.
807 int DisassemblerX64::JumpConditionalShort(byte* data) {
808   byte cond = *data & 0x0F;
809   byte b = *(data + 1);
810   byte* dest = data + static_cast<int8_t>(b) + 2;
811   const char* mnem = conditional_code_suffix[cond];
812   AppendToBuffer("j%s %s", mnem, NameOfAddress(dest));
813   return 2;
814 }
815
816
817 // Returns number of bytes used, including *data.
818 int DisassemblerX64::SetCC(byte* data) {
819   DCHECK_EQ(0x0F, *data);
820   byte cond = *(data + 1) & 0x0F;
821   const char* mnem = conditional_code_suffix[cond];
822   AppendToBuffer("set%s%c ", mnem, operand_size_code());
823   PrintRightByteOperand(data + 2);
824   return 3;  // includes 0x0F
825 }
826
827
828 // Returns number of bytes used, including *data.
829 int DisassemblerX64::FPUInstruction(byte* data) {
830   byte escape_opcode = *data;
831   DCHECK_EQ(0xD8, escape_opcode & 0xF8);
832   byte modrm_byte = *(data+1);
833
834   if (modrm_byte >= 0xC0) {
835     return RegisterFPUInstruction(escape_opcode, modrm_byte);
836   } else {
837     return MemoryFPUInstruction(escape_opcode, modrm_byte, data+1);
838   }
839 }
840
841 int DisassemblerX64::MemoryFPUInstruction(int escape_opcode,
842                                            int modrm_byte,
843                                            byte* modrm_start) {
844   const char* mnem = "?";
845   int regop = (modrm_byte >> 3) & 0x7;  // reg/op field of modrm byte.
846   switch (escape_opcode) {
847     case 0xD9: switch (regop) {
848         case 0: mnem = "fld_s"; break;
849         case 3: mnem = "fstp_s"; break;
850         case 7: mnem = "fstcw"; break;
851         default: UnimplementedInstruction();
852       }
853       break;
854
855     case 0xDB: switch (regop) {
856         case 0: mnem = "fild_s"; break;
857         case 1: mnem = "fisttp_s"; break;
858         case 2: mnem = "fist_s"; break;
859         case 3: mnem = "fistp_s"; break;
860         default: UnimplementedInstruction();
861       }
862       break;
863
864     case 0xDD: switch (regop) {
865         case 0: mnem = "fld_d"; break;
866         case 3: mnem = "fstp_d"; break;
867         default: UnimplementedInstruction();
868       }
869       break;
870
871     case 0xDF: switch (regop) {
872         case 5: mnem = "fild_d"; break;
873         case 7: mnem = "fistp_d"; break;
874         default: UnimplementedInstruction();
875       }
876       break;
877
878     default: UnimplementedInstruction();
879   }
880   AppendToBuffer("%s ", mnem);
881   int count = PrintRightOperand(modrm_start);
882   return count + 1;
883 }
884
885 int DisassemblerX64::RegisterFPUInstruction(int escape_opcode,
886                                              byte modrm_byte) {
887   bool has_register = false;  // Is the FPU register encoded in modrm_byte?
888   const char* mnem = "?";
889
890   switch (escape_opcode) {
891     case 0xD8:
892       UnimplementedInstruction();
893       break;
894
895     case 0xD9:
896       switch (modrm_byte & 0xF8) {
897         case 0xC0:
898           mnem = "fld";
899           has_register = true;
900           break;
901         case 0xC8:
902           mnem = "fxch";
903           has_register = true;
904           break;
905         default:
906           switch (modrm_byte) {
907             case 0xE0: mnem = "fchs"; break;
908             case 0xE1: mnem = "fabs"; break;
909             case 0xE3: mnem = "fninit"; break;
910             case 0xE4: mnem = "ftst"; break;
911             case 0xE8: mnem = "fld1"; break;
912             case 0xEB: mnem = "fldpi"; break;
913             case 0xED: mnem = "fldln2"; break;
914             case 0xEE: mnem = "fldz"; break;
915             case 0xF0: mnem = "f2xm1"; break;
916             case 0xF1: mnem = "fyl2x"; break;
917             case 0xF2: mnem = "fptan"; break;
918             case 0xF5: mnem = "fprem1"; break;
919             case 0xF7: mnem = "fincstp"; break;
920             case 0xF8: mnem = "fprem"; break;
921             case 0xFC: mnem = "frndint"; break;
922             case 0xFD: mnem = "fscale"; break;
923             case 0xFE: mnem = "fsin"; break;
924             case 0xFF: mnem = "fcos"; break;
925             default: UnimplementedInstruction();
926           }
927       }
928       break;
929
930     case 0xDA:
931       if (modrm_byte == 0xE9) {
932         mnem = "fucompp";
933       } else {
934         UnimplementedInstruction();
935       }
936       break;
937
938     case 0xDB:
939       if ((modrm_byte & 0xF8) == 0xE8) {
940         mnem = "fucomi";
941         has_register = true;
942       } else if (modrm_byte  == 0xE2) {
943         mnem = "fclex";
944       } else if (modrm_byte == 0xE3) {
945         mnem = "fninit";
946       } else {
947         UnimplementedInstruction();
948       }
949       break;
950
951     case 0xDC:
952       has_register = true;
953       switch (modrm_byte & 0xF8) {
954         case 0xC0: mnem = "fadd"; break;
955         case 0xE8: mnem = "fsub"; break;
956         case 0xC8: mnem = "fmul"; break;
957         case 0xF8: mnem = "fdiv"; break;
958         default: UnimplementedInstruction();
959       }
960       break;
961
962     case 0xDD:
963       has_register = true;
964       switch (modrm_byte & 0xF8) {
965         case 0xC0: mnem = "ffree"; break;
966         case 0xD8: mnem = "fstp"; break;
967         default: UnimplementedInstruction();
968       }
969       break;
970
971     case 0xDE:
972       if (modrm_byte  == 0xD9) {
973         mnem = "fcompp";
974       } else {
975         has_register = true;
976         switch (modrm_byte & 0xF8) {
977           case 0xC0: mnem = "faddp"; break;
978           case 0xE8: mnem = "fsubp"; break;
979           case 0xC8: mnem = "fmulp"; break;
980           case 0xF8: mnem = "fdivp"; break;
981           default: UnimplementedInstruction();
982         }
983       }
984       break;
985
986     case 0xDF:
987       if (modrm_byte == 0xE0) {
988         mnem = "fnstsw_ax";
989       } else if ((modrm_byte & 0xF8) == 0xE8) {
990         mnem = "fucomip";
991         has_register = true;
992       }
993       break;
994
995     default: UnimplementedInstruction();
996   }
997
998   if (has_register) {
999     AppendToBuffer("%s st%d", mnem, modrm_byte & 0x7);
1000   } else {
1001     AppendToBuffer("%s", mnem);
1002   }
1003   return 2;
1004 }
1005
1006
1007
1008 // Handle all two-byte opcodes, which start with 0x0F.
1009 // These instructions may be affected by an 0x66, 0xF2, or 0xF3 prefix.
1010 // We do not use any three-byte opcodes, which start with 0x0F38 or 0x0F3A.
1011 int DisassemblerX64::TwoByteOpcodeInstruction(byte* data) {
1012   byte opcode = *(data + 1);
1013   byte* current = data + 2;
1014   // At return, "current" points to the start of the next instruction.
1015   const char* mnemonic = TwoByteMnemonic(opcode);
1016   if (operand_size_ == 0x66) {
1017     // 0x66 0x0F prefix.
1018     int mod, regop, rm;
1019     if (opcode == 0x3A) {
1020       byte third_byte = *current;
1021       current = data + 3;
1022       if (third_byte == 0x17) {
1023         get_modrm(*current, &mod, &regop, &rm);
1024         AppendToBuffer("extractps ");  // reg/m32, xmm, imm8
1025         current += PrintRightOperand(current);
1026         AppendToBuffer(",%s,%d", NameOfXMMRegister(regop), (*current) & 3);
1027         current += 1;
1028       } else if (third_byte == 0x21) {
1029         get_modrm(*current, &mod, &regop, &rm);
1030         // insertps xmm, xmm, imm8
1031         AppendToBuffer("insertps %s,%s,%d",
1032                        NameOfXMMRegister(regop),
1033                        NameOfXMMRegister(rm),
1034                        (*(current + 1)) & 3);
1035         current += 2;
1036       } else if (third_byte == 0x22) {
1037         get_modrm(*current, &mod, &regop, &rm);
1038         // pinsrd xmm, reg32, imm8
1039         AppendToBuffer("pinsrd %s,%s,%d",
1040                        NameOfXMMRegister(regop),
1041                        NameOfCPURegister(rm),
1042                        (*(current + 1)) & 3);
1043         current += 2;
1044       } else if (third_byte == 0x0b) {
1045         get_modrm(*current, &mod, &regop, &rm);
1046          // roundsd xmm, xmm/m64, imm8
1047         AppendToBuffer("roundsd %s,", NameOfXMMRegister(regop));
1048         current += PrintRightXMMOperand(current);
1049         AppendToBuffer(",%d", (*current) & 3);
1050         current += 1;
1051       } else {
1052         UnimplementedInstruction();
1053       }
1054     } else if (opcode == 0x38) {
1055       byte third_byte = *current;
1056       current = data + 3;
1057       if (third_byte == 0x40) {
1058         get_modrm(*current, &mod, &regop, &rm);
1059         AppendToBuffer("pmulld %s, ", NameOfXMMRegister(regop));
1060         current += PrintRightXMMOperand(current);
1061       } else {
1062         UnimplementedInstruction();
1063       }
1064     } else {
1065       get_modrm(*current, &mod, &regop, &rm);
1066       if (opcode == 0x1f) {
1067         current++;
1068         if (rm == 4) {  // SIB byte present.
1069           current++;
1070         }
1071         if (mod == 1) {  // Byte displacement.
1072           current += 1;
1073         } else if (mod == 2) {  // 32-bit displacement.
1074           current += 4;
1075         }  // else no immediate displacement.
1076         AppendToBuffer("nop");
1077       } else if (opcode == 0x28) {
1078         AppendToBuffer("movapd %s,", NameOfXMMRegister(regop));
1079         current += PrintRightXMMOperand(current);
1080       } else if (opcode == 0x29) {
1081         AppendToBuffer("movapd ");
1082         current += PrintRightXMMOperand(current);
1083         AppendToBuffer(",%s", NameOfXMMRegister(regop));
1084       } else if (opcode == 0x6E) {
1085         AppendToBuffer("mov%c %s,",
1086                        rex_w() ? 'q' : 'd',
1087                        NameOfXMMRegister(regop));
1088         current += PrintRightOperand(current);
1089       } else if (opcode == 0x6F) {
1090         AppendToBuffer("movdqa %s,",
1091                        NameOfXMMRegister(regop));
1092         current += PrintRightXMMOperand(current);
1093       } else if (opcode == 0x70) {
1094         AppendToBuffer("pshufd %s,",
1095                        NameOfXMMRegister(regop));
1096         current += PrintRightXMMOperand(current);
1097         AppendToBuffer(",0x%x", (*current) & 0xff);
1098         current += 1;
1099       } else if (opcode == 0x5B) {
1100         AppendToBuffer("cvtps2dq %s,",
1101                        NameOfXMMRegister(regop));
1102         current += PrintRightXMMOperand(current);
1103       } else if (opcode == 0xFE) {
1104         AppendToBuffer("paddd %s,",
1105                        NameOfXMMRegister(regop));
1106         current += PrintRightXMMOperand(current);
1107       } else if (opcode == 0xFA) {
1108         AppendToBuffer("psubd %s,",
1109                        NameOfXMMRegister(regop));
1110         current += PrintRightXMMOperand(current);
1111       } else if (opcode == 0x7E) {
1112         AppendToBuffer("mov%c ",
1113                        rex_w() ? 'q' : 'd');
1114         current += PrintRightOperand(current);
1115         AppendToBuffer(",%s", NameOfXMMRegister(regop));
1116       } else if (opcode == 0x7F) {
1117         AppendToBuffer("movdqa ");
1118         current += PrintRightXMMOperand(current);
1119         AppendToBuffer(",%s", NameOfXMMRegister(regop));
1120       } else if (opcode == 0xD6) {
1121         AppendToBuffer("movq ");
1122         current += PrintRightXMMOperand(current);
1123         AppendToBuffer(",%s", NameOfXMMRegister(regop));
1124       } else if (opcode == 0x50) {
1125         AppendToBuffer("movmskpd %s,", NameOfCPURegister(regop));
1126         current += PrintRightXMMOperand(current);
1127       } else if (opcode == 0x73) {
1128         current += 1;
1129         DCHECK(regop == 6);
1130         AppendToBuffer("psllq,%s,%d", NameOfXMMRegister(rm), *current & 0x7f);
1131         current += 1;
1132       } else if (opcode == 0x62) {
1133         AppendToBuffer("punpackldq %s,", NameOfXMMRegister(regop));
1134         current += PrintRightXMMOperand(current);
1135       } else if (opcode == 0x72) {
1136         AppendToBuffer(regop == rsi ? "pslld "
1137                                     : regop == rdx ? "psrld" : "psrad");
1138         current += PrintRightXMMOperand(current);
1139         AppendToBuffer(",0x%x", (*current) & 0xff);
1140         current += 1;
1141       } else if (opcode == 0xC6) {
1142         AppendToBuffer("shufpd %s,", NameOfXMMRegister(regop));
1143         current += PrintRightXMMOperand(current);
1144         AppendToBuffer(",0x%x", (*current) & 0xff);
1145         current += 1;
1146       } else if (opcode == 0xF4) {
1147         AppendToBuffer("pmuludq %s,", NameOfXMMRegister(regop));
1148         current += PrintRightXMMOperand(current);
1149       } else {
1150         const char* mnemonic = "?";
1151         if (opcode == 0x51) {
1152           mnemonic = "sqrtpd";
1153         } else if (opcode == 0x54) {
1154           mnemonic = "andpd";
1155         } else  if (opcode == 0x56) {
1156           mnemonic = "orpd";
1157         } else  if (opcode == 0x57) {
1158           mnemonic = "xorpd";
1159         } else  if (opcode == 0x58) {
1160           mnemonic = "addpd";
1161         } else  if (opcode == 0x59) {
1162           mnemonic = "mulpd";
1163         } else  if (opcode == 0x5C) {
1164           mnemonic = "subpd";
1165         } else  if (opcode == 0x5D) {
1166           mnemonic = "minpd";
1167         } else  if (opcode == 0x5E) {
1168           mnemonic = "divpd";
1169         } else  if (opcode == 0x5F) {
1170           mnemonic = "maxpd";
1171         } else if (opcode == 0x2E) {
1172           mnemonic = "ucomisd";
1173         } else if (opcode == 0x2F) {
1174           mnemonic = "comisd";
1175         } else if (opcode == 0x66) {
1176           mnemonic = "pcmpgtd";
1177         } else if (opcode == 0x76) {
1178           mnemonic = "pcmpeqd";
1179         } else if (opcode == 0xD2) {
1180           mnemonic = "psrld";
1181         } else if (opcode == 0xE2) {
1182           mnemonic = "psrad";
1183         } else if (opcode == 0xF2) {
1184           mnemonic = "pslld";
1185         } else {
1186           UnimplementedInstruction();
1187         }
1188         AppendToBuffer("%s %s,", mnemonic, NameOfXMMRegister(regop));
1189         current += PrintRightXMMOperand(current);
1190       }
1191     }
1192   } else if (group_1_prefix_ == 0xF2) {
1193     // Beginning of instructions with prefix 0xF2.
1194
1195     if (opcode == 0x11 || opcode == 0x10) {
1196       // MOVSD: Move scalar double-precision fp to/from/between XMM registers.
1197       AppendToBuffer("movsd ");
1198       int mod, regop, rm;
1199       get_modrm(*current, &mod, &regop, &rm);
1200       if (opcode == 0x11) {
1201         current += PrintRightXMMOperand(current);
1202         AppendToBuffer(",%s", NameOfXMMRegister(regop));
1203       } else {
1204         AppendToBuffer("%s,", NameOfXMMRegister(regop));
1205         current += PrintRightXMMOperand(current);
1206       }
1207     } else if (opcode == 0x2A) {
1208       // CVTSI2SD: integer to XMM double conversion.
1209       int mod, regop, rm;
1210       get_modrm(*current, &mod, &regop, &rm);
1211       AppendToBuffer("%sd %s,", mnemonic, NameOfXMMRegister(regop));
1212       current += PrintRightOperand(current);
1213     } else if (opcode == 0x2C) {
1214       // CVTTSD2SI:
1215       // Convert with truncation scalar double-precision FP to integer.
1216       int mod, regop, rm;
1217       get_modrm(*current, &mod, &regop, &rm);
1218       AppendToBuffer("cvttsd2si%c %s,",
1219           operand_size_code(), NameOfCPURegister(regop));
1220       current += PrintRightXMMOperand(current);
1221     } else if (opcode == 0x2D) {
1222       // CVTSD2SI: Convert scalar double-precision FP to integer.
1223       int mod, regop, rm;
1224       get_modrm(*current, &mod, &regop, &rm);
1225       AppendToBuffer("cvtsd2si%c %s,",
1226           operand_size_code(), NameOfCPURegister(regop));
1227       current += PrintRightXMMOperand(current);
1228     } else if ((opcode & 0xF8) == 0x58 || opcode == 0x51) {
1229       // XMM arithmetic. Mnemonic was retrieved at the start of this function.
1230       int mod, regop, rm;
1231       get_modrm(*current, &mod, &regop, &rm);
1232       AppendToBuffer("%s %s,", mnemonic, NameOfXMMRegister(regop));
1233       current += PrintRightXMMOperand(current);
1234     } else if (opcode == 0xC2) {
1235       // Intel manual 2A, Table 3-18.
1236       int mod, regop, rm;
1237       get_modrm(*current, &mod, &regop, &rm);
1238       const char* const pseudo_op[] = {
1239         "cmpeqsd",
1240         "cmpltsd",
1241         "cmplesd",
1242         "cmpunordsd",
1243         "cmpneqsd",
1244         "cmpnltsd",
1245         "cmpnlesd",
1246         "cmpordsd"
1247       };
1248       AppendToBuffer("%s %s,%s",
1249                      pseudo_op[current[1]],
1250                      NameOfXMMRegister(regop),
1251                      NameOfXMMRegister(rm));
1252       current += 2;
1253     } else {
1254       UnimplementedInstruction();
1255     }
1256   } else if (group_1_prefix_ == 0xF3) {
1257     // Instructions with prefix 0xF3.
1258     if (opcode == 0x11 || opcode == 0x10) {
1259       // MOVSS: Move scalar double-precision fp to/from/between XMM registers.
1260       AppendToBuffer("movss ");
1261       int mod, regop, rm;
1262       get_modrm(*current, &mod, &regop, &rm);
1263       if (opcode == 0x11) {
1264         current += PrintRightOperand(current);
1265         AppendToBuffer(",%s", NameOfXMMRegister(regop));
1266       } else {
1267         AppendToBuffer("%s,", NameOfXMMRegister(regop));
1268         current += PrintRightOperand(current);
1269       }
1270     } else if (opcode == 0x2A) {
1271       // CVTSI2SS: integer to XMM single conversion.
1272       int mod, regop, rm;
1273       get_modrm(*current, &mod, &regop, &rm);
1274       AppendToBuffer("%ss %s,", mnemonic, NameOfXMMRegister(regop));
1275       current += PrintRightOperand(current);
1276     } else if (opcode == 0x2C) {
1277       // CVTTSS2SI:
1278       // Convert with truncation scalar single-precision FP to dword integer.
1279       int mod, regop, rm;
1280       get_modrm(*current, &mod, &regop, &rm);
1281       AppendToBuffer("cvttss2si%c %s,",
1282           operand_size_code(), NameOfCPURegister(regop));
1283       current += PrintRightXMMOperand(current);
1284     } else if (opcode == 0x5A) {
1285       // CVTSS2SD:
1286       // Convert scalar single-precision FP to scalar double-precision FP.
1287       int mod, regop, rm;
1288       get_modrm(*current, &mod, &regop, &rm);
1289       AppendToBuffer("cvtss2sd %s,", NameOfXMMRegister(regop));
1290       current += PrintRightXMMOperand(current);
1291     } else if (opcode == 0x7E) {
1292       int mod, regop, rm;
1293       get_modrm(*current, &mod, &regop, &rm);
1294       AppendToBuffer("movq %s,", NameOfXMMRegister(regop));
1295       current += PrintRightXMMOperand(current);
1296     } else {
1297       UnimplementedInstruction();
1298     }
1299   } else if (opcode == 0x1F) {
1300     // NOP
1301     int mod, regop, rm;
1302     get_modrm(*current, &mod, &regop, &rm);
1303     current++;
1304     if (rm == 4) {  // SIB byte present.
1305       current++;
1306     }
1307     if (mod == 1) {  // Byte displacement.
1308       current += 1;
1309     } else if (mod == 2) {  // 32-bit displacement.
1310       current += 4;
1311     }  // else no immediate displacement.
1312     AppendToBuffer("nop");
1313
1314   } else if (opcode == 0x28) {
1315     // movaps xmm, xmm/m128
1316     int mod, regop, rm;
1317     get_modrm(*current, &mod, &regop, &rm);
1318     AppendToBuffer("movaps %s,", NameOfXMMRegister(regop));
1319     current += PrintRightXMMOperand(current);
1320
1321   } else if (opcode == 0x29) {
1322     // movaps xmm/m128, xmm
1323     int mod, regop, rm;
1324     get_modrm(*current, &mod, &regop, &rm);
1325     AppendToBuffer("movaps ");
1326     current += PrintRightXMMOperand(current);
1327     AppendToBuffer(",%s", NameOfXMMRegister(regop));
1328
1329   } else if (opcode == 0x10) {
1330     // movups xmm, xmm/m128
1331     int mod, regop, rm;
1332     get_modrm(*current, &mod, &regop, &rm);
1333     AppendToBuffer("movups %s, ", NameOfXMMRegister(regop));
1334     current += PrintRightXMMOperand(current);
1335
1336   } else if (opcode == 0x11) {
1337     // movups xmm/m128, xmm
1338     int mod, regop, rm;
1339     get_modrm(*current, &mod, &regop, &rm);
1340     AppendToBuffer("movups ");
1341     current += PrintRightXMMOperand(current);
1342     AppendToBuffer(", %s", NameOfXMMRegister(regop));
1343
1344   } else if (opcode == 0xA2) {
1345     // CPUID
1346     AppendToBuffer("%s", mnemonic);
1347
1348   } else if ((opcode & 0xF0) == 0x40) {
1349     // CMOVcc: conditional move.
1350     int condition = opcode & 0x0F;
1351     const InstructionDesc& idesc = cmov_instructions[condition];
1352     byte_size_operand_ = idesc.byte_size_operation;
1353     current += PrintOperands(idesc.mnem, idesc.op_order_, current);
1354
1355   } else if (opcode >= 0x53 && opcode <= 0x5F) {
1356     const char* const pseudo_op[] = {
1357       "rcpps",
1358       "andps",
1359       "andnps",
1360       "orps",
1361       "xorps",
1362       "addps",
1363       "mulps",
1364       "cvtps2pd",
1365       "cvtdq2ps",
1366       "subps",
1367       "minps",
1368       "divps",
1369       "maxps",
1370     };
1371     int mod, regop, rm;
1372     get_modrm(*current, &mod, &regop, &rm);
1373     AppendToBuffer("%s %s,",
1374                    pseudo_op[opcode - 0x53],
1375                    NameOfXMMRegister(regop));
1376     current += PrintRightXMMOperand(current);
1377
1378   } else if (opcode == 0xC6) {
1379     // shufps xmm, xmm/m128, imm8
1380     int mod, regop, rm;
1381     get_modrm(*current, &mod, &regop, &rm);
1382     AppendToBuffer("shufps %s, ", NameOfXMMRegister(regop));
1383     current += PrintRightXMMOperand(current);
1384     AppendToBuffer(", %d", (*current) & 3);
1385     current += 1;
1386
1387   } else if (opcode == 0xC6) {
1388     // shufps xmm, xmm/m128, imm8
1389     int mod, regop, rm;
1390     get_modrm(*current, &mod, &regop, &rm);
1391     AppendToBuffer("shufps %s, ", NameOfXMMRegister(regop));
1392     current += PrintRightXMMOperand(current);
1393     AppendToBuffer(", %d", (*current) & 3);
1394     current += 1;
1395
1396   } else if (opcode == 0x54) {
1397     // andps xmm, xmm/m128
1398     int mod, regop, rm;
1399     get_modrm(*current, &mod, &regop, &rm);
1400     AppendToBuffer("andps %s, ", NameOfXMMRegister(regop));
1401     current += PrintRightXMMOperand(current);
1402
1403   } else if (opcode == 0x56) {
1404     // orps xmm, xmm/m128
1405     int mod, regop, rm;
1406     get_modrm(*current, &mod, &regop, &rm);
1407     AppendToBuffer("orps %s, ", NameOfXMMRegister(regop));
1408     current += PrintRightXMMOperand(current);
1409
1410   } else if (opcode == 0x58) {
1411     // addps xmm, xmm/m128
1412     int mod, regop, rm;
1413     get_modrm(*current, &mod, &regop, &rm);
1414     AppendToBuffer("addps %s, ", NameOfXMMRegister(regop));
1415     current += PrintRightXMMOperand(current);
1416
1417   } else if (opcode == 0x59) {
1418     // mulps xmm, xmm/m128
1419     int mod, regop, rm;
1420     get_modrm(*current, &mod, &regop, &rm);
1421     AppendToBuffer("mulps %s, ", NameOfXMMRegister(regop));
1422     current += PrintRightXMMOperand(current);
1423
1424   } else if (opcode == 0x5C) {
1425     // subps xmm, xmm/m128
1426     int mod, regop, rm;
1427     get_modrm(*current, &mod, &regop, &rm);
1428     AppendToBuffer("subps %s, ", NameOfXMMRegister(regop));
1429     current += PrintRightXMMOperand(current);
1430
1431   } else if (opcode == 0x5E) {
1432     // divps xmm, xmm/m128
1433     int mod, regop, rm;
1434     get_modrm(*current, &mod, &regop, &rm);
1435     AppendToBuffer("divps %s, ", NameOfXMMRegister(regop));
1436     current += PrintRightXMMOperand(current);
1437
1438   } else if (opcode == 0x5D) {
1439     // minps xmm, xmm/m128
1440     int mod, regop, rm;
1441     get_modrm(*current, &mod, &regop, &rm);
1442     AppendToBuffer("minps %s, ", NameOfXMMRegister(regop));
1443     current += PrintRightXMMOperand(current);
1444
1445   } else if (opcode == 0x5F) {
1446     // maxps xmm, xmm/m128
1447     int mod, regop, rm;
1448     get_modrm(*current, &mod, &regop, &rm);
1449     AppendToBuffer("maxps %s, ", NameOfXMMRegister(regop));
1450     current += PrintRightXMMOperand(current);
1451
1452   } else if (opcode == 0x5B) {
1453     // cvtdq2ps xmm, xmm/m128
1454     int mod, regop, rm;
1455     get_modrm(*current, &mod, &regop, &rm);
1456     AppendToBuffer("cvtdq2ps %s, ", NameOfXMMRegister(regop));
1457     current += PrintRightXMMOperand(current);
1458
1459
1460   } else if (opcode == 0x53) {
1461     // rcpps xmm, xmm/m128
1462     int mod, regop, rm;
1463     get_modrm(*current, &mod, &regop, &rm);
1464     AppendToBuffer("rcpps %s, ", NameOfXMMRegister(regop));
1465     current += PrintRightXMMOperand(current);
1466
1467   } else if (opcode == 0x52) {
1468     // rsqrtps xmm, xmm/m128
1469     int mod, regop, rm;
1470     get_modrm(*current, &mod, &regop, &rm);
1471     AppendToBuffer("rsqrtps %s, ", NameOfXMMRegister(regop));
1472     current += PrintRightXMMOperand(current);
1473
1474   } else if (opcode == 0x51) {
1475     // sqrtps xmm, xmm/m128
1476     int mod, regop, rm;
1477     get_modrm(*current, &mod, &regop, &rm);
1478     AppendToBuffer("sqrtps %s, ", NameOfXMMRegister(regop));
1479     current += PrintRightXMMOperand(current);
1480
1481   } else if (opcode == 0x50) {
1482     // movmskps reg, xmm
1483     int mod, regop, rm;
1484     get_modrm(*current, &mod, &regop, &rm);
1485     AppendToBuffer("movmskps %s,", NameOfCPURegister(regop));
1486     current += PrintRightXMMOperand(current);
1487
1488   } else if (opcode == 0xC2) {
1489     // Intel manual 2A, Table 3-11.
1490     int mod, regop, rm;
1491     get_modrm(*current, &mod, &regop, &rm);
1492     const char* const pseudo_op[] = {
1493       "cmpeqps",
1494       "cmpltps",
1495       "cmpleps",
1496       "cmpunordps",
1497       "cmpneqps",
1498       "cmpnltps",
1499       "cmpnleps",
1500       "cmpordps"
1501     };
1502     AppendToBuffer("%s %s,%s",
1503                    pseudo_op[current[1]],
1504                    NameOfXMMRegister(regop),
1505                    NameOfXMMRegister(rm));
1506     current += 2;
1507
1508   } else if ((opcode & 0xF0) == 0x80) {
1509     // Jcc: Conditional jump (branch).
1510     current = data + JumpConditional(data);
1511
1512   } else if (opcode == 0xBE || opcode == 0xBF || opcode == 0xB6 ||
1513              opcode == 0xB7 || opcode == 0xAF) {
1514     // Size-extending moves, IMUL.
1515     current += PrintOperands(mnemonic, REG_OPER_OP_ORDER, current);
1516
1517   } else if ((opcode & 0xF0) == 0x90) {
1518     // SETcc: Set byte on condition. Needs pointer to beginning of instruction.
1519     current = data + SetCC(data);
1520
1521   } else if (opcode == 0xAB || opcode == 0xA5 || opcode == 0xAD) {
1522     // SHLD, SHRD (double-precision shift), BTS (bit set).
1523     AppendToBuffer("%s ", mnemonic);
1524     int mod, regop, rm;
1525     get_modrm(*current, &mod, &regop, &rm);
1526     current += PrintRightOperand(current);
1527     if (opcode == 0xAB) {
1528       AppendToBuffer(",%s", NameOfCPURegister(regop));
1529     } else {
1530       AppendToBuffer(",%s,cl", NameOfCPURegister(regop));
1531     }
1532   } else if (opcode == 0xBD) {
1533     AppendToBuffer("%s%c ", mnemonic, operand_size_code());
1534     int mod, regop, rm;
1535     get_modrm(*current, &mod, &regop, &rm);
1536     AppendToBuffer("%s,", NameOfCPURegister(regop));
1537     current += PrintRightOperand(current);
1538   } else {
1539     UnimplementedInstruction();
1540   }
1541   return static_cast<int>(current - data);
1542 }
1543
1544
1545 // Mnemonics for two-byte opcode instructions starting with 0x0F.
1546 // The argument is the second byte of the two-byte opcode.
1547 // Returns NULL if the instruction is not handled here.
1548 const char* DisassemblerX64::TwoByteMnemonic(byte opcode) {
1549   switch (opcode) {
1550     case 0x1F:
1551       return "nop";
1552     case 0x2A:  // F2/F3 prefix.
1553       return "cvtsi2s";
1554     case 0x51:  // F2 prefix.
1555       return "sqrtsd";
1556     case 0x58:  // F2 prefix.
1557       return "addsd";
1558     case 0x59:  // F2 prefix.
1559       return "mulsd";
1560     case 0x5A:  // F2 prefix.
1561       return "cvtsd2ss";
1562     case 0x5C:  // F2 prefix.
1563       return "subsd";
1564     case 0x5E:  // F2 prefix.
1565       return "divsd";
1566     case 0xA2:
1567       return "cpuid";
1568     case 0xA5:
1569       return "shld";
1570     case 0xAB:
1571       return "bts";
1572     case 0xAD:
1573       return "shrd";
1574     case 0xAF:
1575       return "imul";
1576     case 0xB6:
1577       return "movzxb";
1578     case 0xB7:
1579       return "movzxw";
1580     case 0xBD:
1581       return "bsr";
1582     case 0xBE:
1583       return "movsxb";
1584     case 0xBF:
1585       return "movsxw";
1586     default:
1587       return NULL;
1588   }
1589 }
1590
1591
1592 // Disassembles the instruction at instr, and writes it into out_buffer.
1593 int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
1594                                        byte* instr) {
1595   tmp_buffer_pos_ = 0;  // starting to write as position 0
1596   byte* data = instr;
1597   bool processed = true;  // Will be set to false if the current instruction
1598                           // is not in 'instructions' table.
1599   byte current;
1600
1601   // Scan for prefixes.
1602   while (true) {
1603     current = *data;
1604     if (current == OPERAND_SIZE_OVERRIDE_PREFIX) {  // Group 3 prefix.
1605       operand_size_ = current;
1606     } else if ((current & 0xF0) == 0x40) {  // REX prefix.
1607       setRex(current);
1608       if (rex_w()) AppendToBuffer("REX.W ");
1609     } else if ((current & 0xFE) == 0xF2) {  // Group 1 prefix (0xF2 or 0xF3).
1610       group_1_prefix_ = current;
1611     } else {  // Not a prefix - an opcode.
1612       break;
1613     }
1614     data++;
1615   }
1616
1617   const InstructionDesc& idesc = instruction_table_->Get(current);
1618   byte_size_operand_ = idesc.byte_size_operation;
1619   switch (idesc.type) {
1620     case ZERO_OPERANDS_INSTR:
1621       if (current >= 0xA4 && current <= 0xA7) {
1622         // String move or compare operations.
1623         if (group_1_prefix_ == REP_PREFIX) {
1624           // REP.
1625           AppendToBuffer("rep ");
1626         }
1627         if (rex_w()) AppendToBuffer("REX.W ");
1628         AppendToBuffer("%s%c", idesc.mnem, operand_size_code());
1629       } else {
1630         AppendToBuffer("%s", idesc.mnem, operand_size_code());
1631       }
1632       data++;
1633       break;
1634
1635     case TWO_OPERANDS_INSTR:
1636       data++;
1637       data += PrintOperands(idesc.mnem, idesc.op_order_, data);
1638       break;
1639
1640     case JUMP_CONDITIONAL_SHORT_INSTR:
1641       data += JumpConditionalShort(data);
1642       break;
1643
1644     case REGISTER_INSTR:
1645       AppendToBuffer("%s%c %s",
1646                      idesc.mnem,
1647                      operand_size_code(),
1648                      NameOfCPURegister(base_reg(current & 0x07)));
1649       data++;
1650       break;
1651     case PUSHPOP_INSTR:
1652       AppendToBuffer("%s %s",
1653                      idesc.mnem,
1654                      NameOfCPURegister(base_reg(current & 0x07)));
1655       data++;
1656       break;
1657     case MOVE_REG_INSTR: {
1658       byte* addr = NULL;
1659       switch (operand_size()) {
1660         case OPERAND_WORD_SIZE:
1661           addr = reinterpret_cast<byte*>(*reinterpret_cast<int16_t*>(data + 1));
1662           data += 3;
1663           break;
1664         case OPERAND_DOUBLEWORD_SIZE:
1665           addr =
1666               reinterpret_cast<byte*>(*reinterpret_cast<uint32_t*>(data + 1));
1667           data += 5;
1668           break;
1669         case OPERAND_QUADWORD_SIZE:
1670           addr = reinterpret_cast<byte*>(*reinterpret_cast<int64_t*>(data + 1));
1671           data += 9;
1672           break;
1673         default:
1674           UNREACHABLE();
1675       }
1676       AppendToBuffer("mov%c %s,%s",
1677                      operand_size_code(),
1678                      NameOfCPURegister(base_reg(current & 0x07)),
1679                      NameOfAddress(addr));
1680       break;
1681     }
1682
1683     case CALL_JUMP_INSTR: {
1684       byte* addr = data + *reinterpret_cast<int32_t*>(data + 1) + 5;
1685       AppendToBuffer("%s %s", idesc.mnem, NameOfAddress(addr));
1686       data += 5;
1687       break;
1688     }
1689
1690     case SHORT_IMMEDIATE_INSTR: {
1691       byte* addr =
1692           reinterpret_cast<byte*>(*reinterpret_cast<int32_t*>(data + 1));
1693       AppendToBuffer("%s rax,%s", idesc.mnem, NameOfAddress(addr));
1694       data += 5;
1695       break;
1696     }
1697
1698     case NO_INSTR:
1699       processed = false;
1700       break;
1701
1702     default:
1703       UNIMPLEMENTED();  // This type is not implemented.
1704   }
1705
1706   // The first byte didn't match any of the simple opcodes, so we
1707   // need to do special processing on it.
1708   if (!processed) {
1709     switch (*data) {
1710       case 0xC2:
1711         AppendToBuffer("ret 0x%x", *reinterpret_cast<uint16_t*>(data + 1));
1712         data += 3;
1713         break;
1714
1715       case 0x69:  // fall through
1716       case 0x6B: {
1717         int mod, regop, rm;
1718         get_modrm(*(data + 1), &mod, &regop, &rm);
1719         int32_t imm = *data == 0x6B ? *(data + 2)
1720             : *reinterpret_cast<int32_t*>(data + 2);
1721         AppendToBuffer("imul%c %s,%s,0x%x",
1722                        operand_size_code(),
1723                        NameOfCPURegister(regop),
1724                        NameOfCPURegister(rm), imm);
1725         data += 2 + (*data == 0x6B ? 1 : 4);
1726         break;
1727       }
1728
1729       case 0x81:  // fall through
1730       case 0x83:  // 0x81 with sign extension bit set
1731         data += PrintImmediateOp(data);
1732         break;
1733
1734       case 0x0F:
1735         data += TwoByteOpcodeInstruction(data);
1736         break;
1737
1738       case 0x8F: {
1739         data++;
1740         int mod, regop, rm;
1741         get_modrm(*data, &mod, &regop, &rm);
1742         if (regop == 0) {
1743           AppendToBuffer("pop ");
1744           data += PrintRightOperand(data);
1745         }
1746       }
1747         break;
1748
1749       case 0xFF: {
1750         data++;
1751         int mod, regop, rm;
1752         get_modrm(*data, &mod, &regop, &rm);
1753         const char* mnem = NULL;
1754         switch (regop) {
1755           case 0:
1756             mnem = "inc";
1757             break;
1758           case 1:
1759             mnem = "dec";
1760             break;
1761           case 2:
1762             mnem = "call";
1763             break;
1764           case 4:
1765             mnem = "jmp";
1766             break;
1767           case 6:
1768             mnem = "push";
1769             break;
1770           default:
1771             mnem = "???";
1772         }
1773         AppendToBuffer(((regop <= 1) ? "%s%c " : "%s "),
1774                        mnem,
1775                        operand_size_code());
1776         data += PrintRightOperand(data);
1777       }
1778         break;
1779
1780       case 0xC7:  // imm32, fall through
1781       case 0xC6:  // imm8
1782       {
1783         bool is_byte = *data == 0xC6;
1784         data++;
1785         if (is_byte) {
1786           AppendToBuffer("movb ");
1787           data += PrintRightByteOperand(data);
1788           int32_t imm = *data;
1789           AppendToBuffer(",0x%x", imm);
1790           data++;
1791         } else {
1792           AppendToBuffer("mov%c ", operand_size_code());
1793           data += PrintRightOperand(data);
1794           if (operand_size() == OPERAND_WORD_SIZE) {
1795             int16_t imm = *reinterpret_cast<int16_t*>(data);
1796             AppendToBuffer(",0x%x", imm);
1797             data += 2;
1798           } else {
1799             int32_t imm = *reinterpret_cast<int32_t*>(data);
1800             AppendToBuffer(",0x%x", imm);
1801             data += 4;
1802           }
1803         }
1804       }
1805         break;
1806
1807       case 0x80: {
1808         data++;
1809         AppendToBuffer("cmpb ");
1810         data += PrintRightByteOperand(data);
1811         int32_t imm = *data;
1812         AppendToBuffer(",0x%x", imm);
1813         data++;
1814       }
1815         break;
1816
1817       case 0x88:  // 8bit, fall through
1818       case 0x89:  // 32bit
1819       {
1820         bool is_byte = *data == 0x88;
1821         int mod, regop, rm;
1822         data++;
1823         get_modrm(*data, &mod, &regop, &rm);
1824         if (is_byte) {
1825           AppendToBuffer("movb ");
1826           data += PrintRightByteOperand(data);
1827           AppendToBuffer(",%s", NameOfByteCPURegister(regop));
1828         } else {
1829           AppendToBuffer("mov%c ", operand_size_code());
1830           data += PrintRightOperand(data);
1831           AppendToBuffer(",%s", NameOfCPURegister(regop));
1832         }
1833       }
1834         break;
1835
1836       case 0x90:
1837       case 0x91:
1838       case 0x92:
1839       case 0x93:
1840       case 0x94:
1841       case 0x95:
1842       case 0x96:
1843       case 0x97: {
1844         int reg = (*data & 0x7) | (rex_b() ? 8 : 0);
1845         if (reg == 0) {
1846           AppendToBuffer("nop");  // Common name for xchg rax,rax.
1847         } else {
1848           AppendToBuffer("xchg%c rax,%s",
1849                          operand_size_code(),
1850                          NameOfCPURegister(reg));
1851         }
1852         data++;
1853       }
1854         break;
1855       case 0xB0:
1856       case 0xB1:
1857       case 0xB2:
1858       case 0xB3:
1859       case 0xB4:
1860       case 0xB5:
1861       case 0xB6:
1862       case 0xB7:
1863       case 0xB8:
1864       case 0xB9:
1865       case 0xBA:
1866       case 0xBB:
1867       case 0xBC:
1868       case 0xBD:
1869       case 0xBE:
1870       case 0xBF: {
1871         // mov reg8,imm8 or mov reg32,imm32
1872         byte opcode = *data;
1873         data++;
1874         bool is_32bit = (opcode >= 0xB8);
1875         int reg = (opcode & 0x7) | (rex_b() ? 8 : 0);
1876         if (is_32bit) {
1877           AppendToBuffer("mov%c %s,",
1878                          operand_size_code(),
1879                          NameOfCPURegister(reg));
1880           data += PrintImmediate(data, OPERAND_DOUBLEWORD_SIZE);
1881         } else {
1882           AppendToBuffer("movb %s,",
1883                          NameOfByteCPURegister(reg));
1884           data += PrintImmediate(data, OPERAND_BYTE_SIZE);
1885         }
1886         break;
1887       }
1888       case 0xFE: {
1889         data++;
1890         int mod, regop, rm;
1891         get_modrm(*data, &mod, &regop, &rm);
1892         if (regop == 1) {
1893           AppendToBuffer("decb ");
1894           data += PrintRightByteOperand(data);
1895         } else {
1896           UnimplementedInstruction();
1897         }
1898         break;
1899       }
1900       case 0x68:
1901         AppendToBuffer("push 0x%x", *reinterpret_cast<int32_t*>(data + 1));
1902         data += 5;
1903         break;
1904
1905       case 0x6A:
1906         AppendToBuffer("push 0x%x", *reinterpret_cast<int8_t*>(data + 1));
1907         data += 2;
1908         break;
1909
1910       case 0xA1:  // Fall through.
1911       case 0xA3:
1912         switch (operand_size()) {
1913           case OPERAND_DOUBLEWORD_SIZE: {
1914             const char* memory_location = NameOfAddress(
1915                 reinterpret_cast<byte*>(
1916                     *reinterpret_cast<int32_t*>(data + 1)));
1917             if (*data == 0xA1) {  // Opcode 0xA1
1918               AppendToBuffer("movzxlq rax,(%s)", memory_location);
1919             } else {  // Opcode 0xA3
1920               AppendToBuffer("movzxlq (%s),rax", memory_location);
1921             }
1922             data += 5;
1923             break;
1924           }
1925           case OPERAND_QUADWORD_SIZE: {
1926             // New x64 instruction mov rax,(imm_64).
1927             const char* memory_location = NameOfAddress(
1928                 *reinterpret_cast<byte**>(data + 1));
1929             if (*data == 0xA1) {  // Opcode 0xA1
1930               AppendToBuffer("movq rax,(%s)", memory_location);
1931             } else {  // Opcode 0xA3
1932               AppendToBuffer("movq (%s),rax", memory_location);
1933             }
1934             data += 9;
1935             break;
1936           }
1937           default:
1938             UnimplementedInstruction();
1939             data += 2;
1940         }
1941         break;
1942
1943       case 0xA8:
1944         AppendToBuffer("test al,0x%x", *reinterpret_cast<uint8_t*>(data + 1));
1945         data += 2;
1946         break;
1947
1948       case 0xA9: {
1949         int64_t value = 0;
1950         switch (operand_size()) {
1951           case OPERAND_WORD_SIZE:
1952             value = *reinterpret_cast<uint16_t*>(data + 1);
1953             data += 3;
1954             break;
1955           case OPERAND_DOUBLEWORD_SIZE:
1956             value = *reinterpret_cast<uint32_t*>(data + 1);
1957             data += 5;
1958             break;
1959           case OPERAND_QUADWORD_SIZE:
1960             value = *reinterpret_cast<int32_t*>(data + 1);
1961             data += 5;
1962             break;
1963           default:
1964             UNREACHABLE();
1965         }
1966         AppendToBuffer("test%c rax,0x%" V8_PTR_PREFIX "x",
1967                        operand_size_code(),
1968                        value);
1969         break;
1970       }
1971       case 0xD1:  // fall through
1972       case 0xD3:  // fall through
1973       case 0xC1:
1974         data += ShiftInstruction(data);
1975         break;
1976       case 0xD0:  // fall through
1977       case 0xD2:  // fall through
1978       case 0xC0:
1979         byte_size_operand_ = true;
1980         data += ShiftInstruction(data);
1981         break;
1982
1983       case 0xD9:  // fall through
1984       case 0xDA:  // fall through
1985       case 0xDB:  // fall through
1986       case 0xDC:  // fall through
1987       case 0xDD:  // fall through
1988       case 0xDE:  // fall through
1989       case 0xDF:
1990         data += FPUInstruction(data);
1991         break;
1992
1993       case 0xEB:
1994         data += JumpShort(data);
1995         break;
1996
1997       case 0xF6:
1998         byte_size_operand_ = true;  // fall through
1999       case 0xF7:
2000         data += F6F7Instruction(data);
2001         break;
2002
2003       case 0x3C:
2004         AppendToBuffer("cmp al,0x%x", *reinterpret_cast<int8_t*>(data + 1));
2005         data +=2;
2006         break;
2007
2008       default:
2009         UnimplementedInstruction();
2010         data += 1;
2011     }
2012   }  // !processed
2013
2014   if (tmp_buffer_pos_ < sizeof tmp_buffer_) {
2015     tmp_buffer_[tmp_buffer_pos_] = '\0';
2016   }
2017
2018   int instr_len = static_cast<int>(data - instr);
2019   DCHECK(instr_len > 0);  // Ensure progress.
2020
2021   int outp = 0;
2022   // Instruction bytes.
2023   for (byte* bp = instr; bp < data; bp++) {
2024     outp += v8::internal::SNPrintF(out_buffer + outp, "%02x", *bp);
2025   }
2026   for (int i = 6 - instr_len; i >= 0; i--) {
2027     outp += v8::internal::SNPrintF(out_buffer + outp, "  ");
2028   }
2029
2030   outp += v8::internal::SNPrintF(out_buffer + outp, " %s",
2031                                  tmp_buffer_.start());
2032   return instr_len;
2033 }
2034
2035
2036 //------------------------------------------------------------------------------
2037
2038
2039 static const char* cpu_regs[16] = {
2040   "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
2041   "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
2042 };
2043
2044
2045 static const char* byte_cpu_regs[16] = {
2046   "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
2047   "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l"
2048 };
2049
2050
2051 static const char* xmm_regs[16] = {
2052   "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
2053   "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15"
2054 };
2055
2056
2057 const char* NameConverter::NameOfAddress(byte* addr) const {
2058   v8::internal::SNPrintF(tmp_buffer_, "%p", addr);
2059   return tmp_buffer_.start();
2060 }
2061
2062
2063 const char* NameConverter::NameOfConstant(byte* addr) const {
2064   return NameOfAddress(addr);
2065 }
2066
2067
2068 const char* NameConverter::NameOfCPURegister(int reg) const {
2069   if (0 <= reg && reg < 16)
2070     return cpu_regs[reg];
2071   return "noreg";
2072 }
2073
2074
2075 const char* NameConverter::NameOfByteCPURegister(int reg) const {
2076   if (0 <= reg && reg < 16)
2077     return byte_cpu_regs[reg];
2078   return "noreg";
2079 }
2080
2081
2082 const char* NameConverter::NameOfXMMRegister(int reg) const {
2083   if (0 <= reg && reg < 16)
2084     return xmm_regs[reg];
2085   return "noxmmreg";
2086 }
2087
2088
2089 const char* NameConverter::NameInCode(byte* addr) const {
2090   // X64 does not embed debug strings at the moment.
2091   UNREACHABLE();
2092   return "";
2093 }
2094
2095
2096 //------------------------------------------------------------------------------
2097
2098 Disassembler::Disassembler(const NameConverter& converter)
2099     : converter_(converter) { }
2100
2101 Disassembler::~Disassembler() { }
2102
2103
2104 int Disassembler::InstructionDecode(v8::internal::Vector<char> buffer,
2105                                     byte* instruction) {
2106   DisassemblerX64 d(converter_, CONTINUE_ON_UNIMPLEMENTED_OPCODE);
2107   return d.InstructionDecode(buffer, instruction);
2108 }
2109
2110
2111 // The X64 assembler does not use constant pools.
2112 int Disassembler::ConstantPoolSizeAt(byte* instruction) {
2113   return -1;
2114 }
2115
2116
2117 void Disassembler::Disassemble(FILE* f, byte* begin, byte* end) {
2118   NameConverter converter;
2119   Disassembler d(converter);
2120   for (byte* pc = begin; pc < end;) {
2121     v8::internal::EmbeddedVector<char, 128> buffer;
2122     buffer[0] = '\0';
2123     byte* prev_pc = pc;
2124     pc += d.InstructionDecode(buffer, pc);
2125     fprintf(f, "%p", prev_pc);
2126     fprintf(f, "    ");
2127
2128     for (byte* bp = prev_pc; bp < pc; bp++) {
2129       fprintf(f, "%02x", *bp);
2130     }
2131     for (int i = 6 - static_cast<int>(pc - prev_pc); i >= 0; i--) {
2132       fprintf(f, "  ");
2133     }
2134     fprintf(f, "  %s\n", buffer.start());
2135   }
2136 }
2137
2138 }  // namespace disasm
2139
2140 #endif  // V8_TARGET_ARCH_X64