Simplified the assembler

This commit is contained in:
Eduard Urbach 2025-02-02 11:48:08 +01:00
parent 6c432154e6
commit a0388ae15a
Signed by: eduard
GPG key ID: 49226B848C78F6C8
5 changed files with 146 additions and 122 deletions

30
src/asm/unnecessary.go Normal file
View file

@ -0,0 +1,30 @@
package asm
import "git.akyoto.dev/cli/q/src/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 len(a.Instructions) == 0 {
return false
}
last := a.Instructions[len(a.Instructions)-1]
if mnemonic == MOVE && last.Mnemonic == MOVE {
lastData, isRegReg := last.Data.(*RegisterRegister)
if !isRegReg {
return false
}
if lastData.Destination == left && lastData.Source == right {
return true
}
if lastData.Destination == right && lastData.Source == left {
return true
}
}
return false
}