Skip to content

Latest commit

 

History

History
105 lines (80 loc) · 1.23 KB

README.md

File metadata and controls

105 lines (80 loc) · 1.23 KB

LLVM-C EXAMPLES

works well on ubuntu 20.04.

repository that gives me great help igor84/summus

Installation

LLVM and Clang.

sudo apt install llvm-12
sudo apt install clang-12

Build

with GNU Make.

make

Run from LLVM IR Bitcode

Run fac.

./fac 8
# fac(8) = 40320

Run fib.

./fib 13
# fib(13) = 233

Run compiled main

./main fib 10
# 55

LLVM IR Text Format

make math.ll
cat math.ll

Fibonacci

this contains basic operations on parameters and local variables. also if statement and while loop for control flow.

  1. compare
  2. loop
  3. assignment
  4. 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;
}

Factorial

this contains recursive call and if statement and switch block. using a PHI instruction to catch all return value of each block.

  1. compare
  2. function call
  3. arithmetic
  4. switch
  5. 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);
  }
}