[clang] Make -masm=intel affect inline asm style
authorNico Weber <thakis@chromium.org>
Thu, 11 Nov 2021 19:20:02 +0000 (14:20 -0500)
committerNico Weber <thakis@chromium.org>
Wed, 17 Nov 2021 18:41:59 +0000 (13:41 -0500)
commitae98182cf7341181e4aa815c372a072dec82779f
treee0ef8d5055ae65688321297e9ee3f822b17f731d
parent68311f21eb5f065dd8d63e83d9628dd8db51a946
[clang] Make -masm=intel affect inline asm style

With this,

  void f() {  __asm__("mov eax, ebx"); }

now compiles with clang with -masm=intel.

This matches gcc.

The flag is not accepted in clang-cl mode. It has no effect on
MSVC-style `__asm {}` blocks, which are unconditionally in intel
mode both before and after this change.

One difference to gcc is that in clang, inline asm strings are
"local" while they're "global" in gcc. Building the following with
-masm=intel works with clang, but not with gcc where the ".att_syntax"
from the 2nd __asm__() is in effect until file end (or until a
".intel_syntax" somewhere later in the file):

  __asm__("mov eax, ebx");
  __asm__(".att_syntax\nmovl %ebx, %eax");
  __asm__("mov eax, ebx");

This also updates clang's intrinsic headers to work both in
-masm=att (the default) and -masm=intel modes.
The official solution for this according to "Multiple assembler dialects in asm
templates" in gcc docs->Extensions->Inline Assembly->Extended Asm
is to write every inline asm snippet twice:

    bt{l %[Offset],%[Base] | %[Base],%[Offset]}

This works in LLVM after D113932 and D113894, so use that.

(Just putting `.att_syntax` at the start of the snippet works in some but not
all cases: When LLVM interpolates in parameters like `%0`, it uses at&t or
intel syntax according to the inline asm snippet's flavor, so the `.att_syntax`
within the snippet happens to late: The interpolated-in parameter is already
in intel style, and then won't parse in the switched `.att_syntax`.)

It might be nice to invent a `#pragma clang asm_dialect push "att"` /
`#pragma clang asm_dialect pop` to be able to force asm style per snippet,
so that the inline asm string doesn't contain the same code in two variants,
but let's leave that for a follow-up.

Fixes PR21401 and PR20241.

Differential Revision: https://reviews.llvm.org/D113707
14 files changed:
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Basic/CodeGenOptions.h
clang/include/clang/Driver/Options.td
clang/lib/CodeGen/CGStmt.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/lib/Headers/immintrin.h
clang/lib/Headers/intrin.h
clang/lib/Headers/x86gprintrin.h
clang/test/CodeGen/inline-asm-intel.c [new file with mode: 0644]
clang/test/CodeGen/inline-asm-mixed-style.c
clang/test/CodeGen/ms-intrinsics-cpuid.c
clang/test/CodeGen/ms-intrinsics.c
clang/test/Driver/masm.c