Setting up the module and the driver
We collect all functions and global variables of a compilation unit in an LLVM module. To facilitate IR generation, we wrap all the functions from the previous sections in a code generator class. To get a working compiler, we also need to define the target architecture for which we want to generate code, and also add the passes that emit the code. We implement all this in the next chapters, starting with the code generator.
Wrapping everything in the code generator
The IR module is the brace around all elements we generate for a compilation unit. At the global level, we iterate through the declarations at the module level and create global variables and call the code generation for procedures. A global variable in tinylang
is mapped to an instance of the llvm::GobalValue
class. This mapping is saved in Globals
and made available to the code generation for procedures:
void CGModule::run(ModuleDeclaration *Mod) { Â Â for (auto *Decl...