Running clang on input from stdin


is there a way to run clang on inoputs from stdin?
i.e. I would like to run calng to encode an asm instruction like:
clang < “add r0, r1, r2\n”
I would like to get to the encoding to sdtout.
any idea how and if it can be done?


You can get started with something like

echo "add r0, r1, r2" | clang -c -x assembler - -o - | llvm-objdump -d

You’ll probably also want to tell Clang what target it’s trying to assemble, and the output will need more massaging if you only want the encoding.

Alternatively, if you have a dev-install of LLVM, the llvm-mc tool can be a bit more direct: echo "add r0, r1, r2" | llvm-mc -show-encoding -triple whatever.

I’ll check