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

@ -1,7 +1,7 @@
package core
import (
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/expression"
)
@ -9,9 +9,43 @@ import (
// All call registers must hold the correct parameter values before the function invocation.
// Registers that are in use must be saved if they are modified by the function.
// After the function call, they must be restored in reverse order.
func (f *Function) CompileCall(expr *expression.Expression) error {
_, err := f.EvaluateCall(expr)
return err
func (f *Function) CompileCall(root *expression.Expression) error {
funcName := root.Children[0].Token.Text()
parameters := root.Children[1:]
registers := f.cpu.Input[:len(parameters)]
isSyscall := funcName == "syscall"
if isSyscall {
registers = f.cpu.Syscall[:len(parameters)]
}
err := f.ExpressionsToRegisters(parameters, registers)
if err != nil {
return err
}
for _, register := range f.cpu.General {
if !f.cpu.IsFree(register) {
f.assembler.Register(asm.PUSH, register)
}
}
if isSyscall {
f.assembler.Syscall()
} else {
f.assembler.Call(funcName)
}
for i := len(f.cpu.General) - 1; i >= 0; i-- {
register := f.cpu.General[i]
if !f.cpu.IsFree(register) {
f.assembler.Register(asm.POP, register)
}
}
return nil
}
// CompileSyscall executes a syscall.
@ -23,17 +57,3 @@ func (f *Function) CompileSyscall(expr *expression.Expression) error {
f.sideEffects++
return err
}
// ExpressionsToRegisters moves multiple expressions into the specified registers.
func (f *Function) ExpressionsToRegisters(expressions []*expression.Expression, registers []cpu.Register) error {
for i := len(registers) - 1; i >= 0; i-- {
f.SaveRegister(registers[i])
err := f.EvaluateTo(expressions[i], registers[i])
if err != nil {
return err
}
}
return nil
}