Since I went to a workshop held several weeks ago, I got interested in LLVM. LLVM stands for Low Level Virtual Machine which is a compiler infrastructure (a good tool to use when you write your compiler). There are lots of project going on using LLVM, unladen swallow is a famous one of them. Now I'm reading LLVM tutorial and I found it's not only good for understanding LLVM, but it's very helpful to understand compiler itself, i.e. lexer, parser, and code generation, with lots of practical tips. It's not difficult at all if you have a basic knowledge of C++.
The following info are some tips to run the sample program (toy.cpp) on fedora.
1) Installation
yum install llvm
yum install clang
yum install llvm-doc
yum install clang-doc
yum install llvm-devel
2) Add -rdynamic option when you compile chapter 5 example program.
g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy -rdynamic
Accoding to the gcc man page,
Pass the flag -export-dynamic to the ELF linker, on targets that
support it. This instructs the linker to add all symbols, not only
used ones, to the dynamic symbol table. This option is needed for
some uses of "dlopen" or to allow obtaining backtraces from within
a program.
Without this, you'll see an error message when you use "putchard"
LLVM ERROR: Program used external function 'putchard' which could not be resolved!
3) To Make the chapter 5 example program work exactly like the document, comment out the following line.
Adding this will do some more optimization.
OurFPM.add(createCFGSimplificationPass());
4) If you are puzzled by the following part of chapter5's If/Then/Else section,
// Codegen of 'Then' can change the current block, update ThenBB for the PHI.
ThenBB = Builder.GetInsertBlock();
try executing the following code and see what it generates.
extern foo();
extern bar();
extern kou();
extern tam();
def baz(x) if x then if foo() then kou() else tam() else bar();
and be aware that if/then/else/merge always ends its execution at merge(ifcont) block.