36 lines
804 B
Go
36 lines
804 B
Go
package optimizer
|
|
|
|
// Optimize applies all enabled peephole passes to the generated ASM lines.
|
|
// Each pass runs sequentially on a parsed representation of the lines.
|
|
// @@OPT markers are stripped from output.
|
|
func Optimize(lines []string, cfg *Config) []string {
|
|
if cfg == nil || !cfg.Any() {
|
|
return lines
|
|
}
|
|
|
|
parsed := parseLines(lines)
|
|
|
|
if cfg.Debug {
|
|
parsed = debugWithOriginals(parsed, "before peephole")
|
|
}
|
|
|
|
if cfg.EnableLoad {
|
|
parsed = passLoadElimination(parsed)
|
|
}
|
|
if cfg.EnableImm {
|
|
parsed = passImmElimination(parsed)
|
|
}
|
|
if cfg.EnableJmp {
|
|
parsed = passJmpNext(parsed)
|
|
}
|
|
if cfg.EnableSelf {
|
|
parsed = passSelfAssignment(parsed)
|
|
}
|
|
|
|
if cfg.Debug {
|
|
parsed = debugWithOriginals(parsed, "after peephole")
|
|
}
|
|
|
|
parsed = stripOptMarkers(parsed)
|
|
return linesToString(parsed)
|
|
}
|