When compiling a haskell source file using the -S option in GHC the assembly code generated is not clear. There’s no clear distinction between which parts of the assembly code belong to which parts of the haskell code. Unlike GCC were each label is named according to the function it corresponds to.
Is there a certain convention in these names produced by GHC? How can I relate certain parts in the generated assembly code to their corresponding parts in the haskell code?
For top level declarations, it’s not too hard. Local definitions can be harder to recognize as their names get mangled and they are likely to get inlined.
Let’s see what happens when we compile this simple module.
You can see that our function
Example.addresulted in the generation ofExample_add_closureandExample_add_info. The_closurepart, as the name suggests, has to do with closures. The_infopart contains the actual instructions of the function. In this case, this is simply a jump to the built-in functionGHC.Base.plusInt.Note that assembly generated from Haskell code looks quite different from what you might get from other languages. The calling conventions are different, and things can get reordered a lot.
In most cases you don’t want to jump straight to assembly. It is usually much easier to understand core, a simplified version of Haskell. (Simpler to compile, not necessarily to read). To get at the core, compile with the
-ddump-simploption.For some good resources on how to read core, see this question.