1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00

[llvm-objdump] Print comments for the disassembled code

LLVM disassembler can generate comments for disassembled instructions.
The patch enables printing these comments for 'llvm-objdump -d'.

Differential Revision: https://reviews.llvm.org/D104699
This commit is contained in:
Igor Kudrin 2021-06-28 14:22:41 +07:00
parent 442739c347
commit 485c1c4224
5 changed files with 77 additions and 21 deletions

View File

@ -3,10 +3,12 @@
## Use '|' to show where the tabs line up.
# CHECK:0000000000000000 <$x.0>:
# CHECK-NEXT: 0: 62 10 00 91 |add|x2, x3, #4
# CHECK-NEXT: 0: 62 10 00 91 |add|x2, x3, #4 // =4
# CHECK-NEXT: 4: 1f 20 03 d5 |nop
# CHECK-EMPTY:
# CHECK-NEXT:0000000000000004 <$d.1>:
# CHECK-NEXT: 4:|ff ff 00 00|.word|0x0000ffff
# CHECK-NEXT:0000000000000008 <$d.1>:
# CHECK-NEXT: 8:|ff ff 00 00|.word|0x0000ffff
add x2, x3, #4
nop
.word 0xffff

View File

@ -0,0 +1,13 @@
## Check that 'llvm-objdump -d' prints comments generated by the disassembler.
# RUN: llvm-mc -filetype=obj -triple=aarch64 -mattr=+sve %s -o %t
# RUN: llvm-objdump -d --mattr=+sve --no-show-raw-insn %t | FileCheck %s
# CHECK: 0000000000000000 <foo>:
# CHECK-NEXT: 0: add x0, x2, #2, lsl #12 // =8192
# CHECK-NEXT: 4: add z31.d, z31.d, #65280 // =0xff00
.text
foo:
add x0, x2, 8192
add z31.d, z31.d, #65280

View File

@ -1,23 +1,24 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
# Use '|' to show where the tabs line up.
# RUN: llvm-objdump -d --print-imm-hex %t | tr '\t' '|' | FileCheck --strict-whitespace %s
# RUN: llvm-objdump -d --print-imm-hex %t | tr '\t' '|' | \
# RUN: FileCheck --match-full-lines --strict-whitespace %s
# RUN: llvm-objdump -d --print-imm-hex --no-show-raw-insn %t | tr '\t' '|' | \
# RUN: FileCheck -check-prefix=NORAW -strict-whitespace %s
# RUN: FileCheck -check-prefix=NORAW --match-full-lines -strict-whitespace %s
# Instructions are expected to be aligned if the instruction in hex is not too long.
# CHECK: 0: c3 |retq
# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 |movq|0x123456(%rip), %rax
# CHECK-NEXT: 8: 48 b8 54 55 55 55 55 55 55 55|movabsq|$0x5555555555555554, %rax
# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 |movq|0x123456(%rip), %rax # 12345e <.text+0x12345e>
# CHECK-NEXT: 8: 48 b8 54 55 55 55 55 55 55 55|movabsq|$0x5555555555555554, %rax # imm = 0x5555555555555554
# CHECK-NEXT: 12: 8f ea 00 12 4c 02 40 00 00 00 00 |lwpval|$0x0, 0x40(%rdx,%rax), %r15d
# CHECK-NEXT: 1d: 8f ea 00 12 04 25 f0 1c f0 1c 00 00 00 00 |lwpins|$0x0, 0x1cf01cf0, %r15d
# CHECK-NEXT: 2b: ff ff |<unknown>
# NORAW: 0: |retq
# NORAW-NEXT: 1: |movq|0x123456(%rip), %rax
# NORAW-NEXT: 8: |movabsq|$0x5555555555555554, %rax
# NORAW-NEXT: 1: |movq|0x123456(%rip), %rax # 12345e <.text+0x12345e>
# NORAW-NEXT: 8: |movabsq|$0x5555555555555554, %rax # imm = 0x5555555555555554
# NORAW-NEXT: 12: |lwpval|$0x0, 0x40(%rdx,%rax), %r15d
# NORAW-NEXT: 1d: |lwpins|$0x0, 0x1cf01cf0, %r15d
# NORAW-NEXT: 2b: |<unknown>

View File

@ -0,0 +1,13 @@
## Check that 'llvm-objdump -d' prints comments generated by the disassembler.
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
# CHECK: 0000000000000000 <foo>:
# CHECK-NEXT: 0: nop
# CHECK-NEXT: 1: cmpl $305419896, %eax # imm = 0x12345678
.text
foo:
nop
cmpl $0x12345678, %eax

View File

@ -1039,6 +1039,29 @@ static StringRef getSegmentName(const MachOObjectFile *MachO,
return "";
}
static void emitPostInstructionInfo(formatted_raw_ostream &FOS,
const MCAsmInfo &MAI,
const MCSubtargetInfo &STI,
StringRef Comments,
LiveVariablePrinter &LVP) {
do {
if (!Comments.empty()) {
// Emit a line of comments.
StringRef Comment;
std::tie(Comment, Comments) = Comments.split('\n');
// MAI.getCommentColumn() assumes that instructions are printed at the
// position of 8, while getInstStartColumn() returns the actual position.
unsigned CommentColumn =
MAI.getCommentColumn() - 8 + getInstStartColumn(STI);
FOS.PadToColumn(CommentColumn);
FOS << MAI.getCommentString() << ' ' << Comment;
}
LVP.printAfterInst(FOS);
FOS << '\n';
} while (!Comments.empty());
FOS.flush();
}
static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
MCContext &Ctx, MCDisassembler *PrimaryDisAsm,
MCDisassembler *SecondaryDisAsm,
@ -1396,12 +1419,14 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
LVP.update({Index, Section.getIndex()},
{Index + Size, Section.getIndex()}, Index + Size != End);
IP->setCommentStream(CommentStream);
PIP.printInst(
*IP, Disassembled ? &Inst : nullptr, Bytes.slice(Index, Size),
{SectionAddr + Index + VMAAdjustment, Section.getIndex()}, FOS,
"", *STI, &SP, Obj->getFileName(), &Rels, LVP);
FOS << CommentStream.str();
Comments.clear();
IP->setCommentStream(llvm::nulls());
// If disassembly has failed, avoid analysing invalid/incomplete
// instruction information. Otherwise, try to resolve the target
@ -1498,8 +1523,10 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
}
}
LVP.printAfterInst(FOS);
FOS << "\n";
assert(Ctx.getAsmInfo());
emitPostInstructionInfo(FOS, *Ctx.getAsmInfo(), *STI,
CommentStream.str(), LVP);
Comments.clear();
// Hexagon does this in pretty printer
if (Obj->getArch() != Triple::hexagon) {