继续传递风格与积极修剪调用堆栈?
我正在考虑将 CPS 之类的东西用于基于 Actor 的语言的解释器中。
函数参数在变体数组中传递,延续在同一个数组中返回,因此一个简单的函数,
def add (x,y) => x + y
因此来自读取/评估/循环的调用将
print( add(7, 5) )
在入口处,
[&add, x, y, &print, _, &repl, ...]
其中 _ 是函数返回的空槽值被写入。
在下一步执行时,参数变为
[&print, 12, &repl, ...]
then
[repl, ...]
等等。 C 中的实现基本上是
for (;;)
args = (args[0].function_pointer)(args);
检查 args 数组的末尾并分配更多空间。
参数是连续的,并且作为对象的“延续”只是参数的子集。
如果我要实现一流的延续,他们将需要克隆参数数组; 你也不会免费得到关闭。 主要目标是能够很好地生成简单的机器代码,并允许您暂停和恢复执行。
虽然这个方案的灵感来自于对 CPS 的思考,但它并不完全是 CPS,并且与积极修剪的 C 堆栈的外观非常相似 - 只是实时变量和每个函数的返回点。
这种样式,特别是参数数组有名称吗? 它有点像蹦床+堆栈,尽管我习惯称“堆栈”更多的是执行的历史而不是未来。
I'm considering something like CPS for use in an interpreter for an actor based language.
The function arguments are passed in an array of variants, and the continuation returned in the same array, so an simple function
def add (x,y) => x + y
so a call from the read/eval/loop would be
print( add(7, 5) )
would on entry be
[&add, x, y, &print, _, &repl, ...]
where _ is an empty slot where the function return value is written.
At the next step of execution, the arguments become
[&print, 12, &repl, ...]
then
[repl, ...]
and so on. The implementation in C is basically
for (;;)
args = (args[0].function_pointer)(args);
with checks for running off the end of the args array and allocating more space.
The arguments are contiguous, and the 'continuation' as an object is just a subset of the arguments.
If I were to implement first-class continuations, they would need cloning the argument array; you also don't get closures for free. The main goal is something which plays well with simple generation of machine code, and lets you suspend and resume execution.
Although this scheme was inspired by thinking about CPS, it isn't quite CPS, and is very similar to what an aggressively trimmed C stack might look like - just the live variables, and the points of return for each function.
Is there a name for this style, and particularly the argument array? It's sort of trampolines + a stack, though what I'm used to calling 'stack' is more the history rather than the future of the execution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这几乎就是福斯了。 拥有一流的堆栈非常类似于拥有延续。
This is almost Forth. Having a first-class stack is pretty much like having continuations.