q/src/register/SaveRegister.go

31 lines
618 B
Go

package register
import (
"slices"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/cpu"
)
// SaveRegister attempts to move a variable occupying this register to another register.
func (f *Machine) SaveRegister(register cpu.Register) {
if !f.RegisterIsUsed(register) {
return
}
if slices.Contains(f.CPU.General, register) {
return
}
variable := f.VariableByRegister(register)
if variable == nil || variable.Value.Alive == 0 {
return
}
newRegister := f.NewRegister()
f.RegisterRegister(asm.MOVE, newRegister, register)
variable.Value.Register = newRegister
f.FreeRegister(register)
}