Skip to content

Add initial control-flow graph model and Prism builder - #959

Open
Morriar wants to merge 1 commit into
mainfrom
cfg-model
Open

Add initial control-flow graph model and Prism builder#959
Morriar wants to merge 1 commit into
mainfrom
cfg-model

Conversation

@Morriar

@Morriar Morriar commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Why

Rubydex needs a control-flow graph before it can model instruction semantics, local-variable flow, reachability, and type inference.

This PR introduces the minimum CFG topology needed to start that work. The representation follows Sorbet’s non-SSA CFG structure but does not attempt full Sorbet compatibility yet.

What changed

Added a CFG model with:

  • Stable BlockIds
  • Dedicated entry and exit blocks
  • Ordered instructions within each BasicBlock
  • Jump, Branch, and Exit terminators
  • Source locations for instructions and control-flow transfers
  • Successor traversal and block lookup APIs

Added a Prism visitor that builds CFGs for:

  • Sequential statements
  • if and unless
  • while and until
  • return
  • Parenthesized expressions
  • Nested branching predicates

The builder preserves Ruby evaluation order and creates explicit branch, merge, loop-header, loop-exit, and unreachable blocks. These structural blocks may be empty; a future CFG simplification pass can remove unnecessary forwarding blocks.

Instructions currently contain only source locations. Instruction semantics, locals, block arguments, and type information will be introduced separately.

Testing

Builder tests render the complete CFG into a canonical textual representation. Each test verifies:

  • Block allocation
  • Instruction ordering
  • True and false successors
  • Merge edges
  • Loop backedges
  • Return-to-exit edges
  • Nested control flow

Follow-up work

This PR intentionally does not include:

  • Instruction lowering
  • Local variables or temporary values
  • break, next, redo, or retry
  • Exception, rescue, or ensure control flow
  • Method arguments or default arguments
  • CFG simplification
  • Sorbet fixture compatibility tests

@Morriar
Morriar requested a review from a team as a code owner July 28, 2026 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant