Skip to content

Commit fa860ff

Browse files
committed
[llvm-objdump] Align instructions to a tab stop in disassembly output
This relands D60376/rL358405, with the difference: sed 'y/\t/ /' -> tr '\t' ' ' BSD sed doesn't support escape characters for the 'y' command. I didn't use it in rL358405 because it was not listed at https://llvm.org/docs/GettingStarted.html#software but it should be available. Original description: In GNU objdump, -w/--wide aligns instructions in the disassembly output. This patch does the same to llvm-objdump. However, we always use the wide format (-w/--wide is ignored), because the narrow format (instructions are misaligned) is probably not very useful. In llvm-readobj, we made a similar decision: always use the wide format, accept but ignore -W/--wide. To save some columns, we change the tab before hex bytes (controlled by --[no-]show-raw-insn) to a space. llvm-svn: 358474
1 parent f10065b commit fa860ff

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
2+
# RUN: llvm-objdump -d -print-imm-hex %t | tr '\t' ' ' | FileCheck -strict-whitespace %s
3+
4+
# RUN: llvm-objdump -d -print-imm-hex -no-show-raw-insn %t | tr '\t' ' ' | \
5+
# RUN: FileCheck -check-prefix=NORAW -strict-whitespace %s
6+
7+
# Instructions are expected to be aligned if the instruction in hex is not too long.
8+
9+
# CHECK: 0: c3 retq
10+
# CHECK-NEXT: 1: 48 8b 05 56 34 12 00 movq 0x123456(%rip), %rax
11+
# CHECK-NEXT: 8: 48 b8 54 55 55 55 55 55 55 55 movabsq $0x5555555555555554, %rax
12+
# CHECK-NEXT: 12: 8f ea 00 12 4c 02 40 00 00 00 00 lwpval $0x0, 0x40(%rdx,%rax), %r15d
13+
# CHECK-NEXT: 1d: 8f ea 00 12 04 25 f0 1c f0 1c 00 00 00 00 lwpins $0x0, 0x1cf01cf0, %r15d
14+
# CHECK-NEXT: 2b: ff ff <unknown>
15+
16+
# NORAW: 0: retq
17+
# NORAW-NEXT: 1: movq 0x123456(%rip), %rax
18+
# NORAW-NEXT: 8: movabsq $0x5555555555555554, %rax
19+
# NORAW-NEXT: 12: lwpval $0x0, 0x40(%rdx,%rax), %r15d
20+
# NORAW-NEXT: 1d: lwpins $0x0, 0x1cf01cf0, %r15d
21+
# NORAW-NEXT: 2b: <unknown>
22+
23+
.text
24+
retq
25+
movq 0x123456(%rip),%rax
26+
movabs $0x5555555555555554,%rax
27+
lwpval $0x0, 0x40(%rdx,%rax), %r15d
28+
lwpins $0x0, 0x1cf01cf0, %r15d
29+
.word 0xffff

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,25 @@ class PrettyPrinter {
595595
std::vector<RelocationRef> *Rels = nullptr) {
596596
if (SP && (PrintSource || PrintLines))
597597
SP->printSourceLine(OS, Address);
598-
if (!NoLeadingAddr)
599-
OS << format("%8" PRIx64 ":", Address.Address);
600-
if (!NoShowRawInsn) {
601-
OS << "\t";
602-
dumpBytes(Bytes, OS);
598+
599+
{
600+
formatted_raw_ostream FOS(OS);
601+
if (!NoLeadingAddr)
602+
FOS << format("%8" PRIx64 ":", Address.Address);
603+
if (!NoShowRawInsn) {
604+
FOS << ' ';
605+
dumpBytes(Bytes, FOS);
606+
}
607+
FOS.flush();
608+
// The output of printInst starts with a tab. Print some spaces so that
609+
// the tab has 1 column and advances to the target tab stop.
610+
unsigned TabStop = NoShowRawInsn ? 16 : 40;
611+
unsigned Column = FOS.getColumn();
612+
FOS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - Column % 8);
613+
614+
// The dtor calls flush() to ensure the indent comes before printInst().
603615
}
616+
604617
if (MI)
605618
IP.printInst(MI, OS, "", STI);
606619
else

0 commit comments

Comments
 (0)