Control Flow Graph to JS Converter
Convert Control Flow Graphs (CFG) back into executable JavaScript. Visualize nodes, debug logic, and generate state-machine code for analysis.
About
This tool is a specialized compiler that transforms a Control Flow Graph (CFG) structure into executable JavaScript code. In software engineering and compiler design, a CFG is a representation, using graph notation, of all paths that might be traversed through a program during its execution. Reconstructing high-level source code from a flat graph is a complex challenge often encountered in reverse engineering, decompilation, and code obfuscation analysis.
The converter utilizes the State Machine Pattern (also known as Control Flow Flattening) to linearize the graph. Instead of attempting to reconstruct complex nested loops (like while or for), which requires advanced Dominance analysis, this tool wraps the logic in a master driver loop. The program state S transitions between block labels, ensuring that even the most spaghetti-like graphs are converted into valid, deterministic JavaScript.
Formulas
The core logic converts the graph connectivity into a state transition function. If we define the set of blocks as B and the transition function as δ, the execution flow is modeled as:
Where S is the current block label and I is the runtime input/condition. The generated JavaScript implements this as:
Reference Data
| Term | Definition | Visual Representation |
|---|---|---|
| Basic Block | A straight-line code sequence with no branches in except to the entry and no branches out except at the exit. | Node (Rectangle) |
| Control Flow Edge | A path from one block to another, potentially representing a jump or a conditional branch. | Arrow (→) |
| Entry Point | The starting node where execution begins. | START |
| State Machine | A design pattern where execution is controlled by a state variable inside a loop. | while(true) |
| Cyclomatic Complexity | A quantitative measure of the number of linearly independent paths through a program's source code. | M = E − N + 2P |