Improved code generation

This commit is contained in:
2024-07-03 16:37:59 +02:00
parent 795935ddfb
commit 4d88260333
15 changed files with 215 additions and 275 deletions

View File

@ -1,11 +1,8 @@
package core
import (
"strconv"
"git.akyoto.dev/cli/q/src/build/ast"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/errors"
"git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/token"
)
@ -16,54 +13,24 @@ func (f *Function) Execute(operation token.Token, register cpu.Register, value *
return f.ExecuteLeaf(operation, register, value.Token)
}
var temporary cpu.Register
if ast.IsFunctionCall(value) {
temporary = f.cpu.Return[0]
} else {
found := false
temporary, found = f.cpu.FindFree(f.cpu.General)
if !found {
panic("no free registers")
}
}
f.cpu.Use(temporary)
defer f.cpu.Free(temporary)
err := f.EvaluateTo(value, temporary)
if err != nil {
return err
}
return f.opRegisterRegister(operation, register, temporary)
}
// ExecuteLeaf performs an operation on a register with the given leaf operand.
func (f *Function) ExecuteLeaf(operation token.Token, register cpu.Register, operand token.Token) error {
switch operand.Kind {
case token.Identifier:
name := operand.Text()
variable, exists := f.variables[name]
if !exists {
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, operand.Position)
}
defer f.useVariable(variable)
return f.opRegisterRegister(operation, register, variable.Register)
case token.Number:
value := operand.Text()
number, err := strconv.Atoi(value)
err := f.CompileCall(value)
if err != nil {
return err
}
return f.opRegisterNumber(operation, register, number)
return f.ExecuteRegisterRegister(operation, register, f.cpu.Output[0])
}
return errors.New(errors.NotImplemented, f.File, operation.Position)
tmp := f.cpu.MustUseFree(f.cpu.General)
defer f.cpu.Free(tmp)
err := f.ExpressionToRegister(value, tmp)
if err != nil {
return err
}
return f.ExecuteRegisterRegister(operation, register, tmp)
}