[llvm] abe0fa4 - [llvm-objdump] Print comments for the disassembled code
Igor Kudrin via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 28 00:27:15 PDT 2021
Author: Igor Kudrin
Date: 2021-06-28T14:25:20+07:00
New Revision: abe0fa43523502c549ff9394d28f9f29f5be0a3d
URL: https://github.com/llvm/llvm-project/commit/abe0fa43523502c549ff9394d28f9f29f5be0a3d
DIFF: https://github.com/llvm/llvm-project/commit/abe0fa43523502c549ff9394d28f9f29f5be0a3d.diff
LOG: [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
Added:
llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-print-comments.s
llvm/test/tools/llvm-objdump/X86/disassemble-print-comments.s
Modified:
llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s
llvm/test/tools/llvm-objdump/X86/disassemble-align.s
llvm/tools/llvm-objdump/llvm-objdump.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s b/llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s
index 8ad204200e544..30528d94cd339 100644
--- a/llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s
+++ b/llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-align.s
@@ -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
diff --git a/llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-print-comments.s b/llvm/test/tools/llvm-objdump/ELF/AArch64/disassemble-print-comments.s
new file mode 100644
index 0000000000000..81bafe58816c8
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/ELF/AArch64/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=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
diff --git a/llvm/test/tools/llvm-objdump/X86/disassemble-align.s b/llvm/test/tools/llvm-objdump/X86/disassemble-align.s
index 09366c9b6b645..17809d88bfbf4 100644
--- a/llvm/test/tools/llvm-objdump/X86/disassemble-align.s
+++ b/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
diff --git a/llvm/test/tools/llvm-objdump/X86/disassemble-print-comments.s b/llvm/test/tools/llvm-objdump/X86/disassemble-print-comments.s
new file mode 100644
index 0000000000000..7e5c29efaf704
--- /dev/null
+++ b/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
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index b0ca095db381e..13e5f68c6d833 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -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) {
More information about the llvm-commits
mailing list