1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-10-25 05:24:11 +00:00

Fix bad code deduplication

This commit is contained in:
Karol Stasiak 2018-10-12 00:01:19 +02:00
parent 30c979cc39
commit 6b33cc49a8

View File

@ -18,6 +18,10 @@ abstract class Deduplicate[T <: AbstractCode](env: Environment, options: Compila
} }
runStage(compiledFunctions, deduplicateIdenticalFunctions) runStage(compiledFunctions, deduplicateIdenticalFunctions)
runStage(compiledFunctions, eliminateTailJumps) runStage(compiledFunctions, eliminateTailJumps)
runStage(compiledFunctions, eliminateTailJumps)
runStage(compiledFunctions, eliminateTailJumps)
runStage(compiledFunctions, eliminateRemainingTrivialTailJumps)
runStage(compiledFunctions, eliminateRemainingTrivialTailJumps)
runStage(compiledFunctions, eliminateRemainingTrivialTailJumps) runStage(compiledFunctions, eliminateRemainingTrivialTailJumps)
fixDoubleRedirects(compiledFunctions) fixDoubleRedirects(compiledFunctions)
// println(compiledFunctions.map { // println(compiledFunctions.map {
@ -28,6 +32,8 @@ abstract class Deduplicate[T <: AbstractCode](env: Environment, options: Compila
// }.mkString(" ; ")) // }.mkString(" ; "))
} }
def removeChains(map: Map[String, String]): Map[String, String] = map.filterNot{case (_, to) => map.contains(to)}
def runStage(compiledFunctions: mutable.Map[String, CompiledFunction[T]], def runStage(compiledFunctions: mutable.Map[String, CompiledFunction[T]],
function: (String, Map[String, Either[String, CodeAndAlignment[T]]]) => Seq[(String, CompiledFunction[T])]): Unit = { function: (String, Map[String, Either[String, CodeAndAlignment[T]]]) => Seq[(String, CompiledFunction[T])]): Unit = {
bySegment(compiledFunctions).foreach { bySegment(compiledFunctions).foreach {
@ -195,7 +201,7 @@ abstract class Deduplicate[T <: AbstractCode](env: Environment, options: Compila
.map(name -> _) .map(name -> _)
case _ => None case _ => None
} }
val fallthroughPredecessors = fallThroughList.groupBy(_._2).mapValues(_.head._1) // TODO: be smarter than head val fallthroughPredecessors = removeChains(fallThroughList).groupBy(_._2).mapValues(_.head._1) // TODO: be smarter than head
fallthroughPredecessors.foreach { fallthroughPredecessors.foreach {
case (to, from) => case (to, from) =>
options.log.debug(s"Fallthrough from $from to $to") options.log.debug(s"Fallthrough from $from to $to")
@ -228,7 +234,7 @@ abstract class Deduplicate[T <: AbstractCode](env: Environment, options: Compila
.map(name -> _) .map(name -> _)
case _ => None case _ => None
} }
val fallthroughPredecessors = fallThroughList.groupBy(_._2).mapValues(_.keySet) val fallthroughPredecessors = removeChains(fallThroughList).groupBy(_._2).mapValues(_.keySet)
fallthroughPredecessors.foreach { fallthroughPredecessors.foreach {
case (to, froms) => case (to, froms) =>
for (from <- froms) { for (from <- froms) {