Generating IR code for expressions
In this recipe, you will see how IR code gets generated for an expression using the compiler frontend.
How to do it…
To implement LLVM IR code generation for our TOY language, proceed with the following code flow:
- Open the
toy.cpp
file as follows:$ vi toy.cpp
- The function to generate code for numeric values can be defined as follows:
Value *NumericAST::Codegen() { return ConstantInt::get(Type::getInt32Ty(getGlobalContext()), numeric_val); }
In LLVM IR, integer constants are represented by the
ConstantInt
class whose numeric value is held by theAPInt
class. - The function for generating code for variable expressions can be defined as follows:
Value *VariableAST::Codegen() { Value *V = Named_Values[Var_Name]; return V ? ...
Get LLVM Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.