1

I'm trying to find a way to set the assembly syntax that LLVM outputs (for x86 in particular). Following the Compiling to Object Code section of the Kaleidoscope tutorial, I'm outputting assembly files with

TargetMachine->addPassesToEmitFile(pass, dest, nullptr, llvm::CGFT_AssemblyFile))

But there's no option to set the output syntax.

llc has the command line option --x86-asm-syntax with which you can set the output assembly syntax (for x86), so I'm sure there must be a way to do it.

So is there a way this can be done through LLVM's C++ API?

5
  • 1
    I have no setup to test this, but looking at the code that creates TargetMachine, the Opt is made of TargetOptions, which apparently includes things like AssemblyLanguage. That's where I'd start, anyway. Commented Oct 9, 2020 at 21:24
  • @DavidWohlferd That seems like a possible solution, but LLVM 10 doesn't seem to have that. Searching for AssemblyLanguage (or similar) in the llvm include directory also returns nothing. Commented Oct 10, 2020 at 8:24
  • It's apparently supposed to be in "llvm/MC/MCTargetOptions.h", but I've no idea when it may have been added. Commented Oct 10, 2020 at 20:43
  • @DavidWohlferd Looking on github, it was added in LLVM 11, which is supposed to be coming out soon. Mind making this into an answer? Commented Oct 10, 2020 at 21:22
  • On a deeper dive into the doxygen reference, AssemblyLanguage is only referenced here where it only checks if it's equal to "masm", so this may not be what I'm looking for after all. Commented Oct 10, 2020 at 21:34

1 Answer 1

3

Here's a few things I found out by reading the LLVM source:

  • The output assembly syntax is controlled by the AssemblyDialect member in llvm::MCAsmInfo. For x86 the derived classes of llvm::MCAsmInfo can be found in X86MCAsmInfo.h and X86MCAsmInfo.cpp.
  • The member AssemblyDialect is initialized to the value of AsmWriterFlavor, which is set by the command line option x86-asm-syntax.

As far as I could tell AssemblyDialect isn't referenced anywhere else in the code base, so the only way to set it is by the x86-asm-syntax flag. For that you can use llvm::cl::ParseCommandLineOptions. Minimal example for setting the assembly syntax to intel:

#include <cassert>
#include <llvm/Support/CommandLine.h>

void set_asm_syntax_to_intel()
{
    char const *args[] = { "some-exe-name", "--x86-asm-syntax=intel" };
    auto const res = llvm::cl::ParseCommandLineOptions(std::size(args), args);
    assert(res);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.