延续传球风格与积极修剪的调用堆栈?

时间:2022-09-22 20:42:16

I'm considering something like CPS for use in an interpreter for an actor based language.

我正在考虑像CPS这样的东西,用于基于演员的语言的翻译。

The function arguments are passed in an array of variants, and the continuation returned in the same array, so an simple function

函数参数在变量数组中传递,并且continuation在同一个数组中返回,因此是一个简单的函数

def add (x,y) => x + y

so a call from the read/eval/loop would be

所以来自read / eval / loop的调用就是

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

等等。 C中的实现基本上是

for (;;)
   args = (args[0].function_pointer)(args);

with checks for running off the end of the args array and allocating more space.

检查运行args数组的末尾并分配更多空间。

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.

虽然这个方案的灵感来自于对CPS的思考,但它并不是CPS,并且与积极修剪的C堆栈看起来非常相似 - 只是实时变量,以及每个函数的返回点。

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.

这个样式有名称,特别是参数数组吗?它是一种蹦床+堆栈,虽然我习惯称之为'堆栈'更多的是历史而不是执行的未来。

1 个解决方案

#1


This is almost Forth. Having a first-class stack is pretty much like having continuations.

这几乎是Forth。拥有一流的堆栈就像拥有延续一样。

#1


This is almost Forth. Having a first-class stack is pretty much like having continuations.

这几乎是Forth。拥有一流的堆栈就像拥有延续一样。