works well on ubuntu 20.04.
repository that gives me great help igor84/summus
LLVM and Clang.
sudo apt install llvm-12
sudo apt install clang-12
with GNU Make.
make
Run fac.
./fac 8
# fac(8) = 40320
Run fib.
./fib 13
# fib(13) = 233
./main fib 10
# 55
make math.ll
cat math.ll
this contains basic operations on parameters and local variables. also if statement and while loop for control flow.
- compare
- loop
- assignment
- arithmetic
int fib(int n) {
int a, b, t;
if (n < 1)
return -1;
if (n < 3)
return 1;
a = 1;
b = 1;
while (n-- > 2) {
t = a + b;
a = b;
b = t;
}
return b;
}
this contains recursive call and if statement and switch block. using a PHI instruction to catch all return value of each block.
- compare
- function call
- arithmetic
- switch
- PHI instruction
int fac(int n) {
if (n < 0)
return -fac(-n);
switch (n) {
case 0:
case 1:
return 1;
default:
return n * fac(n - 1);
}
}