Detecting tail calls through CFA

This commit is contained in:
Andrey Breslav
2013-12-06 00:00:01 +04:00
parent 9f319e8b24
commit 97319808b6
16 changed files with 483 additions and 7 deletions
@@ -0,0 +1,35 @@
== test ==
tailRecursive fun test() : Int {
try {
// do nothing
} finally {
test()
}
}
---------------------
L0:
<START>
mark({ try { // do nothing } finally { test() } })
mark(try { // do nothing } finally { test() })
jmp?(L2 [onExceptionToFinallyBlock]) NEXT:[mark({ test() }), mark({ // do nothing })]
mark({ // do nothing })
read (Unit)
jmp(L3 [skipFinallyToErrorBlock]) NEXT:[mark({ test() })]
L2 [onExceptionToFinallyBlock]:
L4 [start finally]:
mark({ test() }) PREV:[jmp?(L2 [onExceptionToFinallyBlock])]
mark(test())
call(test, test)
L5 [finish finally]:
jmp(error) NEXT:[<ERROR>]
L3 [skipFinallyToErrorBlock]:
mark({ test() }) PREV:[jmp(L3 [skipFinallyToErrorBlock])]
mark(test())
call(test, test)
L1:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[jmp(error)]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,7 @@
tailRecursive fun test() : Int {
try {
// do nothing
} finally {
test()
}
}
@@ -0,0 +1,37 @@
== test ==
tailRecursive fun test() : Int {
try {
// do nothing
} finally {
return test()
}
}
---------------------
L0:
<START>
mark({ try { // do nothing } finally { return test() } })
mark(try { // do nothing } finally { return test() })
jmp?(L2 [onExceptionToFinallyBlock]) NEXT:[mark({ return test() }), mark({ // do nothing })]
mark({ // do nothing })
read (Unit)
jmp(L3 [skipFinallyToErrorBlock]) NEXT:[mark({ return test() })]
L2 [onExceptionToFinallyBlock]:
L4 [start finally]:
mark({ return test() }) PREV:[jmp?(L2 [onExceptionToFinallyBlock])]
mark(test())
call(test, test)
ret(*) L1 NEXT:[<END>]
L5 [finish finally]:
- jmp(error) NEXT:[<ERROR>] PREV:[]
L3 [skipFinallyToErrorBlock]:
mark({ return test() }) PREV:[jmp(L3 [skipFinallyToErrorBlock])]
mark(test())
call(test, test)
ret(*) L1
L1:
<END> NEXT:[<SINK>] PREV:[ret(*) L1, ret(*) L1]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,7 @@
tailRecursive fun test() : Int {
try {
// do nothing
} finally {
return test()
}
}
@@ -0,0 +1,46 @@
== sum ==
tailRecursive fun sum(x: Long, sum: Long): Long {
if (x == 0.toLong()) return sum
return sum(x - 1, sum + x)
}
---------------------
L0:
<START>
v(x: Long)
w(x)
v(sum: Long)
w(sum)
mark({ if (x == 0.toLong()) return sum return sum(x - 1, sum + x) })
mark(if (x == 0.toLong()) return sum)
mark(x == 0.toLong())
r(x)
mark(0.toLong())
mark(toLong())
r(0)
call(toLong, toLong)
call(x == 0.toLong(), equals)
jf(L2) NEXT:[read (Unit), r(sum)]
r(sum)
ret(*) L1 NEXT:[<END>]
- jmp(L3) NEXT:[mark(sum(x - 1, sum + x))] PREV:[]
L2:
read (Unit) PREV:[jf(L2)]
L3:
mark(sum(x - 1, sum + x))
mark(x - 1)
r(x)
r(1)
call(-, minus)
mark(sum + x)
r(sum)
r(x)
call(+, plus)
call(sum, sum)
ret(*) L1
L1:
<END> NEXT:[<SINK>] PREV:[ret(*) L1, ret(*) L1]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
+4
View File
@@ -0,0 +1,4 @@
tailRecursive fun sum(x: Long, sum: Long): Long {
if (x == 0.toLong()) return sum
return sum(x - 1, sum + x)
}
@@ -0,0 +1,33 @@
== foo ==
tailRecursive fun foo() {
try {
return foo()
}
catch (e: Throwable) {
}
}
---------------------
L0:
<START>
mark({ try { return foo() } catch (e: Throwable) { } })
mark(try { return foo() } catch (e: Throwable) { })
jmp?(L2 [onException]) NEXT:[v(e: Throwable), mark({ return foo() })]
mark({ return foo() })
mark(foo())
call(foo, foo)
ret(*) L1 NEXT:[<END>]
- jmp(L3 [afterCatches]) NEXT:[<END>] PREV:[]
L2 [onException]:
v(e: Throwable) PREV:[jmp?(L2 [onException])]
w(e)
mark({ })
read (Unit)
jmp(L3 [afterCatches])
L1:
L3 [afterCatches]:
<END> NEXT:[<SINK>] PREV:[ret(*) L1, jmp(L3 [afterCatches])]
error:
<ERROR> PREV:[]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
+7
View File
@@ -0,0 +1,7 @@
tailRecursive fun foo() {
try {
return foo()
}
catch (e: Throwable) {
}
}
@@ -0,0 +1,48 @@
== test ==
fun test() : Unit {
try {
test()
} catch (any : Exception) {
test()
} finally {
test()
}
}
---------------------
L0:
<START>
mark({ try { test() } catch (any : Exception) { test() } finally { test() } })
mark(try { test() } catch (any : Exception) { test() } finally { test() })
jmp?(L2 [onException]) NEXT:[v(any : Exception), jmp?(L3 [onExceptionToFinallyBlock])]
jmp?(L3 [onExceptionToFinallyBlock]) NEXT:[mark({ test() }), mark({ test() })]
mark({ test() })
mark(test())
call(test, test)
jmp(L4 [afterCatches]) NEXT:[jmp(L5 [skipFinallyToErrorBlock])]
L2 [onException]:
v(any : Exception) PREV:[jmp?(L2 [onException])]
w(any)
mark({ test() })
mark(test())
call(test, test)
jmp(L4 [afterCatches])
L4 [afterCatches]:
jmp(L5 [skipFinallyToErrorBlock]) NEXT:[mark({ test() })] PREV:[jmp(L4 [afterCatches]), jmp(L4 [afterCatches])]
L3 [onExceptionToFinallyBlock]:
L6 [start finally]:
mark({ test() }) PREV:[jmp?(L3 [onExceptionToFinallyBlock])]
mark(test())
call(test, test)
L7 [finish finally]:
jmp(error) NEXT:[<ERROR>]
L5 [skipFinallyToErrorBlock]:
mark({ test() }) PREV:[jmp(L5 [skipFinallyToErrorBlock])]
mark(test())
call(test, test)
L1:
<END> NEXT:[<SINK>]
error:
<ERROR> PREV:[jmp(error)]
sink:
<SINK> PREV:[<ERROR>, <END>]
=====================
@@ -0,0 +1,9 @@
fun test() : Unit {
try {
test()
} catch (any : Exception) {
test()
} finally {
test()
}
}
@@ -0,0 +1,10 @@
tailRecursive fun sum(x: Long, sum: Long): Long {
if (x == 0.toLong()) return sum
return sum(x - 1, sum + x)
}
fun box() : String {
val sum = sum(1000000, 0)
if (sum != 500000500000.toLong()) return "Fail $sum"
return "OK"
}