Skip to content

Commit 4a9da96

Browse files
authored
[clang] Add cc1 --output-asm-variant= to set output syntax
2fcaa54 (2010) added cc1as option `-output-asm-variant` (untested) to set the output syntax. `clang -cc1as -filetype asm -output-asm-variant 1` allows AT&T input and Intel output (`AssemblerDialect` is also used by non-x86 targets). This patch renames the cc1as option (to avoid collision with -o) and makes it available for cc1 to set output syntax. This allows different input & output syntax: ``` echo 'asm("mov $1, %eax");' | clang -xc - -S -o - -Xclang --output-asm-variant=1 ``` Note: `AsmWriterFlavor` (with a misleading name), used to initialize MCAsmInfo::AssemblerDialect, is primarily used for assembly input, not for output. Therefore, `echo 'asm("mov $1, %eax");' | clang -x c - -mllvm --x86-asm-syntax=intel -S -o -`, which achieves a similar goal before Clang 19, was unintended. Close #109157 Pull Request: #109360
1 parent 4c4fb6a commit 4a9da96

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ CODEGENOPT(EmulatedTLS , 1, 0) ///< Set by default or -f[no-]emulated-tls.
9696
ENUM_CODEGENOPT(EmbedBitcode, EmbedBitcodeKind, 2, Embed_Off)
9797
/// Inline asm dialect, -masm=(att|intel)
9898
ENUM_CODEGENOPT(InlineAsmDialect, InlineAsmDialectKind, 1, IAD_ATT)
99+
CODEGENOPT(OutputAsmVariant, 2, 3) ///< Set the asm variant for output (3: unspecified).
99100
CODEGENOPT(ForbidGuardVariables , 1, 0) ///< Issue errors if C++ guard variables
100101
///< are required.
101102
CODEGENOPT(FunctionSections , 1, 0) ///< Set when -ffunction-sections is enabled.

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7217,6 +7217,9 @@ def fuse_ctor_homing: Flag<["-"], "fuse-ctor-homing">,
72177217
def as_secure_log_file : Separate<["-"], "as-secure-log-file">,
72187218
HelpText<"Emit .secure_log_unique directives to this filename.">,
72197219
MarshallingInfoString<CodeGenOpts<"AsSecureLogFile">>;
7220+
def output_asm_variant : Joined<["--"], "output-asm-variant=">,
7221+
HelpText<"Select the asm variant (integer) to use for output (3: unspecified)">,
7222+
MarshallingInfoInt<CodeGenOpts<"OutputAsmVariant">, "3">;
72207223

72217224
} // let Visibility = [CC1Option, CC1AsOption]
72227225

@@ -8307,8 +8310,6 @@ def filetype : Separate<["-"], "filetype">,
83078310
HelpText<"Specify the output file type ('asm', 'null', or 'obj')">;
83088311

83098312
// Transliterate Options
8310-
def output_asm_variant : Separate<["-"], "output-asm-variant">,
8311-
HelpText<"Select the asm variant index to use for output">;
83128313
def show_encoding : Flag<["-"], "show-encoding">,
83138314
HelpText<"Show instruction encoding information in transliterate mode">;
83148315
def show_inst : Flag<["-"], "show-inst">,

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
509509
Options.MCOptions.X86RelaxRelocations = CodeGenOpts.X86RelaxRelocations;
510510
Options.MCOptions.CompressDebugSections =
511511
CodeGenOpts.getCompressDebugSections();
512+
if (CodeGenOpts.OutputAsmVariant != 3) // 3 (default): not specified
513+
Options.MCOptions.OutputAsmVariant = CodeGenOpts.OutputAsmVariant;
512514
Options.MCOptions.ABIName = TargetOpts.ABI;
513515
for (const auto &Entry : HSOpts.UserEntries)
514516
if (!Entry.IsFramework &&
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// REQUIRES: x86-registered-target
2+
/// AT&T input
3+
// RUN: %clang_cc1 -triple x86_64 -S --output-asm-variant=0 %s -o - | FileCheck --check-prefix=ATT %s
4+
// RUN: %clang_cc1 -triple x86_64 -S --output-asm-variant=1 %s -o - | FileCheck --check-prefix=INTEL %s
5+
6+
/// Intel input
7+
// RUN: %clang_cc1 -triple x86_64 -S -D INTEL -mllvm -x86-asm-syntax=intel -inline-asm=intel %s -o - | FileCheck --check-prefix=INTEL %s
8+
// RUN: %clang_cc1 -triple x86_64 -S -D INTEL -mllvm -x86-asm-syntax=intel -inline-asm=intel --output-asm-variant=1 %s -o - | FileCheck --check-prefix=INTEL %s
9+
10+
// ATT: movl $1, %eax
11+
// ATT: movl $2, %eax
12+
13+
// INTEL: mov eax, 1
14+
// INTEL: mov eax, 2
15+
16+
#ifdef INTEL
17+
asm("mov eax, 1");
18+
void foo() {
19+
asm("mov eax, 2");
20+
}
21+
#else
22+
asm("mov $1, %eax");
23+
void foo() {
24+
asm("mov $2, %eax");
25+
}
26+
#endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// REQUIRES: x86-registered-target
2+
// RUN: %clang -cc1as -triple x86_64 %s -o - | FileCheck %s --check-prefix=ATT
3+
// RUN: %clang -cc1as -triple x86_64 %s --output-asm-variant=1 -o - | FileCheck %s --check-prefix=INTEL
4+
5+
// ATT: movl $1, %eax
6+
// INTEL: mov eax, 1
7+
8+
mov $1, %eax

llvm/include/llvm/MC/MCTargetOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class MCTargetOptions {
7272

7373
bool X86Sse2Avx = false;
7474

75+
std::optional<unsigned> OutputAsmVariant;
76+
7577
EmitDwarfUnwindType EmitDwarfUnwind;
7678

7779
int DwarfVersion = 0;

llvm/lib/CodeGen/LLVMTargetMachine.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ Expected<std::unique_ptr<MCStreamer>> LLVMTargetMachine::createMCStreamer(
160160
switch (FileType) {
161161
case CodeGenFileType::AssemblyFile: {
162162
MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
163-
getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
163+
getTargetTriple(),
164+
Options.MCOptions.OutputAsmVariant.value_or(MAI.getAssemblerDialect()),
165+
MAI, MII, MRI);
164166

165167
// Create a code emitter if asked to show the encoding.
166168
std::unique_ptr<MCCodeEmitter> MCE;

0 commit comments

Comments
 (0)