一句话:提出单趟递归算法,将任意控制流图翻译为结构化控制流,并计划集成到 GHC。
许多编译器用任意有向图表示过程内控制流,但 JavaScript、WebAssembly 等目标语言只允许由循环、条件与多级 break/exit 构成的结构化控制流。Peterson、Kasami 和 Tokura 在 1973 年给出了任意控制流到结构化控制流的翻译,但原始版本需三趟,可能因此被 Emscripten 忽视。本文调整标准控制流分析,并借助函数式编程的递归函数、不可变 AST 与支配树,将该翻译与若干代码改进合并为单趟实现。该实现计划加入 Glasgow Haskell Compiler,且其单趟翻译、不可变表示和支配树使用比原版更易推理。
In many compilers, control flow is represented using an arbitrary directed graph. But in some interesting target languages, including JavaScript and WebAssembly, intraprocedural control flow can be expressed only in structured ways, using loops, conditionals, and multilevel breaks or exits. As was shown by Peterson, Kasami, and Tokura in 1973, such structured control flow can be obtained by translating arbitrary control flow. The translation uses two standard analyses, but as published, it takes three passes—which may explain why it was overlooked by Emscripten, a popular compiler from C to JavaScript. By tweaking the analyses and by applying fundamental ideas from functional programming (recursive functions and immutable abstract-syntax trees), the translation, along with a couple of code improvements, can be implemented in a single pass. This new implementation is slated to be added to the Glasgow Haskell Compiler. Its single-pass translation, its immutable representation, and its use of dominator trees make it much easier to reason about than the original translation.
@article{paperbot1509,
title = {Beyond Relooper: recursive translation of unstructured control flow to structured control flow (functional pearl)},
author = {Norman Ramsey},
journal = {Proceedings of the ACM on Programming Languages},
volume = {6},
number = {ICFP},
year = {2022},
doi = {10.1145/3547621}
}