package core import ( "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/errors" "git.akyoto.dev/cli/q/src/build/expression" ) // CompileCall executes a function call. // 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(root *expression.Expression) error { funcName := root.Children[0].Token.Text() isSyscall := funcName == "syscall" if !isSyscall { _, exists := f.Functions[funcName] if !exists { return errors.New(&errors.UnknownFunction{Name: funcName}, f.File, root.Children[0].Token.Position) } } parameters := root.Children[1:] registers := f.cpu.Input[:len(parameters)] if isSyscall { registers = f.cpu.Syscall[:len(parameters)] } err := f.ExpressionsToRegisters(parameters, registers) if err != nil { return err } f.SaveRegister(f.cpu.Output[0]) // Push for _, register := range f.cpu.General { if f.Scope().IsUsed(register) { f.Register(asm.PUSH, register) } } // Call if isSyscall { f.Syscall() } else { f.Call(funcName) } for _, register := range registers { if register == f.cpu.Output[0] && root.Parent != nil { continue } f.Scope().Free(register) } // Pop for i := len(f.cpu.General) - 1; i >= 0; i-- { register := f.cpu.General[i] if f.Scope().IsUsed(register) { f.Register(asm.POP, register) } } return nil }