Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 941
Sam Avatar asked Jul 09 '14 11:07

Sam


People also ask

Is LLVM IR assembly?

LLVM IR is a low-level intermediate representation used by the LLVM compiler framework. You can think of LLVM IR as a platform-independent assembly language with an infinite number of function local registers.

How does LLVM IR work?

A developer uses the API to generate instructions in a format called an intermediate representation, or IR. LLVM can then compile the IR into a standalone binary or perform a JIT (just-in-time) compilation on the code to run in the context of another program, such as an interpreter or runtime for the language.

What is AST in LLVM?

2.2. The Abstract Syntax Tree (AST) The AST for a program captures its behavior in such a way that it is easy for later stages of the compiler (e.g. code generation) to interpret. We basically want one object for each construct in the language, and the AST should closely model the language.


1 Answers

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.

like image 90
Eli Bendersky Avatar answered Sep 20 '22 19:09

Eli Bendersky