Skip to content

Commit

Permalink
figured out basic runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
chsasank committed Apr 13, 2024
1 parent bc6a2bb commit accfa2a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
1 change: 0 additions & 1 deletion docs/dev/backend/approach.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions src/backend/dsl.scm → src/backend/brilisp.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
)
))
12 changes: 7 additions & 5 deletions src/backend/llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions src/backend/run.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions src/backend/runtime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int print(int x){
printf("%d\n", x);
return x;
}

0 comments on commit accfa2a

Please sign in to comment.