xo-interpreter2 stack: invoke closures w/ tail-call opt [WIP]

This commit is contained in:
Roland Conybeare 2026-02-13 02:05:47 -05:00
commit cfa5692804
14 changed files with 174 additions and 87 deletions

View file

@ -21,7 +21,6 @@ set(SELF_SRCS
IPrintable_DVsmApplyClosureFrame.cpp
DClosure.cpp
IProcedure_DClosure.cpp
IGCObject_DClosure.cpp
IPrintable_DClosure.cpp

View file

@ -36,6 +36,14 @@ namespace xo {
DClosure::apply_nocheck(obj<ARuntimeContext> rcx,
const DArray * args)
{
// control here only if you try to invoke a closure
// as a procedure.
//
// May support this later, but requires
// nesting VSM (because call consumes c++ stack)
//
// typically prefer trampoline built into VSM
(void)args;
scope log(XO_DEBUG(true));
@ -45,23 +53,6 @@ namespace xo {
log && log(xtag("vsm_rcx.data", (void*)vsm_rcx.data()));
// we already checked this stuff before calling apply_nocheck()
// assert (n_args == args->size());
#ifdef NOT_YET
auto local_env
= DLocalEnv::_make(vsm_rcx->allocator(),
env_,
lambda_->local_symtab(),
args);
#endif
// plan:
// 1. push current local environment to vsm stack_
// 2. set expr_ to lambda body
// 2. set pc_ to eval
// 3. set cont_ to restore local_env_
auto err_mm
= vsm_rcx->error_allocator();

View file

@ -7,6 +7,7 @@
namespace xo {
using xo::mm::AGCObject;
using xo::reflect::typeseq;
namespace scm {
@ -18,6 +19,18 @@ namespace xo {
local_env_{local_env}
{}
DVsmApplyClosureFrame *
DVsmApplyClosureFrame::make(obj<AAllocator> mm,
obj<AGCObject> stack,
VsmInstr cont,
DLocalEnv * local_env)
{
void * mem = mm.alloc(typeseq::id<DVsmApplyClosureFrame>(),
sizeof(DVsmApplyClosureFrame));
return new (mem) DVsmApplyClosureFrame(stack, cont, local_env);
}
std::size_t
DVsmApplyClosureFrame::shallow_size() const noexcept
{

View file

@ -1,39 +0,0 @@
/** @file IProcedure_DClosure.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IProcedure_DClosure.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IProcedure_DClosure.json5]
**/
#include "detail/IProcedure_DClosure.hpp"
namespace xo {
namespace scm {
auto
IProcedure_DClosure::is_nary(const DClosure & self) noexcept -> bool
{
return self.is_nary();
}
auto
IProcedure_DClosure::n_args(const DClosure & self) noexcept -> std::int32_t
{
return self.n_args();
}
auto
IProcedure_DClosure::apply_nocheck(DClosure & self, obj<ARuntimeContext> rcx, const DArray * args) -> obj<AGCObject>
{
return self.apply_nocheck(rcx, args);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IProcedure_DClosure.cpp */

View file

@ -6,6 +6,7 @@
#include "VirtualSchematikaMachine.hpp"
#include "VsmApplyFrame.hpp"
#include "VsmEvalArgsFrame.hpp"
#include "VsmApplyClosureFrame.hpp"
#include "VsmRcx.hpp"
#include "Closure.hpp"
#include <xo/expression2/ApplyExpr.hpp>
@ -164,9 +165,9 @@ namespace xo {
log && log(xtag("pc", pc_),
xtag("cont", cont_));
obj<APrintable> stack_pr
= (FacetRegistry::instance()
.try_variant<APrintable,AGCObject>(stack_));
obj<APrintable> stack_pr = stack_.to_facet<APrintable>();
// = (FacetRegistry::instance()
// .try_variant<APrintable,AGCObject>(stack_));
if (stack_pr)
log && log(xtag("stack", stack_pr));
@ -184,6 +185,9 @@ namespace xo {
case vsm_opcode::evalargs:
_do_evalargs_op();
break;
case vsm_opcode::applycoda:
_do_applycoda_op();
break;
}
return true;
@ -370,10 +374,68 @@ namespace xo {
// TODO: check argument types
this->value_ = VsmResult(fn_.apply_nocheck(rcx_.to_op(), args_));
this->pc_ = cont_;
auto closure = obj<AGCObject,DClosure>::from(fn_);
return;
if (closure) {
_do_call_closure_op();
return;
} else {
_do_call_closure_op();
return;
}
}
void
VirtualSchematikaMachine::_do_call_closure_op()
{
// We need to preserve registers while evaluating
// lambda body
auto closure = obj<AGCObject,DClosure>::from(fn_);
// TODO: for tail recursion:
// check whether stack_ already refers to a
// DVsmApplyClosureFrame instance, in which case
// we can just refer to it instead of pushing a new one
if (cont_ == VsmInstr::c_applycoda) {
// we are making a tail call.
// No need to preserve (stack, cont, local_env),
// since continuation will restore on top of them
// frame top stackframe anyway
} else {
obj<AGCObject,
DVsmApplyClosureFrame> frame(
DVsmApplyClosureFrame::make(mm_.to_op(),
stack_,
cont_,
local_env_));
// push frame w/ saved vsm registers
this->stack_ = frame;
this->cont_ = VsmInstr::c_applycoda;
}
auto lambda = closure->lambda();
auto local_env
= DLocalEnv::_make(mm_.to_op(),
local_env_,
lambda->local_symtab(),
args_);
this->local_env_ = local_env;
this->expr_ = lambda->body_expr();
this->pc_ = VsmInstr::c_eval;
}
void
VirtualSchematikaMachine::_do_call_primitive_op()
{
auto fn = fn_.to_facet<AProcedure>();
this->value_ = VsmResult(fn.apply_nocheck(rcx_.to_op(), args_));
this->pc_ = cont_;
}
void
@ -434,10 +496,11 @@ namespace xo {
= evalargs_frame->apply_expr();
if (i_arg == -1) {
auto fn = value.to_facet<AProcedure>();
bool is_native_fn = value.to_facet<AProcedure>();
bool is_closure = obj<AGCObject,DClosure>::from(value);
if (fn) {
apply_frame->assign_fn(fn);
if (is_native_fn || is_closure) {
apply_frame->assign_fn(value);
i_arg = evalargs_frame->increment_arg();
@ -493,6 +556,22 @@ namespace xo {
assert(false);
}
void
VirtualSchematikaMachine::_do_applycoda_op()
{
// see DVsmApplyClosureFrame
auto frame = obj<AGCObject,DVsmApplyClosureFrame>::from(stack_);
assert(frame);
this->stack_ = frame->stack();
this->local_env_ = frame->local_env();
this->pc_ = frame->cont();
// not implemented
assert(false);
}
} /*namespace scm*/
} /*namespace xo*/

View file

@ -15,6 +15,7 @@ namespace xo {
case vsm_opcode::eval: return "eval";
case vsm_opcode::apply: return "apply";
case vsm_opcode::evalargs: return "evalargs";
case vsm_opcode::applycoda: return "applycoda";
case vsm_opcode::N:
break;
}
@ -33,6 +34,9 @@ namespace xo {
VsmInstr
VsmInstr::c_evalargs = VsmInstr(vsm_opcode::evalargs);
VsmInstr
VsmInstr::c_applycoda = VsmInstr(vsm_opcode::applycoda);
} /*namespace scm*/
} /*namespace xo*/

View file

@ -59,9 +59,9 @@ namespace xo {
FacetRegistry::register_impl<AGCObject, DPrimitive_gco_2_gco_gco>();
FacetRegistry::register_impl<APrintable, DPrimitive_gco_2_gco_gco>();
FacetRegistry::register_impl<AProcedure, DClosure>();
FacetRegistry::register_impl<AGCObject, DClosure>();
FacetRegistry::register_impl<APrintable, DClosure>();
// FacetRegistry::register_impl<AProcedure, DClosure>();
// FacetRegistry::register_impl<AGCObject, DClosure>();
// FacetRegistry::register_impl<APrintable, DClosure>();
// RuntimeContext
// \- VsmRcx