This repository contains the starter code for Lab 3 of the course Language and Compiler Design.
In this lab, you will extend the previous lab sessions' work on the CALCB language. You need to add boolean constants, logical operators, and comparison operators to the language. You will also implement a compiler that translates CALCB expressions into LLVM code.
Building the project follows the same steps as in previous labs. Make sure you have dune installed.
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
-
Add boolean constants (
trueandfalse) to the syntax of theCALCBlanguage (lexer and parser). -
Add logical operators (
&&,||,not) to the syntax of theCALCBlanguage (lexer and parser). Pay special attention to operator precedence and associativity. -
Add comparison operators (
==,!=,<,>,<=,>=) to the syntax of theCALCBlanguage (lexer and parser). Pay special attention to operator precedence and associativity. -
Extend the interpreter to evaluate boolean constants, logical operators, and comparison operators. Notice that now the result of evaluating and expression is no longer an integer but can also be a boolean. Use a sum type to represent the result of evaluation.
type result =
| IntV of int
| BoolV of boolyou will need to also add a function that converts a result to a string for printing.
-
Modify the interpreter to account for boolean values and boolean operations.
-
Test the unparser and interpreter with expressions that use boolean constants, logical operators, and comparison operators.
-
Extend the compiler to generate LLVM code for boolean constants, logical operators, and comparison operators.
- Boolean values can be represented as
i1in LLVM (1 bit integer). - Logical operations can be implemented using LLVM instructions such as
and,or, andxor. Notice that we are not yet implementing short-circuit evaluation. - Comparison operations can be implemented using LLVM instructions such as
icmp.
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