[llvm-objdump] Add -M {att,intel} & deprecate --x86-asm-syntax={att,intel}
authorFangrui Song <i@maskray.me>
Wed, 5 May 2021 07:20:41 +0000 (00:20 -0700)
committerFangrui Song <i@maskray.me>
Wed, 5 May 2021 07:20:41 +0000 (00:20 -0700)
The internal `cl::opt` option --x86-asm-syntax sets the AsmParser and AsmWriter
dialect. The option is used by llc and llvm-mc tests to set the AsmWriter dialect.

This patch adds -M {att,intel} as GNU objdump compatible aliases (PR43413).

Note: the dialect is initialized when the MCAsmInfo is constructed.
`MCInstPrinter::applyTargetSpecificCLOption` is called too late and its MCAsmInfo
reference is const, so changing the `cl::opt` in
`MCInstPrinter::applyTargetSpecificCLOption` is not an option, at least without
large amount of refactoring.

Reviewed By: hoy, jhenderson, thakis

Differential Revision: https://reviews.llvm.org/D101695

llvm/docs/CommandGuide/llvm-objdump.rst
llvm/docs/ReleaseNotes.rst
llvm/test/tools/llvm-objdump/X86/elf-disassemble-symbololize-operands.yaml
llvm/test/tools/llvm-objdump/X86/syntax-mode.s [new file with mode: 0644]
llvm/tools/llvm-objdump/llvm-objdump.cpp

index cb2e452..4f6a633 100644 (file)
@@ -146,8 +146,14 @@ OPTIONS
 
 .. option:: -M, --disassembler-options=<opt1[,opt2,...]>
 
-  Pass target-specific disassembler options. Currently supported for ARM targets
-  only. Available options are ``reg-names-std`` and ``reg-names-raw``.
+  Pass target-specific disassembler options. Available options:
+
+  * ``reg-names-std``: ARM only (default). Print in ARM 's instruction set documentation, with r13/r14/r15 replaced by sp/lr/pc.
+  * ``reg-names-raw``: ARM only. Use r followed by the register number.
+  * ``no-aliases``: RISC-V only. Print raw instruction mnemonic instead of pesudo instruction mnemonic.
+  * ``numeric``: RISC-V only. Print raw register names instead of ABI mnemonic. (e.g. print x1 instead of ra)
+  * ``att``: x86 only (default). Print in the AT&T syntax.
+  * ``intel``: x86 only. Print in the intel syntax.
 
 .. option:: --mcpu=<cpu-name>
 
@@ -242,6 +248,7 @@ OPTIONS
 
 .. option:: --x86-asm-syntax=<style>
 
+  Deprecated.
   When used with :option:`--disassemble`, choose style of code to emit from
   X86 backend. Supported values are:
 
index 93be668..042189e 100644 (file)
@@ -141,6 +141,10 @@ Changes to the LLVM tools
 * Support for in-order processors has been added to ``llvm-mca``.
   (`D94928 <https://reviews.llvm.org/D94928>`_)
 
+* llvm-objdump supports ``-M {att,intel}`` now.
+  ``--x86-asm-syntax`` is a deprecated internal option which will be removed in LLVM 14.0.0.
+  (`D101695 <https://reviews.llvm.org/D101695>`_)
+
 Changes to LLDB
 ---------------------------------
 
index afce86a..959b6d8 100644 (file)
@@ -1,7 +1,7 @@
 # RUN: yaml2obj %s -o %t
-# RUN: llvm-objdump %t -d --symbolize-operands --x86-asm-syntax=intel --no-show-raw-insn --no-leading-addr | \
+# RUN: llvm-objdump %t -d --symbolize-operands -intel --no-show-raw-insn --no-leading-addr | \
 # RUN:   FileCheck %s --match-full-lines --check-prefix=INTEL
-# RUN: llvm-objdump %t -d --symbolize-operands --x86-asm-syntax=att --no-show-raw-insn --no-leading-addr | \
+# RUN: llvm-objdump %t -d --symbolize-operands -att --no-show-raw-insn --no-leading-addr | \
 # RUN:   FileCheck %s --match-full-lines --check-prefix=ATT
 
 ## Expect to find the branch labels and global variable name.
