[PATCH] D60376: [llvm-objdump] Align instructions to a tab stop and accept (and ignore) -w/--wide

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 7 06:02:01 PDT 2019


MaskRay created this revision.
MaskRay added reviewers: rupprecht, jhenderson, grimar.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
MaskRay updated this revision to Diff 194059.
MaskRay added a comment.

use sed y

not sure if `tr` is usable (not used by any test yet and not listed on https://llvm.org/docs/GettingStarted.html) so I use sed here.


Align instructions to improve readability.
This is similar to what GNU objdump -w does.
Since the narrow output is not very useful, don't implement it.

Also accept but ignore -w/--wide. llvm-readobj does the same.


Repository:
  rL LLVM

https://reviews.llvm.org/D60376

Files:
  test/tools/llvm-objdump/X86/disassemble-align.s
  tools/llvm-objdump/llvm-objdump.cpp


Index: tools/llvm-objdump/llvm-objdump.cpp
===================================================================
--- tools/llvm-objdump/llvm-objdump.cpp
+++ tools/llvm-objdump/llvm-objdump.cpp
@@ -303,6 +303,10 @@
                              cl::CommaSeparated,
                              cl::aliasopt(DisassemblerOptions));
 
+static cl::opt<bool>
+    Wide("wide", cl::desc("Ignored for compatibility with GNU objdump"));
+static cl::alias WideShort("w", cl::Grouping, cl::aliasopt(Wide));
+
 static StringRef ToolName;
 
 typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy;
@@ -602,16 +606,25 @@
                          std::vector<RelocationRef> *Rels = nullptr) {
     if (SP && (PrintSource || PrintLines))
       SP->printSourceLine(OS, Address);
-    if (!NoLeadingAddr)
+    unsigned Column = 0;
+    if (!NoLeadingAddr) {
       OS << format("%8" PRIx64 ":", Address.Address);
+      Column += 9;
+    }
     if (!NoShowRawInsn) {
       OS << "\t";
+      Column = (Column + 7) & -8;
       dumpBytes(Bytes, OS);
+      Column += 3 * Bytes.size();
     }
+    // The output of printInst always starts with a tab. Print some spaces so
+    // that the tab advances to a tab stop.
+    unsigned TabStop = NoShowRawInsn ? 16 : 40;
+    OS.indent(Column < TabStop - 1 ? TabStop - 1 - Column : 7 - (Column & 7));
     if (MI)
       IP.printInst(MI, OS, "", STI);
     else
-      OS << " <unknown>";
+      OS << "\t<unknown>";
   }
 };
 PrettyPrinter PrettyPrinterInst;
Index: test/tools/llvm-objdump/X86/disassemble-align.s
===================================================================
--- /dev/null
+++ test/tools/llvm-objdump/X86/disassemble-align.s
@@ -0,0 +1,30 @@
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s | \
+# RUN:   llvm-objdump -d -print-imm-hex - | sed 'y/\t/ /' | FileCheck -strict-whitespace %s
+
+# -w/--wide is ignored. llvm-objdump always produces wide output.
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s | \
+# RUN:   llvm-objdump -wd -print-imm-hex - | sed 'y/\t/ /' | FileCheck -strict-whitespace %s
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s | \
+# RUN:   llvm-objdump --wide -d -print-imm-hex - | sed 's/\t/ /g' | FileCheck -strict-whitespace %s
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s | \
+# RUN:   llvm-objdump -d -print-imm-hex -no-show-raw-insn - | sed 's/\t/ /g' | \
+# 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: 55                      pushq %rbp
+# 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: c3                      retq
+
+# NORAW:       0:       pushq %rbp
+# NORAW-NEXT:  1:       movq 0x123456(%rip), %rax
+# NORAW-NEXT:  8:       movabsq $0x5555555555555554, %rax
+# NORAW-NEXT: 12:       retq
+
+.text
+  pushq %rbp
+  movq 0x123456(%rip),%rax
+  movabs $0x5555555555555554,%rax
+  ret


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60376.194059.patch
Type: text/x-patch
Size: 3081 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190407/81ad737e/attachment.bin>


More information about the llvm-commits mailing list