refactor: Split codegen.rs (6,953 lines, 102 functions) into focused modules #206

Closed
opened 2026-01-09 04:42:39 +00:00 by navicore · 1 comment
navicore commented 2026-01-09 04:42:39 +00:00 (Migrated from github.com)

Problem

crates/compiler/src/codegen.rs is a 6,953-line monolith containing 102 functions. This makes the code difficult to navigate, test, and maintain.

Current State

The file mixes multiple concerns:

  • SSA register allocation
  • LLVM IR generation
  • Inline optimization
  • Symbol management
  • Virtual stack operations
  • Match statement handling
  • Type coercion

Proposed Solution

Split into focused modules under crates/compiler/src/codegen/:

codegen/
├── mod.rs           # Public API, CodegenContext struct
├── ssa.rs           # SSA register allocation (fresh_temp, temp_counter, virtual stack)
├── ir.rs            # LLVM IR text generation (emit_*, writeln! calls)
├── inline.rs        # Inline optimizations (codegen_inline_*)
├── symbols.rs       # Symbol table management
├── statements.rs    # Statement codegen (if, match, while, etc.)
└── builtins.rs      # Builtin word code generation

Specific Functions to Move

To ssa.rs:

  • fresh_temp()
  • fresh_label()
  • Virtual stack operations
  • Register allocation logic

To inline.rs:

  • codegen_inline_binary_op (lines 4079-4190)
  • codegen_inline_unary_op
  • Other codegen_inline_* functions

To statements.rs:

  • codegen_match_statement (lines 5269-5386)
  • codegen_if_statement
  • codegen_while_statement

To ir.rs:

  • emit_runtime_declarations (lines 1298-1543)
  • All writeln! generation logic

Acceptance Criteria

  • No single module exceeds 1,500 lines
  • Each module has a single, clear responsibility
  • All existing tests pass
  • Public API remains unchanged (re-export from mod.rs)

Labels

refactor, technical-debt, high-priority

## Problem `crates/compiler/src/codegen.rs` is a 6,953-line monolith containing 102 functions. This makes the code difficult to navigate, test, and maintain. ## Current State The file mixes multiple concerns: - SSA register allocation - LLVM IR generation - Inline optimization - Symbol management - Virtual stack operations - Match statement handling - Type coercion ## Proposed Solution Split into focused modules under `crates/compiler/src/codegen/`: ``` codegen/ ├── mod.rs # Public API, CodegenContext struct ├── ssa.rs # SSA register allocation (fresh_temp, temp_counter, virtual stack) ├── ir.rs # LLVM IR text generation (emit_*, writeln! calls) ├── inline.rs # Inline optimizations (codegen_inline_*) ├── symbols.rs # Symbol table management ├── statements.rs # Statement codegen (if, match, while, etc.) └── builtins.rs # Builtin word code generation ``` ## Specific Functions to Move ### To `ssa.rs`: - `fresh_temp()` - `fresh_label()` - Virtual stack operations - Register allocation logic ### To `inline.rs`: - `codegen_inline_binary_op` (lines 4079-4190) - `codegen_inline_unary_op` - Other `codegen_inline_*` functions ### To `statements.rs`: - `codegen_match_statement` (lines 5269-5386) - `codegen_if_statement` - `codegen_while_statement` ### To `ir.rs`: - `emit_runtime_declarations` (lines 1298-1543) - All `writeln!` generation logic ## Acceptance Criteria - [ ] No single module exceeds 1,500 lines - [ ] Each module has a single, clear responsibility - [ ] All existing tests pass - [ ] Public API remains unchanged (re-export from mod.rs) ## Labels refactor, technical-debt, high-priority
navicore commented 2026-01-10 22:22:31 +00:00 (Migrated from github.com)
https://github.com/navicore/patch-seq/pull/226
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
navicore/patch-seq#206
No description provided.