Abstract
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.
- Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman. 2007. Compilers: Principles, Techniques, and Tools. Pearson, Boston, 2nd edition.
Google Scholar
- Frances E. Allen. 1970. Control flow analysis. Proceedings of a Symposium on Compiler Optimization, in SIGPLAN Notices, 5 (7): 1–19.
Google Scholar
Digital Library
- Brenda S. Baker. 1977 (January). An algorithm for structuring flowgraphs. Journal of the ACM, 24 (1): 98–120.
Google Scholar
Digital Library
- Bytecode Alliance. 2022. Cranelift code generator. URL https://github.com/bytecodealliance/wasmtime/tree/main/cranelift.
Google Scholar
- Jack W. Davidson and Christopher W. Fraser. 1984 (October). Code selection through object code optimization. ACM Transactions on Programming Languages and Systems, 6 (4): 505–526.
Google Scholar
Digital Library
- Martin Erwig. 2001 (September). Inductive graphs and functional graph algorithms. Journal of Functional Programming, 11 (5): 467–492.
Google Scholar
Digital Library
- Andreas Haas, Andreas Rossberg, Derek L. Schuff, Ben L. Titzer, Michael Holman, Dan Gohman, Luke Wagner, Alon Zakai, and JF Bastien. 2017 (June). Bringing the Web up to speed with WebAssembly. Proceedings of the ACM SIGPLAN ’17 Conference on Programming Language Design and Implementation, in SIGPLAN Notices, 52 (6): 185–200.
Google Scholar
- Matthew S Hecht and Jeffrey D Ullman. 1972. Flow graph reducibility. SIAM Journal on Computing, 1 (2): 188.
Google Scholar
Digital Library
- Yuri Iozzelli. 2019 (April). Solving the structured control flow problem once and for all. URL https://medium.com/leaningtech/solving-the-structured-control-flow-prob lem-once-and-for-all-5123117b1ee2. Blog post, snapshotted in the Internet Archive on December 24, 2021.
Google Scholar
- Johan Janssen and Henk Corporaal. 1997. Making graphs reducible with controlled node splitting. ACM Transactions on Programming Languages and Systems, 19 (6): 1031–1052.
Google Scholar
Digital Library
- Thomas Lengauer and Robert Endre Tarjan. 1979. A fast algorithm for finding dominators in a flowgraph. ACM Transactions on Programming Languages and Systems, 1 (1): 121–141.
Google Scholar
Digital Library
- W. Wesley Peterson, Tadao Kasami, and Nobuki Tokura. 1973. On the capabilities of while, repeat, and exit statements. Communications of the ACM, 16 (8): 503–512.
Google Scholar
Digital Library
- Simon L. Peyton Jones. 1992 (April). Implementing lazy functional languages on stock hardware: The spineless tagless G-machine. Journal of Functional Programming, 2 (2): 127–202.
Google Scholar
Cross Ref
- Simon L. Peyton Jones, Norman Ramsey, and Fermin Reig. 1999 (September). C–: A portable assembly language that supports garbage collection. In International Conference on Principles and Practice of Declarative Programming, LNCS volume 1702, pages 1–28. Springer Verlag. Invited paper.
Google Scholar
Cross Ref
- Norman Ramsey. 2022 (June). Beyond relooper: Recursive translation of unstructured control flow to structured control flow. Software artifact archived at Zenodo.
Google Scholar
Digital Library
- Norman Ramsey and Jo ao Dias. 2005 (September). An applicative control-flow graph based on Huet’s zipper. In ACM SIGPLAN Workshop on ML, pages 101–122.
Google Scholar
- Norman Ramsey, Jo ao Dias, and Simon L. Peyton Jones. 2010. Hoopl: A modular, reusable library for dataflow analysis and transformation. Proceedings of the 3rd ACM SIGPLAN Symposium on Haskell (Haskell 2010), in SIGPLAN Notices, 45 (11): 121–134.
Google Scholar
Digital Library
- Norman Ramsey, Simon L. Peyton Jones, and Christian Lindig. 2005 (February). The C– language specification Version 2.0 (CVS revision 1.128). See http://www.cs.tufts.edu/~nr/c–/code.html#spec.
Google Scholar
- Lyle Ramshaw. 1988. Eliminating go to’s while preserving program structure. Journal of the ACM, 35 (4): 893–920.
Google Scholar
Digital Library
- Martin Sulzmann, Manuel MT Chakravarty, Simon Peyton Jones, and Kevin Donnelly. 2007. System F with type equality coercions. In Proceedings of the 2007 ACM SIGPLAN International Workshop on Types in Language Design and Implementation (TLDI), pages 53–66.
Google Scholar
Digital Library
- Ben L. Titzer. 2022. A fast in-place interpreter for WebAssembly. PACMPL, 6 (OOPSLA). To appear.
Google Scholar
- Alon Zakai. 2011. Emscripten: An LLVM-to-JavaScript compiler. In Proceedings of the ACM International Conference on Object Oriented Programming Systems Languages and Applications, OOPSLA ’11, page 301–312.
Google Scholar
Digital Library
Index Terms
Beyond Relooper: recursive translation of unstructured control flow to structured control flow (functional pearl)
Recommendations
Pushdown control-flow analysis for free
POPL '16Traditional control-flow analysis (CFA) for higher-order languages introduces spurious connections between callers and callees, and different invocations of a function may pollute each other's return flows. Recently, three distinct approaches have been ...
Interprocedural control flow analysis of first-order programs with tail-call optimization
Knowledge of low-level control flow is essential for many compiler optimizations. In systems with tail-call optimization, the determination of interprocedural control flow is complicated by the fact that because of tail-call optimization, control flow ...
Pushdown control-flow analysis for free
POPL '16: Proceedings of the 43rd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming LanguagesTraditional control-flow analysis (CFA) for higher-order languages introduces spurious connections between callers and callees, and different invocations of a function may pollute each other's return flows. Recently, three distinct approaches have been ...






Comments