How to generate assembly code with clang in Intel syntax? - c++

As this question shows, with g++, I can do g++ -S -masm=intel test.cpp.
Also, with clang, I can do clang++ -S test.cpp, but -masm=intel is not supported by clang (warning argument unused during compilation: -masm=intel). How do I get intel syntax with clang?

As noted below by #thakis, newer versions of Clang (3.5+) accept the -masm=intel argument.
For older versions, this should get clang to emit assembly code with Intel syntax:
clang++ -S -mllvm --x86-asm-syntax=intel test.cpp
You can use -mllvm <arg> to pass in llvm options from the clang command line. Sadly this option doesn't appear to be well documented, and thus I only found it by browsing through the llvm mailing lists.

As of clang r208683 (clang 3.5+), it understands -masm=intel. So if your clang is new enough, you can just use that.

Presuming you can have Clang emit normal LLVM byte codes, you can then use llc to compile to assembly language, and use its --x86-asm-syntax=intel option to get the result in Intel syntax.

Related

Weird issues with Clang and C++, and uniform initialization

I was attempting to compile and run the example on this page that explores function pointers as a function input. The example I was running was the 66 line one about halfway down the page.
https://www.learncpp.com/cpp-tutorial/function-pointers/
I am on Mac iOS 12.3.1. I tried to compile with
g++ sort.cc
and was getting errors that no semicolons were in my for loops, i believe due to the bracket initialization throughout the code. And when I run it with:
g++ -std=c++11 sort.cc
It works fine.
BUT
Shouldn't my clang be compiling at at least C++11? running
clang -v
I get
Apple clang version 13.1.6 (clang-1316.0.21.2)
Target: x86_64-apple-darwin21.4.0
And from what I can tell clang versions past 4 default to c++14.
Also, when I run using clang or gcc I get errors setting -std=c++xx, but it works fine with g++. But as far as I can tell, g++ and gcc are aliases to clang, and running gcc -v or g++ -v gives me clang version 13.1.6.
So whats going on?
Xcode clang defaults to C++98. Compiling a simple program which has C++11 or later features will tell you that C++11 or later isn't the default. e.g.,
// a.cpp
constexpr int i = 10;
clang a.cpp -c
a.cpp:1:1: error: unknown type name 'constexpr'
constexpr int i = 10;
^
1 error generated.
while clang a.cpp -c -std=c++11 works fine.
Also see: https://trac.macports.org/wiki/CompilerSelection

Why does MinGW GCC tolerate missing return types even with `-pedantic-errors`, unlike regular GCC?

Here's the code:
main() {}
On gcc.godbolt.org, both GCC 10.1 and Clang 10 (with -Wall -Wextra -pedantic-errors -std=c++20) refuse to compile this.
GCC: error: ISO C++ forbids declaration of 'main' with no type [-Wpedantic]
Clang: error: C++ requires a type specifier for all declarations
But on my local machine, MinGW GCC happily accepts this code without any errors or warnings (with the same flags). This is not something new; this specific peculiarity was there for years.
Why does MinGW GCC behave differntly from the regular GCC in this case? Are there any flags to make it diagnose this error?
I got my GCC from MSYS2. It identifies as
# g++ --version
g++.exe (Rev3, Built by MSYS2 project) 10.1.0
Copyright (C) 2020 Free Software Foundation, Inc.
Clang 10 on the same machine does reject the code (the official binary, using libstdc++ from this GCC).
As noted by #ssbssa, MinGW GCC enables -fms-extensions by default, and it's one of the effects of that flag.
Compile with -fno-ms-extensions to fix that.

LLVM opt tool does not vectorize IR generated by clang -O0

I'm trying to build JIT compiler based on optimization pipeline borrowed from opt tool.
However I stuck with a problem that my JIT does not vectorize the code.
I tried to reproduce it with opt on simple example here. https://godbolt.org/z/eRKrLa
In that example clang -O3 emits vectorized IR, however if I try to optimize IR generated by clang -O0 it does not do any changes.
What am I doing wrong?
This is expected. The output of clang -O0 is not intended to be re-optimized. You'd need to do something like clang -O3 -mllvm -disable-llvm-optzns

How to force clang use llvm assembler instead of system?

I'm working on LLVM/Clang fork (for AVR). How can i force Clang Driver to use LLVM assembler instead of system one?
MBA-Anton:bin asmirnov$ ./clang++
-I/Applications/avr.app/Contents/Resources/Java/hardware/avr/cores/avr -I/Applications/avr.app/Contents/Resources/Java/hardware/avr/variants/standard
/var/folders/64/fwfkm1k51zbd4_c5lwpsbljh0000gn/T/build5450310618632673119.tmp/Blink.cpp
-o /tmp/Blink.avr.hex -I /Applications/avr.app//Contents/Resources/Java/hardware/tools/avr/avr/include/
--target=avr -O1 -v
And it uses LLVM clang compiler (not system, correct):
clang version 3.6.0 (https://github.com/4ntoine/clang.git 0d08deedd548d964f63cf896ae9acb8d878a5fd8) (https://github.com/dylanmckay/avr-llvm.git 447b58bced825fa7bea31f3882f277535cc9fca6)
Target: avr
Thread model: posix
"/Users/asmirnov/Documents/dev/src/llvm_dylan/installed/bin/clang" -cc1 ...
But system assembler (incorrect):
"/usr/bin/as" -o ...
It should use LLVM's llvm-as as it "knows" AVR target (opcodes, etc). How can i force it use LLVM's assembler?
You can use clang to emit LLVM IR code by using the option -emit-llvm.
Let's say you want to compile a C file into LLVM IR:
clang -emit-llvm -c file.c
It will create a LLVM bytecode file file.bc. You will be able to compile the file with llc.

How can I get clang++ installed by macports to use a non-system libc++

clang++-mp-X.Y seems to trip up when told to use libc++
clang++-mp-X.Y -std=c++0x -std=libc++ SOME_SOURCE_FILE
Often generates errors which I do not see when using
clang++- -std=c++0x -std=libc++ SOME_SOURCE_FILE
I'm guessing that the system libc++ and system clang (XCode 4.2.1) are made for each other. How can I get macports's clang to use a version of libc++ that it will work with?
From libc++:
To use your tip-of-trunk libc++ on Mac OS with clang you can:
export DYLD_LIBRARY_PATH=<path-to-libcxx>/lib
clang++ -std=c++11 -stdlib=libc++ -nostdinc++ -I<path-to-libcxx>/include -L<path-to-libcxx>/lib test.cpp