Implemented a basic optimization

This commit is contained in:
Eduard Urbach 2024-07-09 11:43:33 +02:00
parent 58d3aaf21d
commit 6102ab466a
Signed by: eduard
GPG key ID: 49226B848C78F6C8
8 changed files with 48 additions and 14 deletions

View file

@ -0,0 +1,34 @@
package asm
import "git.akyoto.dev/cli/q/src/build/cpu"
// unnecessary returns true if the register/register operation can be skipped.
func (a *Assembler) unnecessary(mnemonic Mnemonic, left cpu.Register, right cpu.Register) bool {
if mnemonic == MOVE && left == right {
return true
}
if len(a.Instructions) == 0 {
return false
}
last := a.Instructions[len(a.Instructions)-1]
if mnemonic == MOVE && last.Mnemonic == MOVE {
data, isRegReg := last.Data.(*RegisterRegister)
if !isRegReg {
return false
}
if data.Destination == left && data.Source == right {
return true
}
if data.Destination == right && data.Source == left {
return true
}
}
return false
}