Commit c762f127 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[disasm] Clean up decoding of immediates

Instead of having hard to read reinterpret_casts all over the place,
extract this to separate methods.

R=mstarzinger@chromium.org

Bug: v8:9396
Change-Id: Id8d47b5dda8f5b32dedfe9f76c4f526ffcff0674
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1691024Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62594}
parent 4d40e867
This diff is collapsed.
...@@ -237,6 +237,30 @@ static const InstructionDesc cmov_instructions[16] = { ...@@ -237,6 +237,30 @@ static const InstructionDesc cmov_instructions[16] = {
{"cmovle", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}, {"cmovle", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false},
{"cmovg", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}}; {"cmovg", TWO_OPERANDS_INSTR, REG_OPER_OP_ORDER, false}};
namespace {
int8_t Imm8(const uint8_t* data) {
return *reinterpret_cast<const int8_t*>(data);
}
uint8_t Imm8_U(const uint8_t* data) {
return *reinterpret_cast<const uint8_t*>(data);
}
int16_t Imm16(const uint8_t* data) {
return *reinterpret_cast<const int16_t*>(data);
}
uint16_t Imm16_U(const uint8_t* data) {
return *reinterpret_cast<const uint16_t*>(data);
}
int32_t Imm32(const uint8_t* data) {
return *reinterpret_cast<const int32_t*>(data);
}
uint32_t Imm32_U(const uint8_t* data) {
return *reinterpret_cast<const uint32_t*>(data);
}
int64_t Imm64(const uint8_t* data) {
return *reinterpret_cast<const int64_t*>(data);
}
} // namespace
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// DisassemblerX64 implementation. // DisassemblerX64 implementation.
...@@ -458,8 +482,7 @@ int DisassemblerX64::PrintRightOperandHelper( ...@@ -458,8 +482,7 @@ int DisassemblerX64::PrintRightOperandHelper(
switch (mod) { switch (mod) {
case 0: case 0:
if ((rm & 7) == 5) { if ((rm & 7) == 5) {
int32_t disp = *reinterpret_cast<int32_t*>(modrmp + 1); AppendToBuffer("[rip+0x%x]", Imm32(modrmp + 1));
AppendToBuffer("[rip+0x%x]", disp);
return 5; return 5;
} else if ((rm & 7) == 4) { } else if ((rm & 7) == 4) {
// Codes for SIB byte. // Codes for SIB byte.
...@@ -473,7 +496,7 @@ int DisassemblerX64::PrintRightOperandHelper( ...@@ -473,7 +496,7 @@ int DisassemblerX64::PrintRightOperandHelper(
return 2; return 2;
} else if (base == 5) { } else if (base == 5) {
// base == rbp means no base register (when mod == 0). // base == rbp means no base register (when mod == 0).
int32_t disp = *reinterpret_cast<int32_t*>(modrmp + 2); int32_t disp = Imm32(modrmp + 2);
AppendToBuffer("[%s*%d%s0x%x]", NameOfCPURegister(index), 1 << scale, AppendToBuffer("[%s*%d%s0x%x]", NameOfCPURegister(index), 1 << scale,
disp < 0 ? "-" : "+", disp < 0 ? -disp : disp); disp < 0 ? "-" : "+", disp < 0 ? -disp : disp);
return 6; return 6;
...@@ -497,8 +520,7 @@ int DisassemblerX64::PrintRightOperandHelper( ...@@ -497,8 +520,7 @@ int DisassemblerX64::PrintRightOperandHelper(
byte sib = *(modrmp + 1); byte sib = *(modrmp + 1);
int scale, index, base; int scale, index, base;
get_sib(sib, &scale, &index, &base); get_sib(sib, &scale, &index, &base);
int disp = (mod == 2) ? *reinterpret_cast<int32_t*>(modrmp + 2) int disp = (mod == 2) ? Imm32(modrmp + 2) : Imm8(modrmp + 2);
: *reinterpret_cast<int8_t*>(modrmp + 2);
if (index == 4 && (base & 7) == 4 && scale == 0 /*times_1*/) { if (index == 4 && (base & 7) == 4 && scale == 0 /*times_1*/) {
AppendToBuffer("[%s%s0x%x]", NameOfCPURegister(base), AppendToBuffer("[%s%s0x%x]", NameOfCPURegister(base),
disp < 0 ? "-" : "+", disp < 0 ? -disp : disp); disp < 0 ? "-" : "+", disp < 0 ? -disp : disp);
...@@ -510,8 +532,7 @@ int DisassemblerX64::PrintRightOperandHelper( ...@@ -510,8 +532,7 @@ int DisassemblerX64::PrintRightOperandHelper(
return mod == 2 ? 6 : 3; return mod == 2 ? 6 : 3;
} else { } else {
// No sib. // No sib.
int disp = (mod == 2) ? *reinterpret_cast<int32_t*>(modrmp + 1) int disp = (mod == 2) ? Imm32(modrmp + 1) : Imm8(modrmp + 1);
: *reinterpret_cast<int8_t*>(modrmp + 1);
AppendToBuffer("[%s%s0x%x]", NameOfCPURegister(rm), AppendToBuffer("[%s%s0x%x]", NameOfCPURegister(rm),
disp < 0 ? "-" : "+", disp < 0 ? -disp : disp); disp < 0 ? "-" : "+", disp < 0 ? -disp : disp);
if (rm == i::kRootRegister.code()) { if (rm == i::kRootRegister.code()) {
...@@ -540,15 +561,15 @@ int DisassemblerX64::PrintImmediate(byte* data, OperandSize size) { ...@@ -540,15 +561,15 @@ int DisassemblerX64::PrintImmediate(byte* data, OperandSize size) {
count = 1; count = 1;
break; break;
case OPERAND_WORD_SIZE: case OPERAND_WORD_SIZE:
value = *reinterpret_cast<int16_t*>(data); value = Imm16(data);
count = 2; count = 2;
break; break;
case OPERAND_DOUBLEWORD_SIZE: case OPERAND_DOUBLEWORD_SIZE:
value = *reinterpret_cast<uint32_t*>(data); value = Imm32_U(data);
count = 4; count = 4;
break; break;
case OPERAND_QUADWORD_SIZE: case OPERAND_QUADWORD_SIZE:
value = *reinterpret_cast<int32_t*>(data); value = Imm32(data);
count = 4; count = 4;
break; break;
default: default:
...@@ -763,7 +784,7 @@ int DisassemblerX64::JumpShort(byte* data) { ...@@ -763,7 +784,7 @@ int DisassemblerX64::JumpShort(byte* data) {
int DisassemblerX64::JumpConditional(byte* data) { int DisassemblerX64::JumpConditional(byte* data) {
DCHECK_EQ(0x0F, *data); DCHECK_EQ(0x0F, *data);
byte cond = *(data + 1) & 0x0F; byte cond = *(data + 1) & 0x0F;
byte* dest = data + *reinterpret_cast<int32_t*>(data + 2) + 6; byte* dest = data + Imm32(data + 2) + 6;
const char* mnem = conditional_code_suffix[cond]; const char* mnem = conditional_code_suffix[cond];
AppendToBuffer("j%s %s", mnem, NameOfAddress(dest)); AppendToBuffer("j%s %s", mnem, NameOfAddress(dest));
return 6; // includes 0x0F return 6; // includes 0x0F
...@@ -2421,18 +2442,15 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2421,18 +2442,15 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
byte* addr = nullptr; byte* addr = nullptr;
switch (operand_size()) { switch (operand_size()) {
case OPERAND_WORD_SIZE: case OPERAND_WORD_SIZE:
addr = addr = reinterpret_cast<byte*>(Imm16(data + 1));
reinterpret_cast<byte*>(*reinterpret_cast<int16_t*>(data + 1));
data += 3; data += 3;
break; break;
case OPERAND_DOUBLEWORD_SIZE: case OPERAND_DOUBLEWORD_SIZE:
addr = addr = reinterpret_cast<byte*>(Imm32_U(data + 1));
reinterpret_cast<byte*>(*reinterpret_cast<uint32_t*>(data + 1));
data += 5; data += 5;
break; break;
case OPERAND_QUADWORD_SIZE: case OPERAND_QUADWORD_SIZE:
addr = addr = reinterpret_cast<byte*>(Imm64(data + 1));
reinterpret_cast<byte*>(*reinterpret_cast<int64_t*>(data + 1));
data += 9; data += 9;
break; break;
default: default:
...@@ -2445,7 +2463,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2445,7 +2463,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
} }
case CALL_JUMP_INSTR: { case CALL_JUMP_INSTR: {
byte* addr = data + *reinterpret_cast<int32_t*>(data + 1) + 5; byte* addr = data + Imm32(data + 1) + 5;
AppendToBuffer("%s %s", idesc.mnem, NameOfAddress(addr)); AppendToBuffer("%s %s", idesc.mnem, NameOfAddress(addr));
data += 5; data += 5;
break; break;
...@@ -2454,10 +2472,10 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2454,10 +2472,10 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
case SHORT_IMMEDIATE_INSTR: { case SHORT_IMMEDIATE_INSTR: {
int32_t imm; int32_t imm;
if (operand_size() == OPERAND_WORD_SIZE) { if (operand_size() == OPERAND_WORD_SIZE) {
imm = *reinterpret_cast<int16_t*>(data + 1); imm = Imm16(data + 1);
data += 3; data += 3;
} else { } else {
imm = *reinterpret_cast<int32_t*>(data + 1); imm = Imm32(data + 1);
data += 5; data += 5;
} }
AppendToBuffer("%s rax,0x%x", idesc.mnem, imm); AppendToBuffer("%s rax,0x%x", idesc.mnem, imm);
...@@ -2478,7 +2496,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2478,7 +2496,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
if (!processed) { if (!processed) {
switch (*data) { switch (*data) {
case 0xC2: case 0xC2:
AppendToBuffer("ret 0x%x", *reinterpret_cast<uint16_t*>(data + 1)); AppendToBuffer("ret 0x%x", Imm16_U(data + 1));
data += 3; data += 3;
break; break;
...@@ -2562,12 +2580,10 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2562,12 +2580,10 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
AppendToBuffer("mov%c ", operand_size_code()); AppendToBuffer("mov%c ", operand_size_code());
data += PrintRightOperand(data); data += PrintRightOperand(data);
if (operand_size() == OPERAND_WORD_SIZE) { if (operand_size() == OPERAND_WORD_SIZE) {
int16_t imm = *reinterpret_cast<int16_t*>(data); AppendToBuffer(",0x%x", Imm16(data));
AppendToBuffer(",0x%x", imm);
data += 2; data += 2;
} else { } else {
int32_t imm = *reinterpret_cast<int32_t*>(data); AppendToBuffer(",0x%x", Imm32(data));
AppendToBuffer(",0x%x", imm);
data += 4; data += 4;
} }
} }
...@@ -2663,12 +2679,12 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2663,12 +2679,12 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
break; break;
} }
case 0x68: case 0x68:
AppendToBuffer("push 0x%x", *reinterpret_cast<int32_t*>(data + 1)); AppendToBuffer("push 0x%x", Imm32(data + 1));
data += 5; data += 5;
break; break;
case 0x6A: case 0x6A:
AppendToBuffer("push 0x%x", *reinterpret_cast<int8_t*>(data + 1)); AppendToBuffer("push 0x%x", Imm8(data + 1));
data += 2; data += 2;
break; break;
...@@ -2676,8 +2692,8 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2676,8 +2692,8 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
case 0xA3: case 0xA3:
switch (operand_size()) { switch (operand_size()) {
case OPERAND_DOUBLEWORD_SIZE: { case OPERAND_DOUBLEWORD_SIZE: {
const char* memory_location = NameOfAddress( const char* memory_location =
reinterpret_cast<byte*>(*reinterpret_cast<int32_t*>(data + 1))); NameOfAddress(reinterpret_cast<byte*>(Imm32(data + 1)));
if (*data == 0xA1) { // Opcode 0xA1 if (*data == 0xA1) { // Opcode 0xA1
AppendToBuffer("movzxlq rax,(%s)", memory_location); AppendToBuffer("movzxlq rax,(%s)", memory_location);
} else { // Opcode 0xA3 } else { // Opcode 0xA3
...@@ -2689,7 +2705,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2689,7 +2705,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
case OPERAND_QUADWORD_SIZE: { case OPERAND_QUADWORD_SIZE: {
// New x64 instruction mov rax,(imm_64). // New x64 instruction mov rax,(imm_64).
const char* memory_location = const char* memory_location =
NameOfAddress(*reinterpret_cast<byte**>(data + 1)); NameOfAddress(reinterpret_cast<byte*>(Imm64(data + 1)));
if (*data == 0xA1) { // Opcode 0xA1 if (*data == 0xA1) { // Opcode 0xA1
AppendToBuffer("movq rax,(%s)", memory_location); AppendToBuffer("movq rax,(%s)", memory_location);
} else { // Opcode 0xA3 } else { // Opcode 0xA3
...@@ -2705,7 +2721,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2705,7 +2721,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
break; break;
case 0xA8: case 0xA8:
AppendToBuffer("test al,0x%x", *reinterpret_cast<uint8_t*>(data + 1)); AppendToBuffer("test al,0x%x", Imm8_U(data + 1));
data += 2; data += 2;
break; break;
...@@ -2713,15 +2729,15 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2713,15 +2729,15 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
int64_t value = 0; int64_t value = 0;
switch (operand_size()) { switch (operand_size()) {
case OPERAND_WORD_SIZE: case OPERAND_WORD_SIZE:
value = *reinterpret_cast<uint16_t*>(data + 1); value = Imm16_U(data + 1);
data += 3; data += 3;
break; break;
case OPERAND_DOUBLEWORD_SIZE: case OPERAND_DOUBLEWORD_SIZE:
value = *reinterpret_cast<uint32_t*>(data + 1); value = Imm32_U(data + 1);
data += 5; data += 5;
break; break;
case OPERAND_QUADWORD_SIZE: case OPERAND_QUADWORD_SIZE:
value = *reinterpret_cast<int32_t*>(data + 1); value = Imm32(data + 1);
data += 5; data += 5;
break; break;
default: default:
...@@ -2764,7 +2780,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer, ...@@ -2764,7 +2780,7 @@ int DisassemblerX64::InstructionDecode(v8::internal::Vector<char> out_buffer,
break; break;
case 0x3C: case 0x3C:
AppendToBuffer("cmp al,0x%x", *reinterpret_cast<int8_t*>(data + 1)); AppendToBuffer("cmp al,0x%x", Imm8(data + 1));
data += 2; data += 2;
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment