refactor: Extract runtime declarations to data-driven approach #212

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

Problem

crates/compiler/src/codegen.rs (lines 872-936) contains 60+ manual writeln!() calls for LLVM runtime declarations:

writeln!(&mut ir, "declare ptr @patch_seq_write(ptr)")?;
writeln!(&mut ir, "declare ptr @patch_seq_write_line(ptr)")?;
writeln!(&mut ir, "declare ptr @patch_seq_read_line(ptr)")?;
writeln!(&mut ir, "declare ptr @patch_seq_string_concat(ptr)")?;
// ... 60+ more lines

Also emit_runtime_declarations (lines 1298-1543) has ~245 lines of similar boilerplate.

Impact

  • Adding new builtins requires manual LLVM declaration (easy to forget)
  • No validation that declarations match actual runtime signatures
  • 300+ lines of repetitive boilerplate
  • Inconsistencies between declaration and implementation possible

Proposed Solution

Option 1: Declarative table

const RUNTIME_FUNCTIONS: &[RuntimeFn] = &[
    RuntimeFn { name: "patch_seq_write", params: &["ptr"], ret: "ptr" },
    RuntimeFn { name: "patch_seq_write_line", params: &["ptr"], ret: "ptr" },
    RuntimeFn { name: "patch_seq_read_line", params: &["ptr"], ret: "ptr" },
    // ...
];

fn emit_runtime_declarations(ir: &mut String) -> Result<()> {
    for func in RUNTIME_FUNCTIONS {
        writeln!(ir, "declare {} @{}({})", 
            func.ret, 
            func.name, 
            func.params.join(", "))?;
    }
    Ok(())
}

Option 2: Build-time generation from runtime crate

Parse crates/runtime/src/*.rs to extract extern "C" function signatures and generate declarations automatically.

Option 3: Macro-based definition

declare_runtime_functions! {
    patch_seq_write(ptr) -> ptr,
    patch_seq_write_line(ptr) -> ptr,
    patch_seq_read_line(ptr) -> ptr,
    // ...
}

Files Affected

  • crates/compiler/src/codegen.rs (lines 872-936, 1298-1543)
  • Potentially new crates/compiler/src/runtime_decls.rs

Acceptance Criteria

  • Runtime declarations defined in single location
  • Adding new runtime function requires single-line change
  • Declarations validated at compile time if possible
  • Code reduction of at least 200 lines

Labels

refactor, technical-debt, medium-priority

## Problem `crates/compiler/src/codegen.rs` (lines 872-936) contains 60+ manual `writeln!()` calls for LLVM runtime declarations: ```rust writeln!(&mut ir, "declare ptr @patch_seq_write(ptr)")?; writeln!(&mut ir, "declare ptr @patch_seq_write_line(ptr)")?; writeln!(&mut ir, "declare ptr @patch_seq_read_line(ptr)")?; writeln!(&mut ir, "declare ptr @patch_seq_string_concat(ptr)")?; // ... 60+ more lines ``` Also `emit_runtime_declarations` (lines 1298-1543) has ~245 lines of similar boilerplate. ## Impact - Adding new builtins requires manual LLVM declaration (easy to forget) - No validation that declarations match actual runtime signatures - 300+ lines of repetitive boilerplate - Inconsistencies between declaration and implementation possible ## Proposed Solution ### Option 1: Declarative table ```rust const RUNTIME_FUNCTIONS: &[RuntimeFn] = &[ RuntimeFn { name: "patch_seq_write", params: &["ptr"], ret: "ptr" }, RuntimeFn { name: "patch_seq_write_line", params: &["ptr"], ret: "ptr" }, RuntimeFn { name: "patch_seq_read_line", params: &["ptr"], ret: "ptr" }, // ... ]; fn emit_runtime_declarations(ir: &mut String) -> Result<()> { for func in RUNTIME_FUNCTIONS { writeln!(ir, "declare {} @{}({})", func.ret, func.name, func.params.join(", "))?; } Ok(()) } ``` ### Option 2: Build-time generation from runtime crate Parse `crates/runtime/src/*.rs` to extract `extern "C"` function signatures and generate declarations automatically. ### Option 3: Macro-based definition ```rust declare_runtime_functions! { patch_seq_write(ptr) -> ptr, patch_seq_write_line(ptr) -> ptr, patch_seq_read_line(ptr) -> ptr, // ... } ``` ## Files Affected - `crates/compiler/src/codegen.rs` (lines 872-936, 1298-1543) - Potentially new `crates/compiler/src/runtime_decls.rs` ## Acceptance Criteria - [ ] Runtime declarations defined in single location - [ ] Adding new runtime function requires single-line change - [ ] Declarations validated at compile time if possible - [ ] Code reduction of at least 200 lines ## Labels refactor, technical-debt, medium-priority
navicore commented 2026-01-10 03:00:15 +00:00 (Migrated from github.com)
https://github.com/navicore/patch-seq/pull/220
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#212
No description provided.