Skip to content

Commit

Permalink
[llvm-objdump] Print comments for the disassembled code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
igorkudrin committed Jun 28, 2021
1 parent 04242bd commit abe0fa4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 21 deletions.
8 changes: 5 additions & 3 deletions llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s
Expand Up @@ -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
@@ -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
29 changes: 15 additions & 14 deletions llvm/test/tools/llvm-objdump/X86/disassemble-align.s
@@ -1,26 +1,27 @@
# 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: 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>
# CHECK: 0: c3 |retq
# 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: 12: |lwpval|$0x0, 0x40(%rdx,%rax), %r15d
# NORAW-NEXT: 1d: |lwpins|$0x0, 0x1cf01cf0, %r15d
# NORAW-NEXT: 2b: |<unknown>
# NORAW: 0: |retq
# 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>

.text
retq
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/tools/llvm-objdump/X86/disassemble-print-comments.s
@@ -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
35 changes: 31 additions & 4 deletions llvm/tools/llvm-objdump/llvm-objdump.cpp
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit abe0fa4

Please sign in to comment.