Skip to content

Commit

Permalink
Add "fibonacci with recursive cps" test
Browse files Browse the repository at this point in the history
  • Loading branch information
AzimMuradov committed Jan 4, 2024
1 parent 1f8116d commit 7e0cd68
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 1 deletion.
27 changes: 27 additions & 0 deletions test/Sample/Fibonacci/FibRecCps.anf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let id'2 x'1 = x'1;;
let ll'12 k'6 a'7 b'8 = (k'6 (a'7 + b'8));;
let ll'11 fib_cps'4 n'5 k'6 a'7 =
let anf'13 = (fib_cps'4 (n'5 - 2))
in
let anf'15 =
let anf'14 = (ll'12 k'6)
in (anf'14 a'7)
in (anf'13 anf'15);;
let fib_cps'4 n'5 k'6 =
if (n'5 < 3)
then (k'6 1)
else
let anf'16 = (fib_cps'4 (n'5 - 1))
in
let anf'19 =
let anf'18 =
let anf'17 = (ll'11 fib_cps'4)
in (anf'17 n'5)
in (anf'18 k'6)
in (anf'16 anf'19);;
let fib'9 n'3 =
let anf'20 = (fib_cps'4 n'3)
in (anf'20 id'2);;
let simp'10 =
let anf'21 = (fib'9 10)
in (print_int anf'21);;
119 changes: 119 additions & 0 deletions test/Sample/Fibonacci/FibRecCps.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
Just
( Program
[ StmtDecl
( DeclFun "id" False
( Fun
(
( "x"
, Nothing
) :| []
) Nothing
( ExprId "x" )
)
)
, StmtDecl
( DeclFun "fib" False
( Fun
(
( "n"
, Nothing
) :| []
) Nothing
( ExprLetIn
( DeclFun "fib_cps" True
( Fun
(
( "n"
, Nothing
) :|
[
( "k"
, Nothing
)
]
) Nothing
( ExprIte
( ExprBinOp ( CompOp LtOp )
( ExprId "n" )
( ExprPrimVal
( PrimValInt 3 )
)
)
( ExprApp
( ExprId "k" )
( ExprPrimVal
( PrimValInt 1 )
)
)
( ExprApp
( ExprApp
( ExprId "fib_cps" )
( ExprBinOp ( ArithOp MinusOp )
( ExprId "n" )
( ExprPrimVal
( PrimValInt 1 )
)
)
)
( ExprFun
( Fun
(
( "a"
, Nothing
) :| []
) Nothing
( ExprApp
( ExprApp
( ExprId "fib_cps" )
( ExprBinOp ( ArithOp MinusOp )
( ExprId "n" )
( ExprPrimVal
( PrimValInt 2 )
)
)
)
( ExprFun
( Fun
(
( "b"
, Nothing
) :| []
) Nothing
( ExprApp
( ExprId "k" )
( ExprBinOp ( ArithOp PlusOp )
( ExprId "a" )
( ExprId "b" )
)
)
)
)
)
)
)
)
)
)
)
( ExprApp
( ExprApp
( ExprId "fib_cps" )
( ExprId "n" )
)
( ExprId "id" )
)
)
)
)
, StmtExpr
( ExprApp
( ExprId "print_int" )
( ExprApp
( ExprId "fib" )
( ExprPrimVal
( PrimValInt 10 )
)
)
)
]
)
104 changes: 104 additions & 0 deletions test/Sample/Fibonacci/FibRecCps.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
; ModuleID = 'fibonacci'





declare external ccc i64 @not(i64)


declare external ccc i64 @print_bool(i64)


declare external ccc i64 @print_int(i64)


declare external ccc i64 @miniml_div(i64, i64)


declare external ccc i64 @miniml_fun_to_paf(i64, i64)


declare external ccc i64 @miniml_apply(i64, i64)


define external ccc i64 @id.2(i64 %x.1_0) {
ret i64 %x.1_0
}


define external ccc i64 @ll.12(i64 %k.6_0, i64 %a.7_0, i64 %b.8_0) {
%1 = add i64 %a.7_0, %b.8_0
%2 = call ccc i64 @miniml_apply(i64 %k.6_0, i64 %1)
ret i64 %2
}


define external ccc i64 @ll.11(i64 %fib_cps.4_0, i64 %n.5_0, i64 %k.6_0, i64 %a.7_0) {
%anf.13_0 = sub i64 %n.5_0, 2
%anf.13_1 = call ccc i64 @miniml_apply(i64 %fib_cps.4_0, i64 %anf.13_0)
%anf.14_0 = ptrtoint i64 (i64, i64, i64)* @ll.12 to i64
%anf.14_1 = call ccc i64 @miniml_fun_to_paf(i64 %anf.14_0, i64 3)
%anf.14_2 = call ccc i64 @miniml_apply(i64 %anf.14_1, i64 %k.6_0)
%anf.15_0 = call ccc i64 @miniml_apply(i64 %anf.14_2, i64 %a.7_0)
%1 = call ccc i64 @miniml_apply(i64 %anf.13_1, i64 %anf.15_0)
ret i64 %1
}


define external ccc i64 @fib_cps.4(i64 %n.5_0, i64 %k.6_0) {
; <label>:0:
%1 = alloca i64
%2 = icmp slt i64 %n.5_0, 3
%3 = zext i1 %2 to i64
%4 = trunc i64 %3 to i1
br i1 %4, label %if.then_0, label %if.else_0
if.then_0:
%5 = call ccc i64 @miniml_apply(i64 %k.6_0, i64 1)
store i64 %5, i64* %1
br label %if.end_0
if.else_0:
%anf.16_0 = ptrtoint i64 (i64, i64)* @fib_cps.4 to i64
%anf.16_1 = call ccc i64 @miniml_fun_to_paf(i64 %anf.16_0, i64 2)
%anf.16_2 = sub i64 %n.5_0, 1
%anf.16_3 = call ccc i64 @miniml_apply(i64 %anf.16_1, i64 %anf.16_2)
%anf.17_0 = ptrtoint i64 (i64, i64, i64, i64)* @ll.11 to i64
%anf.17_1 = call ccc i64 @miniml_fun_to_paf(i64 %anf.17_0, i64 4)
%anf.17_2 = ptrtoint i64 (i64, i64)* @fib_cps.4 to i64
%anf.17_3 = call ccc i64 @miniml_fun_to_paf(i64 %anf.17_2, i64 2)
%anf.17_4 = call ccc i64 @miniml_apply(i64 %anf.17_1, i64 %anf.17_3)
%anf.18_0 = call ccc i64 @miniml_apply(i64 %anf.17_4, i64 %n.5_0)
%anf.19_0 = call ccc i64 @miniml_apply(i64 %anf.18_0, i64 %k.6_0)
%6 = call ccc i64 @miniml_apply(i64 %anf.16_3, i64 %anf.19_0)
store i64 %6, i64* %1
br label %if.end_0
if.end_0:
%7 = load i64, i64* %1
ret i64 %7
}


define external ccc i64 @fib.9(i64 %n.3_0) {
%anf.20_0 = ptrtoint i64 (i64, i64)* @fib_cps.4 to i64
%anf.20_1 = call ccc i64 @miniml_fun_to_paf(i64 %anf.20_0, i64 2)
%anf.20_2 = call ccc i64 @miniml_apply(i64 %anf.20_1, i64 %n.3_0)
%1 = ptrtoint i64 (i64)* @id.2 to i64
%2 = call ccc i64 @miniml_fun_to_paf(i64 %1, i64 1)
%3 = call ccc i64 @miniml_apply(i64 %anf.20_2, i64 %2)
ret i64 %3
}


@simp.10 = global i64 0


define external ccc i64 @main() {
%anf.21_0 = ptrtoint i64 (i64)* @fib.9 to i64
%anf.21_1 = call ccc i64 @miniml_fun_to_paf(i64 %anf.21_0, i64 1)
%anf.21_2 = call ccc i64 @miniml_apply(i64 %anf.21_1, i64 10)
%1 = ptrtoint i64 (i64)* @print_int to i64
%2 = call ccc i64 @miniml_fun_to_paf(i64 %1, i64 1)
%3 = call ccc i64 @miniml_apply(i64 %2, i64 %anf.21_2)
store i64 %3, i64* @simp.10
ret i64 0
}
10 changes: 10 additions & 0 deletions test/Sample/Fibonacci/FibRecCps.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let id x = x

let fib n =
let rec fib_cps n k =
if n < 3
then k 1
else fib_cps (n - 1) (fun a -> fib_cps (n - 2) (fun b -> k (a + b)))
in fib_cps n id;;

print_int (fib 10)
1 change: 1 addition & 0 deletions test/Sample/Fibonacci/FibRecCps.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
55
5 changes: 4 additions & 1 deletion test/Sample/FibonacciTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ testLlvmRun (title, testFileProvider) =
type TestFileProvider = (String, String -> FilePath)

fibs :: [TestFileProvider]
fibs = [fibRec, fibRecLoop]
fibs = [fibRec, fibRecLoop, fibRecCps]

fibRec :: TestFileProvider
fibRec = ("recursive fibonacci", \ext -> testFile $ "FibRec." <> ext)

fibRecLoop :: TestFileProvider
fibRecLoop = ("fibonacci with recursive loop", \ext -> testFile $ "FibRecLoop." <> ext)

fibRecCps :: TestFileProvider
fibRecCps = ("fibonacci with recursive cps", \ext -> testFile $ "FibRecCps." <> ext)

testFile :: String -> String
testFile filename = "test/Sample/Fibonacci/" <> filename

0 comments on commit 7e0cd68

Please sign in to comment.