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