Skip to content

Commit a9732a2

Browse files
committed
[MC][X86/m68k] Emit syntax directive for AT&T
This eases interoperability by making it explicit in emitted assembly code which syntax is used. Refactored to remove X86-specific directives and logic from the generic MC(Asm)Streamer. Motivated by building LLVM with `-mllvm -x86-asm-syntax=intel` (i.e. a global preference for Intel syntax). A Bolt test (`runtime/X86/fdata-escape-chars.ll`) was using `llc` to compile to assembly and then assembling with `clang`. The specific option causes Clang to assume Intel syntax but only for assembly and not inline assembly.
1 parent 96a5289 commit a9732a2

File tree

8 files changed

+30
-13
lines changed

8 files changed

+30
-13
lines changed

llvm/docs/ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ Changes to the X86 Backend
152152

153153
* `-mcpu=wildcatlake` is now supported.
154154
* `-mcpu=novalake` is now supported.
155+
* `.att_syntax` is now emitted at the beginning of the file when emitting AT&T
156+
syntax assembly.
155157

156158
Changes to the OCaml bindings
157159
-----------------------------

llvm/include/llvm/MC/MCStreamer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ class LLVM_ABI MCStreamer {
10551055
/// Get the .xdata section used for the given section.
10561056
MCSection *getAssociatedXDataSection(const MCSection *TextSec);
10571057

1058-
virtual void emitSyntaxDirective();
1058+
virtual void emitSyntaxDirective(StringRef Syntax, StringRef Options);
10591059

10601060
/// Record a relocation described by the .reloc directive.
10611061
virtual void emitRelocDirective(const MCExpr &Offset, StringRef Name,

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class MCAsmStreamer final : public MCStreamer {
123123
EmitCommentsAndEOL();
124124
}
125125

126-
void emitSyntaxDirective() override;
126+
void emitSyntaxDirective(StringRef Syntax, StringRef Options) override;
127127

128128
void EmitCommentsAndEOL();
129129

@@ -796,14 +796,12 @@ void MCAsmStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
796796
EmitEOL();
797797
}
798798

799-
void MCAsmStreamer::emitSyntaxDirective() {
800-
if (MAI->getAssemblerDialect() == 1) {
801-
OS << "\t.intel_syntax noprefix";
802-
EmitEOL();
799+
void MCAsmStreamer::emitSyntaxDirective(StringRef Syntax, StringRef Options) {
800+
OS << "\t." << Syntax << "_syntax";
801+
if (!Options.empty()) {
802+
OS << " " << Options;
803803
}
804-
// FIXME: Currently emit unprefix'ed registers.
805-
// The intel_syntax directive has one optional argument
806-
// with may have a value of prefix or noprefix.
804+
EmitEOL();
807805
}
808806

809807
void MCAsmStreamer::beginCOFFSymbolDef(const MCSymbol *Symbol) {

llvm/lib/MC/MCStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ MCSection *MCStreamer::getAssociatedXDataSection(const MCSection *TextSec) {
876876
TextSec);
877877
}
878878

879-
void MCStreamer::emitSyntaxDirective() {}
879+
void MCStreamer::emitSyntaxDirective(StringRef Syntax, StringRef Options) {}
880880

881881
static unsigned encodeSEHRegNum(MCContext &Ctx, MCRegister Reg) {
882882
return Ctx.getRegisterInfo()->getSEHRegNum(Reg);

llvm/lib/Target/M68k/M68kAsmPrinter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ void M68kAsmPrinter::emitFunctionBodyStart() {}
190190
void M68kAsmPrinter::emitFunctionBodyEnd() {}
191191

192192
void M68kAsmPrinter::emitStartOfAsmFile(Module &M) {
193-
OutStreamer->emitSyntaxDirective();
193+
// m68k assemblers generally don't support .att_syntax so we only emit the
194+
// directive for Intel syntax.
195+
if (MAI->getAssemblerDialect() == InlineAsm::AD_Intel) {
196+
OutStreamer->emitSyntaxDirective("intel", "noprefix");
197+
}
194198
}
195199

196200
void M68kAsmPrinter::emitEndOfAsmFile(Module &M) {}

llvm/lib/Target/X86/X86AsmPrinter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,13 @@ void X86AsmPrinter::emitStartOfAsmFile(Module &M) {
932932
if (M.getModuleFlag("import-call-optimization"))
933933
EnableImportCallOptimization = true;
934934
}
935-
OutStreamer->emitSyntaxDirective();
935+
936+
// FIXME: Currently emit unprefix'ed registers.
937+
// The intel_syntax directive has one optional argument
938+
// with may have a value of prefix or noprefix.
939+
const bool IntelSyntax = MAI->getAssemblerDialect() == InlineAsm::AD_Intel;
940+
OutStreamer->emitSyntaxDirective(IntelSyntax ? "intel" : "att",
941+
IntelSyntax ? "noprefix" : "");
936942

937943
// If this is not inline asm and we're in 16-bit
938944
// mode prefix assembly with .code16.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
;; Make sure that we always emit an assembly syntax directive for X86.
2+
; RUN: llc < %s -mtriple=x86_64 | FileCheck %s --check-prefix=ATT
3+
; RUN: llc < %s -mtriple=x86_64 -x86-asm-syntax=att | FileCheck %s --check-prefix=ATT
4+
; RUN: llc < %s -mtriple=x86_64 -x86-asm-syntax=intel | FileCheck %s --check-prefix=INTEL
5+
6+
; INTEL: .intel_syntax noprefix
7+
; ATT: .att_syntax

llvm/test/CodeGen/X86/coal-sections.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
; Check that *coal* sections are not emitted.
44

55
; CHECK: .section __TEXT,__text,regular,pure_instructions{{$}}
6-
; CHECK-NEXT: .globl _foo
6+
; CHECK: .globl _foo
77

88
; CHECK: .section __TEXT,__const{{$}}
99
; CHECK-NEXT: .globl _a

0 commit comments

Comments
 (0)