Skip to content

Commit e977326

Browse files
committed
add a readme
1 parent a133113 commit e977326

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# LLVM-C EXAMPLE
2+
3+
works well on ubuntu 20.04.
4+
5+
## Installation
6+
7+
LLVM and Clang.
8+
9+
```bash
10+
sudo apt install llvm-12
11+
sudo apt install clang-12
12+
```
13+
14+
## Build
15+
16+
with GNU Make.
17+
18+
```bash
19+
make
20+
```
21+
22+
## Run from LLVM IR Bitcode
23+
24+
Run fac.
25+
26+
```bash
27+
./fac 8
28+
# fac(8) = 40320
29+
```
30+
31+
Run fib.
32+
33+
```bash
34+
./fib 13
35+
# fib(13) = 233
36+
```
37+
38+
## LLVM IR Text Format
39+
40+
```bash
41+
make math.ll
42+
cat math.ll
43+
```
44+
45+
## Fibonacci
46+
47+
this contains basic operations on parameters and local variables.
48+
also if statement and while loop for control flow.
49+
50+
0. compare
51+
0. loop
52+
0. assignment
53+
0. arithmetic
54+
55+
```c
56+
int fib(int n) {
57+
int a, b, t;
58+
if (n < 1)
59+
return -1;
60+
if (n < 3)
61+
return 1;
62+
a = 1;
63+
b = 1;
64+
while (n-- > 2) {
65+
t = a + b;
66+
a = b;
67+
b = t;
68+
}
69+
return b;
70+
}
71+
```
72+
73+
## Factorial
74+
75+
this contains recursive call and if statement and switch block.
76+
using a PHI instruction to catch all return value of each block.
77+
78+
0. compare
79+
0. function call
80+
0. arithmetic
81+
0. switch
82+
0. PHI instruction
83+
84+
```c
85+
int fac(int n) {
86+
if (n < 0)
87+
return -fac(-n);
88+
switch (n) {
89+
case 0:
90+
case 1:
91+
return 1;
92+
default:
93+
return n * fac(n - 1);
94+
}
95+
}
96+
```

0 commit comments

Comments
 (0)