I've been learning compiler since this year, especially LLVM that is famous compiler architecture written by C++ from Illinois university. Recently, I'm faced with a problem when i try to study backend of LLVM. So, I gonna arrange information of LLVM and around some tools firstly to refresh my confused brain again. In this entry, I would like to confirm kind of tools of LLVM.
The Clang is compiler frontend of C, C++, Objective-C and Objective-C++. Using this compiler, you can create assembly language and executing file with selecting appropriate module. So this run on LLVM, using Clang and LLVM, it provides almost function of tool chain and enable to substitute all of GCC stuck.
There are some characteristic of Clang below:
- Saving memory and high speed compiler
- Readable message
- Compatibility with GCC
And there are some option on Clang, for example below:
|1||-help||output help information|
|2||-o||specify name of output file|
|3||-c||execute only pre-process, compile, assemble|
|4||-emit-llvm||output as LLVM IR format|
|5||-S||execute only pre-process, compile|
|6||-cc1||processing as compiler without using driver|
In case of using
-S at same time, it outputs LLVM IR which human can read format. Using only
-emit-llvm, it outputs LLVM IR's bit code. So, there are more options of Clang, example of using is below:
# Make executing file using Clang $ clang -o hoge.o hoge.c # Output bit code of LLVM using Clang $ clang -emit-llvm -o hoge.bc hoge.c # Output assembly language of LLVM using Clang $ clang -emit-llvm -S -o hoge.ll hoge.c
The lli is interpreter of LLVM BitCode and compiler of JIT. lli can run on compiler of JIT with default. lli's default mode is Lazy, so if you want to be Eager, you should set
-disable-lazy into option of lli. Eager is a mode which creates all function before it execute
@main(). Lazy is a mode which set stub into each of function and do creating every time that control be moved to function. If you want to run as interpreter, you should set
-force-interpreter into option of lli.
Example of using lli below:
$ clang -emit-llvm -o hoge.bc hoge.c $ lli -force-interpreter hoge.bc
The llvm-as is the LLVM assembler. It reads a file containing human-readable LLVM assembly language, translates it to LLVM bitcode. If filename is omitted or is
-, then llvm-as reads its input from standard input. If an output file is not specified with the
-o option, then llvm-as sends its output to a file or standard output by following these rules:
- If the input is standard input, then the output is standard output.
- If the input is a file that ends with .ll, then the output file is of the same name, except that the suffix is changed to .bc.
- If the input is a file that does not end with the .ll suffix, then the output file has the same name as the input file, except that the .bc suffix is appended.
Example of using llvm-as below:
$ clang -emit-llvm -S -o hoge.s hoge.c $ llvm-as -o hoge.bc hoge.s $ lli hoge.bc
The llc compiles LLVM IR as input into assembly language for a specified architecture or object. The assembly language output can then be passed through a native assembler and linker to generate a native executable. You can specify architecture with
-march option, but if you don't specify this option, it automatically select architecture from input file.
Example of using llc below:
$ clang -emit-llvm -S -o hoge.ll hoge.c $ llvm-as -o hoge.bc hoge.ll $ llc -o hoge.s hoge.bc
If you output object using llc, you should specify
$ llc -filetype=obj -o hoge.o hoge.ll
The opt command is the modular LLVM optimizer and analyzer for LLVM IR. It takes LLVM source files as input, runs the specified optimizations or analyses on it, and then outputs the optimized file or the analysis results. The function of opt depends on whether the
-analyze option is given. There are various pass on LLVM, then you can freely apply these pass to LLVM IR using opt command.
Example of using opt below:
$ clang -emit-llvm -S -o hoge.ll hoge.c $ opt -03 -o hoge.bc hoge.ll $ lli hoge.bc
The llvm-link is a tool to combine several LLVM-IR into a file. llvm-link takes several LLVM bitcode files and links them together into a single LLVM bitcode file. It writes the output file to standard output, unless the -o option is used to specify a filename.
Example of using llvm-link below:
$ clang -emit-llvm -S -o hoge.ll hoge.c $ clang -emit-llvm -S -o lib.ll lib.c $ llvm-link -o a.ll hoge.ll lib.ll
The llvm-objdump is a tool to disassemble object file. Setting
arch option, you can specify architecture.
Example of using llvm-objdump below:
$ llvm-objdump -d hoge.o
Typical option of llvm-config are below:
|1||-help||output help information|
|2||-includedir||print path of header file of LLVM|
|3||-libdir||print path of library file of LLVM|
|4||-cxxflags||output flag of C++ to use header of LLVM|
|5||-ldflags||output flag of C++ to link library of LLVM|
|6||-libs||print all library to link component of LLVM specified|
I gonna read document of LLVM more and more.