Improved code generation

This commit is contained in:
Eduard Urbach 2024-07-09 12:43:29 +02:00
parent 8103faa8b6
commit 1204591cdc
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 12 additions and 7 deletions

View File

@ -35,6 +35,12 @@ func (f *Function) CompileCall(root *expression.Expression) error {
return err return err
} }
f.SaveRegister(f.cpu.Output[0])
for _, register := range registers {
f.SaveRegister(register)
}
// Push // Push
for _, register := range f.cpu.General { for _, register := range f.cpu.General {
if f.cpu.IsUsed(register) { if f.cpu.IsUsed(register) {

View File

@ -34,6 +34,8 @@ func (f *Function) ExpressionToRegister(node *expression.Expression, register cp
if f.UsesRegister(right, register) { if f.UsesRegister(right, register) {
register = f.cpu.MustFindFree(f.cpu.General) register = f.cpu.MustFindFree(f.cpu.General)
} else {
f.SaveRegister(register)
} }
f.cpu.Reserve(register) f.cpu.Reserve(register)

View File

@ -47,13 +47,6 @@ func NewFunction(name string, file *fs.File, body token.List) *Function {
func (f *Function) Compile() { func (f *Function) Compile() {
defer close(f.finished) defer close(f.finished)
f.assembler.Label(asm.LABEL, f.Name) f.assembler.Label(asm.LABEL, f.Name)
for _, register := range f.cpu.Input {
if f.cpu.IsUsed(register) {
f.SaveRegister(register)
}
}
f.err = f.CompileTokens(f.Body) f.err = f.CompileTokens(f.Body)
f.assembler.Return() f.assembler.Return()
} }

View File

@ -10,6 +10,10 @@ import (
// SaveRegister attempts to move a variable occupying this register to another register. // SaveRegister attempts to move a variable occupying this register to another register.
func (f *Function) SaveRegister(register cpu.Register) { func (f *Function) SaveRegister(register cpu.Register) {
if !f.cpu.IsUsed(register) {
return
}
var variable *Variable var variable *Variable
for _, v := range f.variables { for _, v := range f.variables {