Generating code with the LLVM backend
The task of the backend is to create optimized machine code from the LLVM IR of a module. The IR is the interface to the backend and can be created using a C++ interface or in textual form. Again, the IR is generated from the AST.
Textual representation of LLVM IR
Before trying to generate the LLVM IR, it should be clear what we want to generate. For our example expression language, the high-level plan is as follows:
- Ask the user for the value of each variable.
- Calculate the value of the expression.
- Print the result.
To ask the user to provide a value for a variable and to print the result, two library functions are used: calc_read()
and calc_write()
. For the with a: 3*a
expression, the generated IR is as follows:
- The library functions must be declared, like in C. The syntax also resembles C. The type before the function name is the return type. The type names surrounded by parenthesis are the argument types...