5

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.

gprathour
  • 14,813
  • 5
  • 66
  • 90
Sam
  • 4,521
  • 13
  • 46
  • 81

1 Answers1

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.

gprathour
  • 14,813
  • 5
  • 66
  • 90
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 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" ? – Sam Jul 09 '14 at 12:49
  • I mean, how AST can be transformed to IR ? Is that a mapping file or something else ? – Sam Jul 09 '14 at 12:52
  • 1
    @Sam explaining the process in detail will take a lot of time and space here, you'd better off checking the source by yourself – Marco A. Jul 09 '14 at 13:24
  • @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 ? – Sam Jul 09 '14 at 13:28
  • 1
    @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. – Eli Bendersky Jul 09 '14 at 19:34
  • Thanks for the feedback. I will go through the link. – Sam Jul 09 '14 at 21:30
  • Here's an update to @shrm 's link: http://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. – Kevin Apr 20 '18 at 13:42
  • The problem I'm facing is to find where the `codeGen` process starts in clang. Can someone please give me some pointers? Thanks – ConsistentProgrammer Feb 28 '19 at 16:25