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