diff --git a/llvm/test/tools/llvm-objdump/X86/syntax-mode.s b/llvm/test/tools/llvm-objdump/X86/syntax-mode.s
new file mode 100644 (file)
index 0000000..df1e063
--- /dev/null
@@ -0,0 +1,25 @@
+## Test att and intel syntax modes.
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t
+# RUN: llvm-objdump -d %t | FileCheck %s --check-prefix=ATT
+# RUN: llvm-objdump -d -M att %t | FileCheck %s --check-prefix=ATT
+# RUN: llvm-objdump -dMintel %t | FileCheck %s --check-prefix=INTEL
+# RUN: llvm-objdump -d --disassembler-options=intel %t | FileCheck %s --check-prefix=INTEL
+
+## The last wins.
+# RUN: llvm-objdump -dM att -M att,intel %t | FileCheck %s --check-prefix=INTEL
+
+## Test discouraged internal cl::opt options.
+# RUN: llvm-objdump -d --x86-asm-syntax=att %t | FileCheck %s --check-prefix=ATT
+# RUN: llvm-objdump -d --x86-asm-syntax=intel %t | FileCheck %s --check-prefix=INTEL
+
+# ATT: movw $1, %ax
+# ATT: imull %esi, %edi
+# ATT: leaq 5(%rsi,%rdi,4), %rax
+
+# INTEL: mov ax, 1
+# INTEL: imul edi, esi
+# INTEL: lea rax, [rsi + 4*rdi + 5]
+
+  movw $1, %ax
+  imull %esi, %edi
+  leaq 5(%rsi,%rdi,4), %rax
index e466364..8dbd88f 100644 (file)
@@ -2419,8 +2419,6 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
   DisassembleSymbols =
       commaSeparatedValues(InputArgs, OBJDUMP_disassemble_symbols_EQ);
   DisassembleZeroes = InputArgs.hasArg(OBJDUMP_disassemble_zeroes);
-  DisassemblerOptions =
-      commaSeparatedValues(InputArgs, OBJDUMP_disassembler_options_EQ);
   if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_dwarf_EQ)) {
     DwarfDumpType =
         StringSwitch<DIDumpType>(A->getValue()).Case("frames", DIDT_DebugFrame);
@@ -2466,24 +2464,40 @@ static void parseObjdumpOptions(const llvm::opt::InputArgList &InputArgs) {
 
   parseMachOOptions(InputArgs);
 
-  // Handle options that get forwarded to cl::opt<>s in libraries.
-  // FIXME: Depending on https://reviews.llvm.org/D84191#inline-946075 ,
-  // hopefully remove this again.
-  std::vector<const char *> LLVMArgs;
-  LLVMArgs.push_back("llvm-objdump (LLVM option parsing)");
-  if (const opt::Arg *A = InputArgs.getLastArg(OBJDUMP_x86_asm_syntax_att,
-                                               OBJDUMP_x86_asm_syntax_intel)) {
+  // Parse -M (--disassembler-options) and deprecated
+  // --x86-asm-syntax={att,intel}.
+  //
+  // Note, for x86, the asm dialect (AssemblerDialect) is initialized when the
+  // MCAsmInfo is constructed. MCInstPrinter::applyTargetSpecificCLOption is
+  // called too late. For now we have to use the internal cl::opt option.
+  const char *AsmSyntax = nullptr;
+  for (const auto *A : InputArgs.filtered(OBJDUMP_disassembler_options_EQ,
+                                          OBJDUMP_x86_asm_syntax_att,
+                                          OBJDUMP_x86_asm_syntax_intel)) {
     switch (A->getOption().getID()) {
     case OBJDUMP_x86_asm_syntax_att:
-      LLVMArgs.push_back("--x86-asm-syntax=att");
-      break;
+      AsmSyntax = "--x86-asm-syntax=att";
+      continue;
     case OBJDUMP_x86_asm_syntax_intel:
-      LLVMArgs.push_back("--x86-asm-syntax=intel");
-      break;
+      AsmSyntax = "--x86-asm-syntax=intel";
+      continue;
     }
+
+    SmallVector<StringRef, 2> Values;
+    llvm::SplitString(A->getValue(), Values, ",");
+    for (StringRef V : Values) {
+      if (V == "att")
+        AsmSyntax = "--x86-asm-syntax=att";
+      else if (V == "intel")
+        AsmSyntax = "--x86-asm-syntax=intel";
+      else
+        DisassemblerOptions.push_back(V.str());
+    }
+  }
+  if (AsmSyntax) {
+    const char *Argv[] = {"llvm-objdump", AsmSyntax};
+    llvm::cl::ParseCommandLineOptions(2, Argv);
   }
-  LLVMArgs.push_back(nullptr);
-  llvm::cl::ParseCommandLineOptions(LLVMArgs.size() - 1, LLVMArgs.data());
 
   // objdump defaults to a.out if no filenames specified.
   if (InputFilenames.empty())