c65gm/internal/optimizer/pass_self.go

34 lines
792 B
Go

package optimizer
import "strings"
// passSelfAssignment removes lda X; sta X pairs where the store immediately
// follows the load and neither line is a branch target.
func passSelfAssignment(lines []asmLine) []asmLine {
var result []asmLine
skipNext := false
for i := 0; i < len(lines); i++ {
if skipNext {
skipNext = false
continue
}
line := lines[i]
// Check for lda X; sta X
if line.isCode && line.opcode == "lda" && !isImmediate(line.operand) && i+1 < len(lines) {
next := lines[i+1]
if next.isCode && next.opcode == "sta" && next.operand == line.operand {
if !strings.Contains(line.operand, "+") {
skipNext = true // skip sta on next iteration
continue // skip lda
}
}
}
result = append(result, line)
}
return result
}