Commit f89f9e4e authored by Sapphie's avatar Sapphie
Browse files

Implement AppC

parent 761e22ae
......@@ -55,18 +55,32 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
private def shrink(tree: Tree): Tree =
shrink(tree, State(census(tree)))
private def replaceArgs(args: Seq[Atom], s: State): Seq[Atom] = args map { a =>
s.aSubst.getOrElse(a, a)
}
private def shrink(tree: Tree, s: State): Tree = tree match {
case AppC(cntName, args) =>
val replacedArgs = replaceArgs(args, s)
if (s.appliedOnce(cntName)) {
// Inline
val cnt = s.cEnv(cntName)
val newState = s.withASubst(cnt.args, replacedArgs)
shrink(cnt.body, newState)
} else {
AppC(cntName, replacedArgs)
}
case AppF(funAtom, retC, args) =>
val replacedArgs = replaceArgs(args, s)
funAtom match {
case AtomN(n) if s.fEnv.contains(n) && s.appliedOnce(n) =>
// Inline
val fun = s.fEnv(n)
val newState = s.withASubst(fun.args, args)
val newState = s.withASubst(fun.args, replacedArgs)
.withCSubst(fun.retC, retC)
val newBody = shrink(fun.body, newState)
newBody
case _ => tree
case _ => AppF(funAtom, retC, replacedArgs)
}
case LetF(funs, body) =>
......@@ -88,7 +102,7 @@ abstract class CPSOptimizer[T <: CPSTreeModule { type Name = Symbol }]
if (s.dead(name) && !impure(prim)) {
shrink(body, s)
} else {
val replacedArgs = args map { a => s.aSubst.getOrElse(a, a)}
val replacedArgs = replaceArgs(args, s)
val primArgPair = (prim, replacedArgs)
if (!unstable(prim) && !impure(prim) && s.eInvEnv.contains(primArgPair)) {
val newS = s.withASubst(name, s.eInvEnv(primArgPair))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment