69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
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])
|
|
|
|
for _, register := range registers {
|
|
f.SaveRegister(register)
|
|
}
|
|
|
|
// Push
|
|
for _, register := range f.cpu.General {
|
|
if f.cpu.IsUsed(register) {
|
|
f.assembler.Register(asm.PUSH, register)
|
|
}
|
|
}
|
|
|
|
// Call
|
|
if isSyscall {
|
|
f.assembler.Syscall()
|
|
} else {
|
|
f.assembler.Call(funcName)
|
|
}
|
|
|
|
// Pop
|
|
for i := len(f.cpu.General) - 1; i >= 0; i-- {
|
|
register := f.cpu.General[i]
|
|
|
|
if f.cpu.IsUsed(register) {
|
|
f.assembler.Register(asm.POP, register)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|