Implemented return values

This commit is contained in:
2024-06-29 21:06:59 +02:00
parent 3e47a12ecd
commit 83640ba590
7 changed files with 113 additions and 61 deletions

37
src/build/SaveRegister.go Normal file
View 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
}