Very hot stuff! It's like ISA-portable assembly, but with types! In particular it also it deals with calling conventions for us (since it is ISA-portable). TODO: isn't that exactly what C does? :-) LLVM IR vs C
Documentation: llvm.org/docs/LangRef.html
Example: llvm/hello.ll adapted from: llvm.org/docs/LangRef.html#module-structure but without double newline.
To execute it as mentioned at github.com/dfellis/llvm-hello-world we can either use their crazy assembly interpreter, tested on Ubuntu 22.10:This seems to use
sudo apt install llvm-runtime
lli hello.llputs from the C standard library.Or we can Lower it to assembly of the local machine:which produces:and then we can assemble link and run with gcc:or with clang:
sudo apt install llvm
llc hello.llhello.sgcc -o hello.out hello.s -no-pie
./hello.outclang -o hello.out hello.s -no-pie
./hello.outhello.s uses the GNU GAS format, which clang is highly compatible with, so both should work in general. Articles by others on the same topic
LLVM is a collection of reusable compiler infrastructure built around a typed intermediate representation. Its static single-assignment form gives each register-like value one definition, making data dependencies explicit for compiler optimization. The same intermediate representation has an in-memory form, a compact bitcode form and a readable textual form.[ref]
LLVM Language Reference Manual. Abstract, Introduction and Well-Formedness
A compiler front end translates a source programming language into the intermediate representation; optimization passes transform it while preserving the required semantics; a compiler back end produces code for a target computer architecture. Sharing the middle stages lets several source programming languages reuse the same analyses and target support. For a small textual example,
%y = add i32 %x, 1 defines a new 32-bit integer value from an earlier one. The value %y is defined once even if the source program repeatedly assigns to a source variable.