Skip to content

Commit

Permalink
[llvm-objdump] Add -M {att,intel} & deprecate --x86-asm-syntax={att,i…
Browse files Browse the repository at this point in the history
…ntel}

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
  • Loading branch information
MaskRay committed May 5, 2021
1 parent cab3c6c commit e510860
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
11 changes: 9 additions & 2 deletions llvm/docs/CommandGuide/llvm-objdump.rst
Expand Up @@ -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>

Expand Down Expand Up @@ -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:

Expand Down
4 changes: 4 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Expand Up @@ -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
---------------------------------

Expand Down
@@ -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 -M 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 -M 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.
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/tools/llvm-objdump/X86/syntax-mode.s
@@ -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
44 changes: 29 additions & 15 deletions llvm/tools/llvm-objdump/llvm-objdump.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit e510860

Please sign in to comment.