# hngocuyen/pycrune architecture

> Architecture diagram of the GitHub repository hngocuyen/pycrune, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hngocuyen/pycrune
- Repository: https://github.com/hngocuyen/pycrune

## Overview

PycRune is a CPython bytecode decompiler whose main workflow is to load a `.pyc`, reconstruct Python source, and optionally verify the result. The CLI coordinates loading, structural decompilation, and emission; CFG, dominance, loop, and exception-scope analysis support reconstruction. Verification recompiles source with a matching CPython and checks behavior and recursive bytecode identity. The README documents batch decompilation and CFG inspection as additional CLI capabilities. The large structural decompiler was not sampled, so its internal calls and finer stages are not asserted here.

Main entry points: [`Program.cs`](https://github.com/hngocuyen/pycrune/blob/main/Program.cs) (CLI commands), [`CFG/ControlFlowGraph.cs`](https://github.com/hngocuyen/pycrune/blob/main/CFG/ControlFlowGraph.cs) (Control-flow graph), [`Decompiler/StructuralDecompiler.cs`](https://github.com/hngocuyen/pycrune/blob/main/Decompiler/StructuralDecompiler.cs) (Structural decompiler), [`Core/PyBridge.cs`](https://github.com/hngocuyen/pycrune/blob/main/Core/PyBridge.cs) (Python bridge)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingest["Input and bridge"]
  node_cli["CLI commands<br/>[Program.cs]"]
  node_locator["Python locator<br/>[PythonLocator.cs]"]
  node_bridge["Python bridge<br/>[PyBridge.cs]"]
  node_code["Code model"]
end

subgraph group_analysis["Bytecode analysis"]
  node_cfg["Control-flow graph"]
  node_blocks["Basic blocks<br/>[BasicBlock.cs]"]
  node_dominators["Dominance analysis<br/>[DominatorTree.cs]"]
  node_loops["Loop analysis<br/>[LoopAnalysis.cs]"]
  node_exceptions["Exception scopes<br/>[ExceptionScope.cs]"]
end

subgraph group_emit["Source reconstruction"]
  node_decompiler["Structural decompiler"]
  node_cleanup["Synthetic cleanup"]
  node_stack["Symbolic stack<br/>[SymbolicStack.cs]"]
  node_ast["Python AST<br/>[AstNodes.cs]"]
  node_emitter["Python emitter<br/>[PythonEmitter.cs]"]
end

subgraph group_verify["Verification"]
  node_verifier["Execution verifier"]
  node_comparer["Bytecode comparer"]
end

node_user(("CLI user"))
node_pyc["PYC input"]
node_source["Python source"]
node_cpython["Matching CPython"]

node_user -->|"invokes"| node_cli
node_cli -->|"loads input"| node_bridge
node_bridge -->|"resolves interpreter"| node_locator
node_bridge -->|"requests bytecode"| node_cpython
node_bridge -->|"builds model"| node_code
node_pyc -->|"supplies file"| node_cli
node_cli -->|"decompiles"| node_decompiler
node_decompiler -.->|"uses control flow"| node_cfg
node_cfg -->|"builds blocks"| node_blocks
node_cfg -->|"reads instructions"| node_code
node_dominators -->|"analyzes graph"| node_cfg
node_loops -->|"analyzes graph"| node_cfg
node_loops -->|"uses dominance"| node_dominators
node_exceptions -->|"analyzes scopes"| node_cfg
node_exceptions -->|"uses dominance"| node_dominators
node_decompiler -.->|"applies cleanup"| node_cleanup
node_decompiler -.->|"simulates stack"| node_stack
node_decompiler -.->|"produces statements"| node_ast
node_cli -->|"emits source"| node_emitter
node_emitter -->|"writes"| node_source
node_cli -->|"checks execution"| node_verifier
node_cli -->|"compares bytecode"| node_comparer
node_verifier -->|"runs programs"| node_cpython
node_comparer -->|"compiles source"| node_bridge
node_source -.->|"provides output"| node_cli

click node_cli "https://github.com/hngocuyen/pycrune/blob/main/Program.cs"
click node_locator "https://github.com/hngocuyen/pycrune/blob/main/Core/PythonLocator.cs"
click node_bridge "https://github.com/hngocuyen/pycrune/blob/main/Core/PyBridge.cs"
click node_code "https://github.com/hngocuyen/pycrune/blob/main/Model/PythonCodeObject.cs"
click node_cfg "https://github.com/hngocuyen/pycrune/blob/main/CFG/ControlFlowGraph.cs"
click node_blocks "https://github.com/hngocuyen/pycrune/blob/main/CFG/BasicBlock.cs"
click node_dominators "https://github.com/hngocuyen/pycrune/blob/main/CFG/DominatorTree.cs"
click node_loops "https://github.com/hngocuyen/pycrune/blob/main/CFG/LoopAnalysis.cs"
click node_exceptions "https://github.com/hngocuyen/pycrune/blob/main/CFG/ExceptionScope.cs"
click node_decompiler "https://github.com/hngocuyen/pycrune/blob/main/Decompiler/StructuralDecompiler.cs"
click node_cleanup "https://github.com/hngocuyen/pycrune/blob/main/Analysis/SyntheticCleanupPass.cs"
click node_stack "https://github.com/hngocuyen/pycrune/blob/main/Decompiler/SymbolicStack.cs"
click node_ast "https://github.com/hngocuyen/pycrune/blob/main/AST/AstNodes.cs"
click node_emitter "https://github.com/hngocuyen/pycrune/blob/main/Decompiler/PythonEmitter.cs"
click node_verifier "https://github.com/hngocuyen/pycrune/blob/main/Verification/ExecutionVerifier.cs"
click node_comparer "https://github.com/hngocuyen/pycrune/blob/main/Verification/BytecodeComparer.cs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_locator,node_bridge,node_code,node_user toneBlue
class node_cfg,node_blocks,node_dominators,node_loops,node_exceptions toneAmber
class node_decompiler,node_cleanup,node_stack,node_ast,node_emitter toneMint
class node_verifier,node_comparer toneRose
class node_pyc,node_source,node_cpython toneIndigo
```

## Components

### Input and bridge

- [CLI commands](https://github.com/hngocuyen/pycrune/blob/main/Program.cs): `Program.cs`
- [Python locator](https://github.com/hngocuyen/pycrune/blob/main/Core/PythonLocator.cs): `Core/PythonLocator.cs`
- [Python bridge](https://github.com/hngocuyen/pycrune/blob/main/Core/PyBridge.cs): `Core/PyBridge.cs`
- [Code model](https://github.com/hngocuyen/pycrune/blob/main/Model/PythonCodeObject.cs): `Model/PythonCodeObject.cs`

### Bytecode analysis

- [Control-flow graph](https://github.com/hngocuyen/pycrune/blob/main/CFG/ControlFlowGraph.cs): `CFG/ControlFlowGraph.cs`
- [Basic blocks](https://github.com/hngocuyen/pycrune/blob/main/CFG/BasicBlock.cs): `CFG/BasicBlock.cs`
- [Dominance analysis](https://github.com/hngocuyen/pycrune/blob/main/CFG/DominatorTree.cs): `CFG/DominatorTree.cs`
- [Loop analysis](https://github.com/hngocuyen/pycrune/blob/main/CFG/LoopAnalysis.cs): `CFG/LoopAnalysis.cs`
- [Exception scopes](https://github.com/hngocuyen/pycrune/blob/main/CFG/ExceptionScope.cs): `CFG/ExceptionScope.cs`

### Source reconstruction

- [Structural decompiler](https://github.com/hngocuyen/pycrune/blob/main/Decompiler/StructuralDecompiler.cs): `Decompiler/StructuralDecompiler.cs`
- [Synthetic cleanup](https://github.com/hngocuyen/pycrune/blob/main/Analysis/SyntheticCleanupPass.cs): `Analysis/SyntheticCleanupPass.cs`
- [Symbolic stack](https://github.com/hngocuyen/pycrune/blob/main/Decompiler/SymbolicStack.cs): `Decompiler/SymbolicStack.cs`
- [Python AST](https://github.com/hngocuyen/pycrune/blob/main/AST/AstNodes.cs): `AST/AstNodes.cs`
- [Python emitter](https://github.com/hngocuyen/pycrune/blob/main/Decompiler/PythonEmitter.cs): `Decompiler/PythonEmitter.cs`

### Verification

- [Execution verifier](https://github.com/hngocuyen/pycrune/blob/main/Verification/ExecutionVerifier.cs): `Verification/ExecutionVerifier.cs`
- [Bytecode comparer](https://github.com/hngocuyen/pycrune/blob/main/Verification/BytecodeComparer.cs): `Verification/BytecodeComparer.cs`

### Other components

- **CLI user**
- **PYC input**
- **Python source**
- **Matching CPython**

## Connections

- CLI user → CLI commands: invokes (evidence: `Program.cs`)
- CLI commands → Python bridge: loads input (evidence: `Program.cs`)
- Python bridge → Python locator: resolves interpreter (evidence: `Core/PyBridge.cs`)
- Python bridge → Matching CPython: requests bytecode (evidence: `Core/PyBridge.cs`)
- Python bridge → Code model: builds model (evidence: `Core/PyBridge.cs`)
- PYC input → CLI commands: supplies file (evidence: `README.md`)
- CLI commands → Structural decompiler: decompiles (evidence: `Program.cs`)
- Structural decompiler → Control-flow graph: uses control flow
- Control-flow graph → Basic blocks: builds blocks (evidence: `CFG/ControlFlowGraph.cs`)
- Control-flow graph → Code model: reads instructions (evidence: `CFG/ControlFlowGraph.cs`)
- Dominance analysis → Control-flow graph: analyzes graph (evidence: `CFG/DominatorTree.cs`)
- Loop analysis → Control-flow graph: analyzes graph (evidence: `CFG/LoopAnalysis.cs`)
- Loop analysis → Dominance analysis: uses dominance (evidence: `CFG/LoopAnalysis.cs`)
- Exception scopes → Control-flow graph: analyzes scopes (evidence: `CFG/ExceptionScope.cs`)
- Exception scopes → Dominance analysis: uses dominance (evidence: `CFG/ExceptionScope.cs`)
- Structural decompiler → Synthetic cleanup: applies cleanup
- Structural decompiler → Symbolic stack: simulates stack
- Structural decompiler → Python AST: produces statements
- CLI commands → Python emitter: emits source (evidence: `Program.cs`)
- Python emitter → Python source: writes (evidence: `Decompiler/PythonEmitter.cs`)
- CLI commands → Execution verifier: checks execution (evidence: `Program.cs`)
- CLI commands → Bytecode comparer: compares bytecode (evidence: `Program.cs`)
- Execution verifier → Matching CPython: runs programs (evidence: `README.md`)
- Bytecode comparer → Python bridge: compiles source (evidence: `Verification/BytecodeComparer.cs`)
- Python source → CLI commands: provides output
