Skip to content

Commit

Permalink
removed redundant handler in chainr1 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Apr 29, 2024
1 parent 7f33e26 commit 741132c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,15 @@ private [deepembedding] final class Chainr[A, B](p: StrictParsley[A], op: Strict
override def codeGen[M[_, +_]: ContOps, R](producesResults: Boolean)(implicit instrs: InstrBuffer, state: CodeGenState): M[R, Unit]= {
if (producesResults) {
val body = state.freshLabel()
// handler1 is where the check offset is kept
val handler1 = state.getLabel(instructions.Refail)
val handler2 = state.freshLabel()
val handler = state.freshLabel()
instrs += new instructions.Push(identity[Any] _)
instrs += new instructions.PushHandler(handler1)
instrs += new instructions.Label(body)
suspend(p.codeGen[M, R](producesResults=true)) >> {
instrs += new instructions.PushHandler(handler2)
instrs += new instructions.PushHandler(handler)
suspend(op.codeGen[M, R](producesResults=true)) |> {
instrs += new instructions.ChainrJump(body)
instrs += new instructions.Label(handler2)
instrs += new instructions.Label(handler)
instrs += instructions.ChainrOpHandler(wrap)
if (!producesResults) instrs += instructions.Pop
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ private [internal] final class RestoreAndPushHandler(var label: Int) extends Ins
// $COVERAGE-ON$
}

private [internal] object Refail extends Instr {
/*private [internal] object Refail extends Instr {
override def apply(ctx: Context): Unit = {
ensureHandlerInstruction(ctx)
ctx.handlers = ctx.handlers.tail
Expand All @@ -289,4 +289,4 @@ private [internal] object Refail extends Instr {
// $COVERAGE-OFF$
override def toString: String = "Refail"
// $COVERAGE-ON$
}
}*/
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,6 @@ private [internal] final class Chainl(var label: Int) extends InstrWithLabel {
// $COVERAGE-ON$
}

private [instructions] object DualHandler {
def popSecondHandlerAndJump(ctx: Context, label: Int): Unit = {
ctx.handlers = ctx.handlers.tail
ctx.updateCheckOffset()
ctx.pc = label
}
}

private [internal] final class ChainrJump(var label: Int) extends InstrWithLabel {
override def apply(ctx: Context): Unit = {
ensureRegularInstruction(ctx)
Expand All @@ -127,7 +119,8 @@ private [internal] final class ChainrJump(var label: Int) extends InstrWithLabel
val acc = ctx.stack.peek[Any => Any]
// We perform the acc after the tos function; the tos function is "closer" to the final p
ctx.stack.exchange((y: Any) => acc(f(x, y)))
DualHandler.popSecondHandlerAndJump(ctx, label)
ctx.handlers = ctx.handlers.tail
ctx.pc = label
}

// $COVERAGE-OFF$
Expand All @@ -138,9 +131,7 @@ private [internal] final class ChainrJump(var label: Int) extends InstrWithLabel
private [internal] final class ChainrOpHandler(wrap: Any => Any) extends Instr {
override def apply(ctx: Context): Unit = {
ensureHandlerInstruction(ctx)
val check = ctx.handlers.check
ctx.handlers = ctx.handlers.tail
ctx.catchNoConsumed(check) {
ctx.catchNoConsumed(ctx.handlers.check) {
ctx.handlers = ctx.handlers.tail
ctx.addErrorToHintsAndPop()
val y = ctx.stack.upop()
Expand All @@ -163,7 +154,10 @@ private [internal] final class SepEndBy1Jump(var label: Int) extends InstrWithLa
ctx.stack.pop_() // the bool
ctx.stack.peek[mutable.Builder[Any, Any]] += x
ctx.stack.upush(true)
DualHandler.popSecondHandlerAndJump(ctx, label)
// pop second handler and jump
ctx.handlers = ctx.handlers.tail
ctx.updateCheckOffset()
ctx.pc = label
}

// $COVERAGE-OFF$
Expand Down

0 comments on commit 741132c

Please sign in to comment.