Implemented return values
This commit is contained in:
37
src/build/SaveRegister.go
Normal file
37
src/build/SaveRegister.go
Normal file
@ -0,0 +1,37 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/build/asm"
|
||||
"git.akyoto.dev/cli/q/src/build/cpu"
|
||||
)
|
||||
|
||||
// SaveRegister attempts to move a variable occupying this register to another register.
|
||||
func (f *Function) SaveRegister(register cpu.Register) {
|
||||
if f.cpu.IsFree(register) {
|
||||
return
|
||||
}
|
||||
|
||||
var variable *Variable
|
||||
|
||||
for _, v := range f.variables {
|
||||
if v.Register == register {
|
||||
variable = v
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if variable == nil || variable.UsesRemaining == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
newRegister, exists := f.cpu.FindFree(f.cpu.General)
|
||||
|
||||
if !exists {
|
||||
panic("no free registers")
|
||||
}
|
||||
|
||||
f.assembler.RegisterRegister(asm.MOVE, newRegister, register)
|
||||
f.cpu.Free(register)
|
||||
f.cpu.Use(newRegister)
|
||||
variable.Register = newRegister
|
||||
}
|
Reference in New Issue
Block a user