Commit 9871602d authored by Sapphie's avatar Sapphie
Browse files

Fix bug

parent 32c98d65
......@@ -32,11 +32,11 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
private def transformLetF(initialFuns: Seq[H.Fun], body: H.Tree): L.LetF = {
// for each function, closes it and returns all the variables that used be free in it
// as well as the associated worker function
// as well as the associated original function name
def transformFunAbs(funs: Seq[H.Fun]): Seq[(L.Fun, Seq[Symbol], Symbol)] = funs match {
case Nil => Nil
case f :: fs =>
val workerFun = Symbol.fresh("w")
val workerFun = Symbol.fresh("worker_function")
val envName = Symbol.fresh("env")
val newArgs = envName +: f.args
val funBody = apply(f.body)
......@@ -49,14 +49,14 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
substitute(funBody)(accSubst)
case freeVar :: vs =>
// Bind the fresh variable v to a block get
val v = Symbol.fresh("v")
val v = Symbol.fresh("block_variable")
L.LetP(v, CPS.BlockGet, Seq(L.AtomN(envName), L.AtomL(counter)),
argsBindings(vs, counter + 1, accSubst + (freeVar -> v)))
}
val newFunBody = argsBindings(fv, 1, Map())
val newFun = L.Fun(workerFun, f.retC, newArgs, newFunBody)
(newFun, fv, workerFun) +: transformFunAbs(fs)
(newFun, fv, f.name) +: transformFunAbs(fs)
}
def allocInitFun(funsAndVars: Seq[(L.Fun, Seq[Symbol], Symbol)], lastBody: L.Tree): L.Tree = {
......@@ -70,13 +70,13 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
funsAndVars match {
case Nil => lastBody
case (fun, vars, workerFun) :: rest =>
val blockAtom = L.AtomN(fun.name)
case (workerFun, vars, originalFunName) :: rest =>
val blockAtom = L.AtomN(originalFunName)
val varInits = initFun(vars, 1, blockAtom, rest)
val t1 = Symbol.fresh("t1")
val primArgs = Seq(blockAtom, L.AtomL(0), L.AtomN(workerFun))
val nextBody = L.LetP(t1, CPS.BlockSet, primArgs, varInits)
L.LetP(fun.name, CPS.BlockAlloc(202), Seq(L.AtomL(vars.length + 1)), nextBody)
val t1 = Symbol.fresh("blockset_unused")
val blockSetArgs = Seq(blockAtom, L.AtomL(0), L.AtomN(workerFun.name))
val nextBody = L.LetP(t1, CPS.BlockSet, blockSetArgs, varInits)
L.LetP(originalFunName, CPS.BlockAlloc(202), Seq(L.AtomL(vars.length + 1)), nextBody)
}
}
......@@ -85,7 +85,9 @@ object CPSValueRepresenter extends (H.Tree => L.Tree) {
val lastBody = apply(body)
val closureAllocInit = allocInitFun(funsAndVars, lastBody)
L.LetF(funsAndVars.unzip3._1, closureAllocInit)
val res = L.LetF(funsAndVars.unzip3._1, closureAllocInit)
println(res)
res
}
// Substitutes _free_ variables in `tree`
......
......@@ -13,6 +13,7 @@ object Main {
def main(args: Array[String]): Unit = {
val backEnd: Tree => TerminalPhaseResult = (
CL3ToCPSTranslator
andThen treePrinter("---------- After CPS translation")
andThen CPSValueRepresenter
andThen treePrinter("---------- After value representation")
andThen treeChecker
......
......@@ -2,13 +2,15 @@
;; Test the "defrec" statement
(@byte-write 67)
;;(@byte-write 67)
(defrec succ (fun (x) (@+ x 1)))
(@byte-write (succ 64))
(defrec fact (fun (x) (if (@= x 0) 1 (@* x (fact (@- x 1))))))
(@byte-write (@- (fact 5) 54))
(defrec fact (fun (x) (@+ x 2)))
(@byte-write (fact 65))
(defrec id (fun (x) (x)))
(id 1)
;;(defrec succ (fun (x) (@+ x 1)))
;;(@byte-write (succ 64))
;;
;;(defrec fact (fun (x) (if (@= x 0) 1 (@* x (fact (@- x 1))))))
;;(@byte-write (@- (fact 5) 54))
;;
;;(defrec fact (fun (x) (@+ x 2)))
;;(@byte-write (fact 65))
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