1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-07-05 09:28:54 +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, eliminateTailJumps)
runStage(compiledFunctions, eliminateTailJumps)
runStage(compiledFunctions, eliminateTailJumps)
runStage(compiledFunctions, eliminateRemainingTrivialTailJumps)
runStage(compiledFunctions, eliminateRemainingTrivialTailJumps)
runStage(compiledFunctions, eliminateRemainingTrivialTailJumps)
fixDoubleRedirects(compiledFunctions)
// println(compiledFunctions.map {
@ -28,6 +32,8 @@ abstract class Deduplicate[T <: AbstractCode](env: Environment, options: Compila
// }.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]],
function: (String, Map[String, Either[String, CodeAndAlignment[T]]]) => Seq[(String, CompiledFunction[T])]): Unit = {
bySegment(compiledFunctions).foreach {
@ -195,7 +201,7 @@ abstract class Deduplicate[T <: AbstractCode](env: Environment, options: Compila
.map(name -> _)
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 {
case (to, from) =>
options.log.debug(s"Fallthrough from $from to $to")
@ -228,7 +234,7 @@ abstract class Deduplicate[T <: AbstractCode](env: Environment, options: Compila
.map(name -> _)
case _ => None
}
val fallthroughPredecessors = fallThroughList.groupBy(_._2).mapValues(_.keySet)
val fallthroughPredecessors = removeChains(fallThroughList).groupBy(_._2).mapValues(_.keySet)
fallthroughPredecessors.foreach {
case (to, froms) =>
for (from <- froms) {