clang & llvm examples, e.g. AST Interpreter, Function Pointer Analysis, Value Range Analysis, Data-Flow Analysis, Andersen Pointer Analysis, LLVM Backend...
This archieve is mainly about some interesting examples of clang and llvm... Some of them are the homework of my postgraduate course, some of them are others, most of them are
toy code: )
An interpreter of clang abstract syntax tree, toy code, now it only support integer type. It supports basic modules of programming language, e.g. ifstmt, whilestmt, function call, malloc and array... For more info, see here...
Implement of use-def chains based on the LLVM IR and bitcode, now it supports direct function calls and function pointer. For the case of function pointer, calculate the functions that may call, if it is determined, replace it with direct function call and write into the bitcode file. Also take the situation that the function pointers are stored in memory into consideration.
Value range analysisis a type of
data flow analysisthat tracks the range (interval) of values that a numeric variable can take on at each point of a program's execution.
redundancy elimination, dead code elimination, instruction selection, etc. But can also be used to improve the
safety of programs, e.g. in the detection of buffer overruns. Techniques for value range analysis typically use
symbolic analysisextensively. When it comes to the detailed implement, for each basic block, define the following data-flow equation：
in[B] = ∪ out[A] | A∈ pred(B) && A！=S out[B] = in[B]∪gen[B]
Homework of foreign CS course ---
Advanced Compilers, for more detail, see here...
Implement of Andersen's pointer analysis algorithm, for more info, see here...
For a compiling system, I think the most complex part is the back end, you have to be proficient in instruction sets of the target processor and familiar with its architecture. Lots of things have to be done, e.g. instruction selection, instruction scheduling, register allocation, code emission and etc. Here i will provide you will an example, for more details, see here