aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2023-07-04 23:37:51 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2023-07-04 23:37:51 +0300
commite82c73d2b57e58894d8c93fc720559df6392d58b (patch)
tree3a637c10d0334e21ba3a75fbf5854d0f43efe07e /example
parent703e37ac56883f9a54b35960fde438c7cfba4592 (diff)
downloadbcgen-e82c73d2b57e58894d8c93fc720559df6392d58b.tar.gz
bcgen-e82c73d2b57e58894d8c93fc720559df6392d58b.zip
add loop example
Diffstat (limited to 'example')
-rw-r--r--example/Makefile17
-rwxr-xr-xexample/execbin0 -> 93744 bytes
-rw-r--r--example/main.c25
-rw-r--r--example/rules.py5
4 files changed, 47 insertions, 0 deletions
diff --git a/example/Makefile b/example/Makefile
new file mode 100644
index 0000000..66c96a0
--- /dev/null
+++ b/example/Makefile
@@ -0,0 +1,17 @@
+CFLAGS = -Wall -Wextra -g -O2
+
+all: exec
+
+exec: bcode.o main.o
+ $(CC) $(CFLAGS) $^ -o $@
+
+bcode.o: ../bcode.c
+ $(CC) $(CFLAGS) -c $^ -o $@
+
+../bcode.c: rules.py
+ cd .. && ./bcgen example/rules.py
+ touch ../bcode.c # just to get make to wake up
+
+.PHONY: clean
+clean:
+ rm -rf *.o exec
diff --git a/example/exec b/example/exec
new file mode 100755
index 0000000..2189325
--- /dev/null
+++ b/example/exec
Binary files differ
diff --git a/example/main.c b/example/main.c
new file mode 100644
index 0000000..7ca152b
--- /dev/null
+++ b/example/main.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include "../bcode.h"
+
+int main()
+{
+ struct compile_state cs;
+ init(&cs);
+
+ select_movi(&cs, 2, 0); // i
+ select_movi(&cs, 1, 1000000000); // limit
+ select_movi(&cs, 0, 0); // total
+
+ breg_t l = label(&cs); // top
+ select_addr(&cs, 0, 0, 2); // add iter to total
+ select_addi(&cs, 2, 2, 1);
+ breloc_t r = select_bltr(&cs, 2, 1, 0); // 0 is placeholder value,
+ // should maybe add in an
+ // UNDEFINED macro or something
+ end(&cs);
+
+ patch(&cs, r, l);
+
+ printf("%llu\n", (unsigned long long)run(&cs));
+ destroy(&cs);
+}
diff --git a/example/rules.py b/example/rules.py
new file mode 100644
index 0000000..9c8d62e
--- /dev/null
+++ b/example/rules.py
@@ -0,0 +1,5 @@
+g = BCGen(r = 7, f = 0)
+g.rule('addr', '_RRR__', 'R0 = R1 + R2;')
+g.rule('addi', '_RR__I', 'R0 = R1 + IMM;')
+g.rule('movi', '_R___I', 'R0 = IMM;')
+g.rule('bltr', 'rRR__I', 'if (R0 < R1) JUMP(IMM);')