llvmllvm-clangllvm-gccllvm-ir

LLVM - How AST can be transformed to IR


I know that an AST generated by the parser is used to generate IR in the frontend.

I am wondering how AST to be parsed and then transformed to IR (prob assembly or bitcode),

AST is a tree, what are the steps involved in the transformation from AST to IR.


Solution

  • Emitting LLVM IR from Clang ASTs happens in Clang's code gen stage. The code for this stage lives in lib/CodeGen/ (relative to Clang's source root). There's no need to parse the AST since Clang has the AST in an in-memory data structure. Code generation is essentially a recursive walk of the AST that emits IR into a Module. If there's any specific step of it that interests you, the best way to examine it would be to look in the code.