skip to main content

Beyond Relooper: recursive translation of unstructured control flow to structured control flow (functional pearl)

Published:31 August 2022Publication History
Skip Abstract Section

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.

References

  1. Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman. 2007. Compilers: Principles, Techniques, and Tools. Pearson, Boston, 2nd edition. Google ScholarGoogle Scholar
  2. Frances E. Allen. 1970. Control flow analysis. Proceedings of a Symposium on Compiler Optimization, in SIGPLAN Notices, 5 (7): 1–19. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. Brenda S. Baker. 1977 (January). An algorithm for structuring flowgraphs. Journal of the ACM, 24 (1): 98–120. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Bytecode Alliance. 2022. Cranelift code generator. URL https://github.com/bytecodealliance/wasmtime/tree/main/cranelift. Google ScholarGoogle Scholar
  5. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  6. Martin Erwig. 2001 (September). Inductive graphs and functional graph algorithms. Journal of Functional Programming, 11 (5): 467–492. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. 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 ScholarGoogle Scholar
  8. Matthew S Hecht and Jeffrey D Ullman. 1972. Flow graph reducibility. SIAM Journal on Computing, 1 (2): 188. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. 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 ScholarGoogle Scholar
  10. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  11. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  12. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  13. 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 ScholarGoogle ScholarCross RefCross Ref
  14. 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 ScholarGoogle ScholarCross RefCross Ref
  15. Norman Ramsey. 2022 (June). Beyond relooper: Recursive translation of unstructured control flow to structured control flow. Software artifact archived at Zenodo. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. 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 ScholarGoogle Scholar
  17. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  18. 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 ScholarGoogle Scholar
  19. Lyle Ramshaw. 1988. Eliminating go to’s while preserving program structure. Journal of the ACM, 35 (4): 893–920. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  21. Ben L. Titzer. 2022. A fast in-place interpreter for WebAssembly. PACMPL, 6 (OOPSLA). To appear. Google ScholarGoogle Scholar
  22. 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 ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Beyond Relooper: recursive translation of unstructured control flow to structured control flow (functional pearl)

      Recommendations

      Comments

      Login options

      Check if you have access through your login credentials or your institution to get full access on this article.

      Sign in

      Full Access

      • Article Metrics

        • Downloads (Last 12 months)1,653
        • Downloads (Last 6 weeks)108

        Other Metrics

      PDF Format

      View or Download as a PDF file.

      PDF

      eReader

      View online with eReader.

      eReader
      About Cookies On This Site

      We use cookies to ensure that we give you the best experience on our website.

      Learn more

      Got it!