diff --git a/docs/dev/backend/approach.md b/docs/dev/backend/approach.md index 4e900ac..e69de29 100644 --- a/docs/dev/backend/approach.md +++ b/docs/dev/backend/approach.md @@ -1 +0,0 @@ -This has been the hardest to figure out. I found that LLVM as an abstraction is not good enough. So I am just writing my abstraction I guess. I am implementing my DSL to emit a JSON that closely mirrors LLVM. \ No newline at end of file diff --git a/src/backend/dsl.scm b/src/backend/brilisp.scm similarity index 97% rename from src/backend/dsl.scm rename to src/backend/brilisp.scm index a00fb3a..92b5405 100644 --- a/src/backend/dsl.scm +++ b/src/backend/brilisp.scm @@ -101,6 +101,8 @@ (else (error "unknown instruction: " instr)))) (bril '( + (bril-define ((print int) (n int))) + (bril-define ((add5 int) (n int)) (set (five int) (const 5)) (set (sum int) (add n five)) @@ -109,6 +111,7 @@ (bril-define ((main int)) (set (a int) (const 9)) (set (b int) (call add5 a)) + (set (tmp int) (call print b)) (ret b) ) )) diff --git a/src/backend/llvm.py b/src/backend/llvm.py index 4513b77..52e1347 100644 --- a/src/backend/llvm.py +++ b/src/backend/llvm.py @@ -45,11 +45,13 @@ def gen_function(self, fn): self.func_symtab = {} # Create the function skeleton from the prototype. func = self.gen_function_prototype(fn) - # Create the entry BB in the function and set the builder to it. - bb_entry = func.append_basic_block("entry") - self.builder = ir.IRBuilder(bb_entry) - retval = self.gen_instructions(fn.instrs) - self.builder.ret(retval) + + if fn.instrs: + # Create the entry BB in the function and set the builder to it. + bb_entry = func.append_basic_block("entry") + self.builder = ir.IRBuilder(bb_entry) + retval = self.gen_instructions(fn.instrs) + self.builder.ret(retval) return func def gen_function_prototype(self, fn): diff --git a/src/backend/run.sh b/src/backend/run.sh new file mode 100644 index 0000000..a25eb58 --- /dev/null +++ b/src/backend/run.sh @@ -0,0 +1,8 @@ +tmp_in=$(mktemp --suffix '.ll') +tmp_out=$(mktemp --suffix '.out') + +cp /dev/stdin $tmp_in +clang $tmp_in runtime.c -o $tmp_out -Wno-override-module +$tmp_out + +rm $tmp_in $tmp_out \ No newline at end of file diff --git a/src/backend/runtime.c b/src/backend/runtime.c new file mode 100644 index 0000000..e9dffd1 --- /dev/null +++ b/src/backend/runtime.c @@ -0,0 +1,6 @@ +#include + +int print(int x){ + printf("%d\n", x); + return x; +}