2

我正在设计一个预先编译 CIL 字节码的系统。为了保持它相对简单并使其非常可移植,系统将发出 C 源代码(但是排除了 OOP 等所有更高级别的结构)而不是机器代码。目的是目标平台的标准 C 编译器将用于该代码以获得最终产品。

最初我打算使用一种非常简单的 GC 方法,例如 stop-the-world。然而,虽然应用程序不需要出色的性能,但它确实需要不错的性能,因此最终可能需要更改 GC。

我正在考虑最终需要某种写入障碍的更复杂的 GC。我已经研究过 SATB 和卡片标记方法,但我还没有准备好实际计划一个好的 GC。我只是不想让这个东西发出 C 源代码,只是后来发现一个有效的 GC 写入障碍需要内联汇编,这在很大程度上违背了发出 C 的目的。

所以,我的问题是,典型的写障碍可以在 C 代码中有效地实现吗?我们可以假设 C 编译器有一个不错的优化器。生成的“源代码”将完全难以辨认,这也是一个假设,因此清晰度并不重要。

我猜想——以更加膨胀的源文件为代价——它可能可以合理地完成,但我会感谢在 GC 设计和/或编译器内部更有经验的人的话。

4

1 回答 1

4

I am assuming you want a precise generational moving or copying GC.

You could have a write barrier in C; as an example, both Ocaml and MELT runtimes have generational GC with a write barrier. And qish is a generational copying GC with a write barrier, working with C.

(BTW, MELT is a domain specific language to extend GCC, and it is compiled to C, exactly like you want to do)

A more significant issue is how do you keep local pointers (and how the GC knows about them), that is the precise aspect of your GC. You may want to pack them in some local structure.... But then it could happen that the C compiler (e.g. GCC) is optimizing a bit less.

You might look into the source code of recent versions of MONO, they have a generational copying GC. Look also inside Chicken Scheme (also generating C code).

Notice that your C code generator will have to be changed to fit inside some (or yours) particular GC implementation (because each GC has slightly different invariants and expectations). Take also care of tail recursion (some C compilers, notably recent GCC, may optimize them in limited cases).

In Qish, MELT or Ocaml the write barrier (on the C side) is implemented by some macro (or inline functions) called for each touched pointer. Details are implementation specific. Your C code generator will have to take care of them.

Be careful that multi-threaded GC are difficult to design, and that debugging GC, even simple ones, takes a lot of time and is difficult.

于 2013-03-17T02:11:31.993 回答