1
0
mirror of https://github.com/KarolS/millfork.git synced 2024-06-12 06:29:34 +00:00
millfork/src/main/scala/millfork/assembly/opt/UnusedLabelRemoval.scala

39 lines
1.2 KiB
Scala
Raw Normal View History

2017-12-06 23:23:30 +00:00
package millfork.assembly.opt
import millfork.CompilationOptions
import millfork.assembly.AddrMode._
import millfork.assembly.Opcode._
import millfork.assembly.{AddrMode, AssemblyLine}
import millfork.env._
import millfork.error.ErrorReporting
/**
* @author Karol Stasiak
*/
object UnusedLabelRemoval extends AssemblyOptimization {
override def optimize(f: NormalFunction, code: List[AssemblyLine], options: CompilationOptions): List[AssemblyLine] = {
val usedLabels = code.flatMap {
case AssemblyLine(LABEL, _, _, _) => None
case AssemblyLine(_, _, MemoryAddressConstant(Label(l)), _) => Some(l)
case _ => None
}.toSet
val definedLabels = code.flatMap {
case AssemblyLine(LABEL, _, MemoryAddressConstant(Label(l)), _) => Some(l).filter(_.startsWith("."))
case _ => None
}.toSet
val toRemove = definedLabels -- usedLabels
if (toRemove.nonEmpty) {
ErrorReporting.debug("Removing labels: " + toRemove.mkString(", "))
code.filterNot {
case AssemblyLine(LABEL, _, MemoryAddressConstant(Label(l)), _) => toRemove(l)
case _ => false
}
} else {
code
}
}
override def name = "Unused label removal"
}