Implement tail calls for continuation calling

This commit is contained in:
Konstantin Nazarov 2024-11-03 18:52:50 +00:00
parent d017884c5b
commit 0ce71beb62
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22

View file

@ -132,11 +132,17 @@ Result<void> VM::vm_call_cont(Opcode& oc, Continuation& fun, bool tail) {
if (reg_end - reg_start != 2) return ERROR(ArgumentCountMismatch);
auto param = TRY(_stack.get(reg_start + 1));
_stack = TRY(_stack.settop(reg_start));
auto cont_stack = TRY(fun.frame());
_stack = TRY(_stack.incpc());
_stack = TRY(cont_stack.attach(_stack));
if (!tail) {
_stack = TRY(_stack.settop(reg_start));
_stack = TRY(_stack.incpc());
_stack = TRY(cont_stack.attach(_stack));
} else {
auto par = TRY(_stack.parent());
if (!par.is<StackFrame>()) return ERROR(TypeMismatch);
_stack = TRY(cont_stack.attach(*par.to<StackFrame>()));
}
_stack = TRY(_stack.set(TRY(_stack.size()), param));
return Result<void>();