Generating code with the LLVM backend
The backend's task is to create optimized machine code from an 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 the LLVM IR
Before trying to generate the LLVM IR, we need to understand what we want to generate. For the 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, calc_read()
and calc_write()
, are used. 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...