commit 488d716ddb144c1db260be1d1a5d82751b207499
parent 9326c8029b527effbfd05314cb2976b9213c6176
Author: Brian Swetland <swetland@frotz.net>
Date: Sun, 19 Dec 2021 18:56:15 -0800
llvm toys
- some little source snippets to see what a Real Compiler does
- Makefile to build into llvm IR, etc
Diffstat:
8 files changed, 101 insertions(+), 0 deletions(-)
diff --git a/llvm-toys/Makefile b/llvm-toys/Makefile
@@ -0,0 +1,22 @@
+
+SRCS := hello.c ssa2.c fib.c gcd.c call.c bigfunc.c struct.c
+
+TARGETS := $(patsubst %.c,%.m2r.ll,$(SRCS)) $(patsubst %.c,%.ll,$(SRCS))
+
+all: all-targets
+
+# -O + disable passes avoids tagging with "optnone"
+# which interferes with further passes requested by the opt tool
+%.bc: %.c
+ clang -c -O -Xclang -disable-llvm-passes -emit-llvm $< -o $@
+
+%.ll: %.bc
+ llvm-dis-10 $<
+
+%.m2r.bc: %.bc
+ opt-10 -mem2reg $< -o $@
+
+all-targets: $(TARGETS)
+
+clean:
+ rm -f *.bc *.ll
diff --git a/llvm-toys/bigfunc.c b/llvm-toys/bigfunc.c
@@ -0,0 +1,4 @@
+
+int bigfunc(int a, int b, int c, int d, int e, int f, int g, int h) {
+ return a + b + c + d + e + f + g + h;
+}
diff --git a/llvm-toys/call.c b/llvm-toys/call.c
@@ -0,0 +1,12 @@
+
+int func(int a, int b);
+
+int caller(int n, int a, int b) {
+ int r;
+ while (n > 0) {
+ int x = func(a * n, b + n);
+ r = r + x;
+ n--;
+ }
+ return r;
+}
diff --git a/llvm-toys/fib.c b/llvm-toys/fib.c
@@ -0,0 +1,14 @@
+int fib(int n) {
+ int a = 0;
+ int b = 1;
+ int z = 0;
+ while (n != z) {
+ int t = a + b;
+ a = b;
+ b = t;
+ n = n - 1;
+ z = 0;
+ }
+ return a;
+}
+
diff --git a/llvm-toys/gcd.c b/llvm-toys/gcd.c
@@ -0,0 +1,11 @@
+int gcd(int x1, int x2) {
+ while (x2 != 0) {
+ int q = x1 / x2;
+ int t = q * x2;
+ int r = x1 - t;
+ x1 = x2;
+ x2 = r;
+ }
+ return x1;
+}
+
diff --git a/llvm-toys/hello.c b/llvm-toys/hello.c
@@ -0,0 +1,13 @@
+int func(int x, int y) {
+ while (x > 0) {
+ y = y + x;
+ if (x == 17) {
+ y *= 2;
+ } else {
+ y -= 1;
+ }
+ x = x - 1;
+ }
+ return y;
+}
+
diff --git a/llvm-toys/ssa2.c b/llvm-toys/ssa2.c
@@ -0,0 +1,13 @@
+
+int f();
+
+int ssa2() {
+ int y, z;
+ y =f();
+ if (y < 0) {
+ z = y + 1;
+ } else {
+ z = y + 2;
+ }
+ return z;
+}
diff --git a/llvm-toys/struct.c b/llvm-toys/struct.c
@@ -0,0 +1,12 @@
+
+struct s {
+ int a;
+ int b;
+ int c;
+};
+
+void func(struct s* x, int y) {
+ x->a = y;
+ x->b = y * 2;
+ x->c = y - 45;
+}