Skip to content

LLVM-C API examples. Create module, add functions, write to bitcode, convert to text format (.ll), load bitcode and execute function by name.

License

Notifications You must be signed in to change notification settings

pseudocc/llvm-c-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
  }
}

About

LLVM-C API examples. Create module, add functions, write to bitcode, convert to text format (.ll), load bitcode and execute function by name.

Resources

License

Stars

Watchers

Forks

Packages

No packages published