Skip to content

Commit

Permalink
tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
butterunderflow committed Oct 23, 2024
1 parent b6474eb commit d08a203
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/main/scala/wasm/MiniWasm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,26 +193,29 @@ object Evaluator {
}
}

def evalDirectCall[Ans](rest: List[Instr],
stack: List[Value],
frame: Frame,
kont: Cont[Ans],
trail: List[Cont[Ans]],
funcIndex: Int,
isTail: Boolean): Ans = {
def evalCall[Ans](rest: List[Instr],
stack: List[Value],
frame: Frame,
kont: Cont[Ans],
trail: List[Cont[Ans]],
funcIndex: Int,
isTail: Boolean): Ans = {
frame.module.funcs(funcIndex) match {
case FuncDef(_, FuncBodyDef(ty, _, locals, body)) =>
val args = stack.take(ty.inps.size).reverse
val newStack = stack.drop(ty.inps.size)
val frameLocals = args ++ locals.map(zero(_))
val newFrame = Frame(frame.module, ArrayBuffer(frameLocals: _*))
val newK: Cont[Ans] = (retStack) => eval(rest, retStack.take(ty.out.size) ++ newStack, frame, kont, trail)
// We push newK on the trail since function creates a new block to escape
// (more or less like `return`)
if (isTail)
// when tail call, share the continuation for returning with the callee
eval(body, List(), newFrame, kont, List(kont))
else
eval(body, List(), newFrame, newK, List(newK))
else {
val restK: Cont[Ans] = (retStack) =>
eval(rest, retStack.take(ty.out.size) ++ newStack, frame, kont, trail)
// We make a new trail by `restK`, since function creates a new block to escape
// (more or less like `return`)
eval(body, List(), newFrame, restK, List(restK))
}
case Import("console", "log", _) =>
//println(s"[DEBUG] current stack: $stack")
val I32V(v) :: newStack = stack
Expand Down Expand Up @@ -350,8 +353,8 @@ object Evaluator {
val goto = if (cond < labels.length) labels(cond) else default
trail(goto)(newStack)
case Return => trail.last(stack)
case Call(f) => evalDirectCall(rest, stack, frame, kont, trail, f, false)
case ReturnCall(f) => evalDirectCall(rest, stack, frame, kont, trail, f, true)
case Call(f) => evalCall(rest, stack, frame, kont, trail, f, false)
case ReturnCall(f) => evalCall(rest, stack, frame, kont, trail, f, true)
case _ =>
println(inst)
throw new Exception(s"instruction $inst not implemented")
Expand Down

0 comments on commit d08a203

Please sign in to comment.