This repository contains the starter code for Lab 2 of the course Language and Compiler Design.
In this lab, you will work with a compiler for a simple expression language named CALC.
Run the following command inside the project root:
dune buildThis compiles the interpreter and related modules.
After building, you can run the interpreter with:
dune exec calccThis will start the program that evaluates expressions written in the defined expression language.
The program asks for an expression to compile and outputs some LLVM code. For instance, if the expression is 1+2+3, the output will be
@.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1
define i32 @main() #0 {
%1 = add nsw i32 1, 2
%2 = add nsw i32 %1, 3
%3 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %2)
ret i32 0
}
declare i32 @printf(ptr noundef, ...) #1This output should be placed in a file with the extension ll and compiled using clang
clang -o a a.llThe result is the executable a which can be run from the console
./ato obtain the result 6
-
Inspect the code of the compiler to understand how the instructions are generated.
-
Research LLVM instructions by reading the documentation and by compiling sample C programs to LLVM. The command to do so is
clang -S -emit-llvm -c a.c -o a.llthe result will be a file called a.ll
- Extend the compiler to implement new instructions.