Automatically Deriving Control-Flow Graph Generators from Operational Semantics

James Koppel, Jackson Kearl, Armando Solar-Lezama · DOI

Abstract

We develop the first theory of control-flow graphs from first principles and use it to create an algorithm for automatically synthesizing many variants of control-flow graph generators from a language's operational semantics. Our approach introduces a new algorithm for converting a large class of small-step operational semantics to an abstract machine. It then uses "abstract rewriting" to automatically abstract the semantics, generating both program-specific CFGs and standalone generator code. We demonstrate that by varying abstraction and projection parameters, our approach synthesizes several families of CFG generators suitable for different tools. We prove the correspondence between generated graphs and original semantics, and provide an algorithm for proving the termination of interpreted-mode generators. Implemented in a tool called Mandate, our algorithm produces human-readable code for medium-sized languages like Tiger and MITScript, supporting two static analyzers built atop the generated graphs.

Introduction

Control-flow graphs (CFGs) are the backbone of modern compilers and static analysis tools. Despite their ubiquity, CFG generators are typically built manually—a process that is tedious, error-prone, and requires re-implementing much of a language's execution logic. Furthermore, there is no single "correct" CFG; different applications require different levels of granularity, from compact basic-block representations for optimization to path-sensitive views for safety analysis.

This paper presents Mandate, a framework that automatically derives CFG generators from a language’s formal small-step operational semantics (SOS). By defining CFGs as "projections of abstracted abstract machine states," the authors provide a principled theoretical foundation for CFG generation, enabling the automatic synthesis of diverse graph variants.

The Mandate Framework

The core of Mandate lies in bridging the gap between high-level operational semantics and the graph-based structures needed for analysis.

From Semantics to Abstract Machines

The process begins by mechanically converting SOS rules into an intermediate Phased Abstract Machine (PAM). This machine explicitly models the "down" phases (recursive sub-expression evaluation) and "up" phases (returning values) inherent in language execution. This conversion ensures that the resulting graph generator is sound with respect to the language's formal definition.

Abstract Rewriting and Projection

To ensure that the generated graphs are finite and useful, Mandate employs "abstract rewriting." By replacing concrete values with abstract symbols (e.g., $\star$), the system can explore all possible execution paths simultaneously. The generation process is controlled by two parameters:

  • Abstraction ($\alpha$): Defines which details to ignore (e.g., treating all integers as an abstract "number" type).
  • Projection ($\pi$): Defines which internal machine transitions to hide, allowing the user to collapse complex semantic steps into familiar, high-level graph nodes.
Figure (a): A standard control-flow graph generated by Mandate for a program that checks a boolean condition twice.

Key Insights: CFGs as Projections

The paper's primary theoretical contribution is the formalization: “A CFG is a projection of the transition graph of abstracted abstract machine states.” This turns CFG generation into a precise mathematical transformation.

A significant advantage of this approach is the ability to generate path-sensitive graphs. By adjusting the abstraction parameter to retain certain boolean values, Mandate can produce graphs that track state across branches, which is essential for sophisticated static analyses like parenthesis balancing or null-pointer detection.

Figure (c): A path-sensitive CFG variant where the state of the boolean variable 'b' is tracked along separate execution paths, enabling more precise analysis.

Implementation and Evaluation

The authors implemented Mandate and evaluated it on three languages: IMP (a toy imperative language), Tiger (a medium-sized Algol-family language with 80 rules), and MITScript (a JavaScript-like language with 60 rules).

Mandate supports two modes:

  1. Interpreted Mode: Directly generates a CFG for a specific input program.
  2. Compiled Mode: Analyzes the language's semantic rules to output a standalone, human-readable CFG generator in Haskell.

The generated code is remarkably concise; for instance, the Tiger for loop generator is only 12 lines of Haskell. To validate the utility of these graphs, the authors built a constant propagator and a path-sensitive parenthesis balancer, both requiring fewer than 150 lines of code.

Conclusion

Mandate represents a significant step toward the "grand vision" of automatically synthesizing all programming tools from a single semantic specification. While currently limited to deterministic languages and simplified heap models, the framework provides a rigorous and automated alternative to the manual construction of CFG generators. Future work includes extending the theory to handle non-determinism and more complex data structures.

Summary updated

BibTeX
@article{koppel2020automatically,
  author = {James Koppel and Jackson Kearl and Armando Solar-Lezama},
  title = {{Automatically Deriving Control-Flow Graph Generators from Operational Semantics}},
  journal = {Proc. ACM Program. Lang.},
  year = {2020},
  doi = {10.1145/3547648},
}
Figure