Skip to content

Commit 77398dc

Browse files
authored
Name join points introduced when compiling clauses (#848)
Quick drive-by from #841 that deserves to be considered separately. Instead of naming each join point created for the RHS of a clause as `b_k_123`, we can give a slightly more specific name based on the origin of the clause. Feel free to bikeshed the names chosen or close this PR altogether if it's useless. FWIW, it helps me ever-so-slightly in the `b_k${index of the case in the source}_123` case when debugging.
1 parent 2b93717 commit 77398dc

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

effekt/shared/src/main/scala/effekt/core/Transformer.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
386386
Context.bind(If(c, transform(thn), transform(els)))
387387

388388
case source.If(guards, thn, els) =>
389-
val thnClause = preprocess(Nil, guards, transform(thn))
390-
val elsClause = preprocess(Nil, Nil, transform(els))
389+
val thnClause = preprocess("thn", Nil, guards, transform(thn))
390+
val elsClause = preprocess("els", Nil, Nil, transform(els))
391391
Context.bind(PatternMatchingCompiler.compile(List(thnClause, elsClause)))
392392

393393
// case i @ source.If(guards, thn, els) =>
@@ -400,24 +400,24 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
400400
// def loop$13() = if ([[cond]]) { [[ body ]]; loop$13() } else { return () }
401401
// loop$13()
402402
case source.While(guards, body, default) =>
403-
val loopName = TmpBlock("whileLoop")
403+
val loopName = TmpBlock("while")
404404
val loopType = core.BlockType.Function(Nil, Nil, Nil, Nil, core.Type.TUnit)
405405

406406
// TODO double check: probably we are forgetting the capture of the guards!
407407
val loopCapt = transform(Context.inferredCapture(body))
408408
val loopCall = Stmt.App(core.BlockVar(loopName, loopType, loopCapt), Nil, Nil, Nil)
409409

410410
val transformedBody = transform(body)
411-
val thenBranch = Stmt.Val(TmpValue("whileThen"), transformedBody.tpe, transformedBody, loopCall)
411+
val thenBranch = Stmt.Val(TmpValue("while_thn"), transformedBody.tpe, transformedBody, loopCall)
412412
val elseBranch = default.map(transform).getOrElse(Return(Literal((), core.Type.TUnit)))
413413

414414
val loopBody = guards match {
415415
case List(MatchGuard.BooleanGuard(cond)) =>
416416
insertBindings { core.If(transformAsPure(cond), thenBranch, elseBranch) }
417417
case _ =>
418418
insertBindings {
419-
val thenClause = preprocess(Nil, guards, thenBranch)
420-
val elseClause = preprocess(Nil, Nil, elseBranch)
419+
val thenClause = preprocess("guard_thn", Nil, guards, thenBranch)
420+
val elseClause = preprocess("guard_els", Nil, Nil, elseBranch)
421421
PatternMatchingCompiler.compile(List(thenClause, elseClause))
422422
}
423423
}
@@ -434,8 +434,8 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
434434
case source.Match(sc, cs, default) =>
435435
// (1) Bind scrutinee and all clauses so we do not have to deal with sharing on demand.
436436
val scrutinee: ValueVar = Context.bind(transformAsPure(sc))
437-
val clauses = cs.map(c => preprocess(scrutinee, c))
438-
val defaultClause = default.map(stmt => preprocess(Nil, Nil, transform(stmt))).toList
437+
val clauses = cs.zipWithIndex.map((c, i) => preprocess(s"k${i}", scrutinee, c))
438+
val defaultClause = default.map(stmt => preprocess("k_els", Nil, Nil, transform(stmt))).toList
439439
val compiledMatch = PatternMatchingCompiler.compile(clauses ++ defaultClause)
440440
Context.bind(compiledMatch)
441441

@@ -527,10 +527,10 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
527527
*/
528528
def collectClauses(term: source.Term)(using Context): Option[List[Clause]] = term match {
529529
case source.If(guards, thn, els) =>
530-
val thenClause = preprocess(Nil, guards, transform(thn))
530+
val thenClause = preprocess("thn", Nil, guards, transform(thn))
531531
val elseClauses = collectClauses(els) match {
532532
case Some(clauses) => clauses
533-
case None => List(preprocess(Nil, Nil, transform(els)))
533+
case None => List(preprocess("els", Nil, Nil, transform(els)))
534534
}
535535
Some(thenClause :: elseClauses)
536536
case _ => None
@@ -653,10 +653,10 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
653653
})
654654
}
655655

656-
def preprocess(sc: ValueVar, clause: source.MatchClause)(using Context): Clause =
657-
preprocess(List((sc, clause.pattern)), clause.guards, transform(clause.body))
656+
def preprocess(label: String, sc: ValueVar, clause: source.MatchClause)(using Context): Clause =
657+
preprocess(label, List((sc, clause.pattern)), clause.guards, transform(clause.body))
658658

659-
def preprocess(patterns: List[(ValueVar, source.MatchPattern)], guards: List[source.MatchGuard], body: core.Stmt)(using Context): Clause = {
659+
def preprocess(label: String, patterns: List[(ValueVar, source.MatchPattern)], guards: List[source.MatchGuard], body: core.Stmt)(using Context): Clause = {
660660
import PatternMatchingCompiler.*
661661

662662
def boundInPattern(p: source.MatchPattern): List[core.ValueParam] = p match {
@@ -686,7 +686,7 @@ object Transformer extends Phase[Typechecked, CoreTransformed] {
686686

687687
// create joinpoint
688688
val params = patterns.flatMap { case (sc, p) => boundInPattern(p) } ++ guards.flatMap(boundInGuard)
689-
val joinpoint = Context.bind(TmpBlock("k"), BlockLit(Nil, Nil, params, Nil, body))
689+
val joinpoint = Context.bind(TmpBlock(label), BlockLit(Nil, Nil, params, Nil, body))
690690

691691
def transformPattern(p: source.MatchPattern): Pattern = p match {
692692
case source.AnyPattern(id) =>

0 commit comments

Comments
 (0)