Background: I am writing a toy Lisp interperter/compiler in Haskell for my own amusement/edification. I am trying to add the ability to compile to LLVM bytecode.
Context: I have been reading the documentation for LLVM.Core and a code example (here) attempting to understand the means of combination and means of abstraction (as described in Abelson and Sussman Structure and Interpretation
of Computer Programs.) used in the Haskell LLVM bindings. There are a lot of small pieces and I am not clear how they are intended to work together. It seems like there is a level of abstraction above the basic LLVM machine instructions that is obvious to someone with lots of experience with LLVM, but not documented for those, like me, who are just getting their feet wet.
Question: What are CodeGenModule and CodeGenFunction and how are they used to build up Functions and Modules?
The
ModuleandFunctiontypes are just thin wrappers around pointers to the corresponding C++ objects (that is,Module*andValue*):The
CodeGenModuleandCodeGenFunctiontypes are parts of the EDSL built on top of theLLVM.FFI.*modules. They useFunction,Moduleand the functions fromLLVM.FFI.*internally and allow you to write LLVM IR in Haskell concisely using do-notation (example taken from Lennart Augustsson’s blog):You can think of
CodeGenModuleas an AST representing a parsed LLVM assembly file (.ll). Given aCodeGenModule, you can e.g. write it to a.bcfile:I also recommend you to acquaint yourself with core classes of LLVM, since they also show through in the Haskell API.