LLVM - How AST can be transformed to IR
Asked Answered
L

1

6

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.

Lodicule answered 9/7, 2014 at 11:6 Comment(0)
S
8

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.

Sober answered 9/7, 2014 at 12:27 Comment(8)
Thanks Eli. Would you mind to explain a bit more about what happened during the "walking of the AST that emits IR into a Module" ?Lodicule
I mean, how AST can be transformed to IR ? Is that a mapping file or something else ?Lodicule
@Lodicule explaining the process in detail will take a lot of time and space here, you'd better off checking the source by yourselfIncantatory
@MarcoA., thanks for your reply. Would it be possible to give a very brief explanation so that I can have a general sense before digging into the code ?Lodicule
@Sam: the link given by mishr is an excellent simple example of emitting LLVM IR from a simple AST. Simple because the input language is simple. The real Clang AST represent languages that are anything but simple (C++), so walking them is very complex as well.Sober
Thanks for the feedback. I will go through the link.Lodicule
Here's an update to @shrm 's link: llvm.org/docs/tutorial/LangImpl03.html. As a starting point, the tutorial first provides a simple AST implementation. It then explains how a unique codegen() function can be written for each type of AST node you have. Then, simply walk the AST (calling the codegen() functions along the way) to generate the LLVM.Doronicum
The problem I'm facing is to find where the codeGen process starts in clang. Can someone please give me some pointers? ThanksShelves

© 2022 - 2024 — McMap. All rights reserved.