User Rating 0.0
Total Usage 0 times
Input: Control Flow Graph (JSON)
Invalid JSON
Visualizer (Drag Nodes) 0 Nodes
Output: JavaScript
Is this tool helpful?

Your feedback helps us improve.

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.

cfg javascript compiler debugging visualization

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:

St+1 = δ(St, I)

Where S is the current block label and I is the runtime input/condition. The generated JavaScript implements this as:

while(running) {
case Bi: Execute Code;SBnext; break;

Reference Data

TermDefinitionVisual Representation
Basic BlockA straight-line code sequence with no branches in except to the entry and no branches out except at the exit.Node (Rectangle)
Control Flow EdgeA path from one block to another, potentially representing a jump or a conditional branch.Arrow (→)
Entry PointThe starting node where execution begins.START
State MachineA design pattern where execution is controlled by a state variable inside a loop.while(true)
Cyclomatic ComplexityA quantitative measure of the number of linearly independent paths through a program's source code.M = EN + 2P

Frequently Asked Questions

This is called "Control Flow Flattening". It is the most robust way to represent an arbitrary graph in linear text without analyzing the nesting structure of loops. It guarantees that any graph, even those with "irreducible" loops (spaghetti code), produces valid JavaScript.
This tool focuses on code-defined graphs. You can edit the JSON configuration on the left, and the visualizer on the right will update in real-time. You can, however, drag nodes in the visualizer to rearrange the layout.
The generated JavaScript will also contain an infinite loop. The "State Machine" faithfully reproduces the logic of the graph. Ensure your graph has a terminal node or a break condition if you intend to run the code.
Yes. This transformation is similar to how obfuscators flatten control flow to make code harder for humans to read, as it destroys the visual structure of the original algorithm.