GHC: Core, STG - AST or text?

As part of the GHC, there is the step of translating the Haskell source code to Core and then (not necessarily as the next next step), translating Core to STG. However, one problem avoids me from my understanding - when do we have β€œnormal” code (ie. Like plain text) and when something really lives in memory, for example, abstract syntax trees (AST)?

And to clarify my question, I will divide it into parts:

1) when analyzing the phase of the Haskell source file, do we immediately create an AST for the Core language? If not, then it seems to me that we need to build an AST of a full Haskell (which seems strange), and then either convert them to ASTs Core, or, firstly, into a textual representation of them in Core and parse again to get Core AST .

2) almost the same question relates to the transition of Core to STG (but in this case, I think I can assume that we have Core ASTs - right?)

+5
source share
1 answer

The Haskell source is first parsed into the full Haskell AST, which is then checked typechecked.

From this moment, it is freed from Core, transferred to STG, and from there to Cmm either to the assembly or to the LLVM code. All these phases are based on AST, there is no textual representation of any of these stages until the assembly code / llvm, which is then written to a file and compiled using external tools.

He is not afraid to have a complete Haskell AST. Actually, it is a requirement to give type errors in terms of the code that the user wrote, instead of detecting type errors only at the Core level.

You can find the AST for Haskell in the HsSym modules and the AST of Core in CoreSyn .

+13
source

Source: https://habr.com/ru/post/1208543/


All Articles