Skip to content

Commit

Permalink
Merge pull request #18 from GlowingScrewdriver/float
Browse files Browse the repository at this point in the history
Floating-point support
  • Loading branch information
chsasank authored Jun 5, 2024
2 parents 486881e + 05ab16f commit 8609012
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/backend/brilisp.scm
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
(define (value? instr)
(and (eq? (first instr) 'set)
(memq (first (third instr))
'(add mul sub div eq lt gt le ge not and or alloc load ptradd id))))
'(add mul sub div eq ne lt gt le ge not and or alloc load ptradd id fadd fsub fmul fdiv feq fne flt fgt fle fge))))

(define (gen-value-instr instr)
(let ((to (second instr))
Expand Down
31 changes: 30 additions & 1 deletion src/backend/llvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def gen_type(self, type):
return ir.VoidType()
elif type == "bool":
return ir.IntType(1)
elif type == "float":
return ir.FloatType()
else:
raise CodegenError(f"Unknown type {type}")

Expand All @@ -81,6 +83,10 @@ def gen_instructions(self, instrs):
"not": "not_",
"and": "and_",
"or": "or_",
"fadd": "fadd",
"fsub": "fsub",
"fmul": "fmul",
"fdiv": "fdiv",
}

cmp_ops = {
Expand All @@ -89,7 +95,16 @@ def gen_instructions(self, instrs):
"gt": ">",
"le": "<=",
"ge": ">=",
"neq": "!=",
"ne": "!=",
}

fcmp_ops = {
"feq": "==",
"flt": "<",
"fgt": ">",
"fle": "<=",
"fge": ">=",
"fne": "!=",
}

def gen_label(instr):
Expand Down Expand Up @@ -149,6 +164,18 @@ def gen_value(instr):
llvm_instr(*[self.gen_var(arg) for arg in instr.args], name=instr.dest),
)

def gen_fcomp(instr):
self.declare_var(self.gen_type(instr.type), instr.dest)
self.gen_symbol_store(
instr.dest,
self.builder.fcmp_ordered(
cmpop=fcmp_ops[instr.op],
lhs=self.gen_var(instr.args[0]),
rhs=self.gen_var(instr.args[1]),
name=instr.dest,
),
)

def gen_comp(instr):
self.declare_var(self.gen_type(instr.type), instr.dest)
self.gen_symbol_store(
Expand Down Expand Up @@ -226,6 +253,8 @@ def gen_id(instr):
gen_value(instr)
elif instr.op in cmp_ops:
gen_comp(instr)
elif instr.op in fcmp_ops:
gen_fcomp(instr)
else:
raise CodegenError(f"Unknown op in the instruction: {dict(instr)}")
except Exception as e:
Expand Down
3 changes: 3 additions & 0 deletions src/backend/tests/brilisp/float.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-0.450000
0.300000
0.300000
20 changes: 20 additions & 0 deletions src/backend/tests/brilisp/float.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(brilisp
(bril-define ((fprint float) (n float)))

(bril-define ((main void))
(set (v0 float) (const 9.0))
(set (v1 float) (const -20.0))
(set (res float) (fdiv v0 v1))
(set (tmp float) (call fprint res))

(set (v2 float) (const .1))
(set (v3 float) (const 0.1))
(set (add1 float) (fadd v2 v3))
(set (dres float) (fadd add1 v2))
(set (tmp float) (call fprint dres))

(set (v4 float) (const .1))
(set (add2 float) (fadd v4 v4))
(set (fres float) (fadd add2 v4))
(set (tmp float) (call fprint fres))
(ret)))
6 changes: 6 additions & 0 deletions src/backend/tests/brilisp/float_compare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0
1
0
1
0
1
25 changes: 25 additions & 0 deletions src/backend/tests/brilisp/float_compare.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(brilisp
(bril-define ((print bool) (b bool)))

(bril-define ((main void))
(set (v1 float) (const 50.0))
(set (v2 float) (const 50.1))

(set (res bool) (feq v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (fne v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (fge v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (fle v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (fgt v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (flt v1 v2))
(set (tmp bool) (call print res))
(ret)))
3 changes: 3 additions & 0 deletions src/backend/tests/brilisp/float_divide_by_zero.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-inf
inf
nan
14 changes: 14 additions & 0 deletions src/backend/tests/brilisp/float_divide_by_zero.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(brilisp
(bril-define ((fprint float) (n float)))

(bril-define ((main void))
(set (v0 float) (const -1.0))
(set (v1 float) (const 1.0))
(set (zero float) (const 0.0))
(set (res float) (fdiv v0 zero))
(set (tmp float) (call fprint res))
(set (res float) (fdiv v1 zero))
(set (tmp float) (call fprint res))
(set (res float) (fdiv zero zero))
(set (tmp float) (call fprint res))
(ret)))
1 change: 1 addition & 0 deletions src/backend/tests/brilisp/float_nan_detect.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
10 changes: 10 additions & 0 deletions src/backend/tests/brilisp/float_nan_detect.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(brilisp
(bril-define ((print bool) (b bool)))

(bril-define ((main void))
(set (zero float) (const 0.0))
(set (nan float) (fdiv zero zero))
(set (res bool) (feq nan nan))
(set (res bool) (not res))
(set (tmp bool) (call print res))
(ret)))
1 change: 1 addition & 0 deletions src/backend/tests/brilisp/float_zero_cmp.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
17 changes: 17 additions & 0 deletions src/backend/tests/brilisp/float_zero_cmp.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(brilisp
(bril-define ((print int) (n int)))

(bril-define ((main void))
(set (v0 float) (const 0.0))
(set (v1 float) (const -0.0))
(set (res bool) (feq v0 v1))
(br res l_true l_false)

(label l_true)
(set (out int) (const 1))
(jmp out)
(label l_false)
(set (out int) (const 0))
(label out)
(set (tmp int) (call print out))
(ret)))
6 changes: 6 additions & 0 deletions src/backend/tests/brilisp/int_compare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0
1
0
1
0
1
25 changes: 25 additions & 0 deletions src/backend/tests/brilisp/int_compare.sexp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(brilisp
(bril-define ((print bool) (b bool)))

(bril-define ((main void))
(set (v1 int) (const 50))
(set (v2 int) (const 51))

(set (res bool) (eq v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (ne v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (ge v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (le v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (gt v1 v2))
(set (tmp bool) (call print res))

(set (res bool) (lt v1 v2))
(set (tmp bool) (call print res))
(ret)))
File renamed without changes.
5 changes: 5 additions & 0 deletions src/backend/runtime.c → src/backend/tests/brilisp/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ int print(int x){
printf("%d\n", x);
return x;
}

float fprint(float x) {
printf("%f\n", x);
return x;
}
2 changes: 1 addition & 1 deletion src/backend/tests/brilisp/turnt.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
command = "guile ../../brilisp.scm < {filename} | python ../../llvm.py | bash ../../run.sh "
command = "guile ../../brilisp.scm < {filename} | python ../../llvm.py | bash run.sh "

0 comments on commit 8609012

Please sign in to comment.