[llvm] r358474 - [llvm-objdump] Align instructions to a tab stop in disassembly output

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 15 20:56:55 PDT 2019


Author: maskray
Date: Mon Apr 15 20:56:55 2019
New Revision: 358474

URL: http://llvm.org/viewvc/llvm-project?rev=358474&view=rev
Log:
[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.

Added:
    llvm/trunk/test/tools/llvm-objdump/X86/disassemble-align.s
Modified:
    llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp

Added: llvm/trunk/test/tools/llvm-objdump/X86/disassemble-align.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/X86/disassemble-align.s?rev=358474&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/X86/disassemble-align.s (added)
+++ llvm/trunk/test/tools/llvm-objdump/X86/disassemble-align.s Mon Apr 15 20:56:55 2019
@@ -0,0 +1,29 @@
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
+# RUN: llvm-objdump -d -print-imm-hex %t | tr '\t' ' ' | FileCheck -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
+
+# 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>
+
+# 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>
+
+.text
+  retq
+  movq 0x123456(%rip),%rax
+  movabs $0x5555555555555554,%rax
+  lwpval $0x0, 0x40(%rdx,%rax), %r15d
+  lwpins $0x0, 0x1cf01cf0, %r15d
+  .word 0xffff

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=358474&r1=358473&r2=358474&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Mon Apr 15 20:56:55 2019
@@ -595,12 +595,25 @@ public:
                          std::vector<RelocationRef> *Rels = nullptr) {
     if (SP && (PrintSource || PrintLines))
       SP->printSourceLine(OS, Address);
-    if (!NoLeadingAddr)
-      OS << format("%8" PRIx64 ":", Address.Address);
-    if (!NoShowRawInsn) {
-      OS << "\t";
-      dumpBytes(Bytes, OS);
+
+    {
+      formatted_raw_ostream FOS(OS);
+      if (!NoLeadingAddr)
+        FOS << format("%8" PRIx64 ":", Address.Address);
+      if (!NoShowRawInsn) {
+        FOS << ' ';
+        dumpBytes(Bytes, FOS);
+      }
+      FOS.flush();
+      // The output of printInst starts with a tab. Print some spaces so that
+      // the tab has 1 column and advances to the target tab stop.
+      unsigned TabStop = NoShowRawInsn ? 16 : 40;
+      unsigned Column = FOS.getColumn();
+      FOS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - Column % 8);
+
+      // The dtor calls flush() to ensure the indent comes before printInst().
     }
+
     if (MI)
       IP.printInst(MI, OS, "", STI);
     else




More information about the llvm-commits mailing list