Skip to content

Rust Code generation#21

Open
loic-fejoz wants to merge 3 commits into
mainfrom
feat/rust-gen
Open

Rust Code generation#21
loic-fejoz wants to merge 3 commits into
mainfrom
feat/rust-gen

Conversation

@loic-fejoz

@loic-fejoz loic-fejoz commented Mar 21, 2026

Copy link
Copy Markdown
Owner

Summary

This PR introduces a Rust Code Generation feature to fsdr-cli, enabling any DSP flowgraph (from csdr commands or .grc files) to be exported as a standalone, Rust source file.

The implementation follows the Futamura projection concept: the flowgraph is partially evaluated, complex parameters (like FIR taps) are pre-computed, and the resulting physical data is baked directly into the generated code as literals. This eliminates runtime overhead for graph construction and parameter resolution.

Key Changes

  • Abstract Backend: Introduced the FsdrBackend trait to decouple semantic flowgraph resolution from physical instantiation.
  • Dual-Backend System:
    • RuntimeBackend: Builds a live FutureSDR flowgraph for immediate CLI execution.
    • CodegenBackend: Emits optimized Rust source code using quote!.
  • fsdr-cli-macros Crate: A new proc-macro sub-crate providing the #[fsdr_instantiate] attribute. This ensures a "One Source of Truth" by automatically
    generating both runtime and codegen versions of a block's constructor.
  • Global Refactor: Migrated all ~40 block converters in src/grc/converter/ to the new generic backend architecture.
  • CLI Integration: Added the --generate flag to both csdr and grc commands.
  • Optimized Baking: Implemented wrappers like CodegenTaps and CodegenPattern to bake complex types into generated source code as standard Rust vectors.

Usage Example

Export an optimized standalone Rust program from a complex pipeline

fsdr-cli csdr --generate src/my_dsp_app.rs convert_u8_f ! shift_addition_cc 0.01 ! fmdemod_quadri_cf ! dump_f

Verification

  • Added integration tests in tests/codegen.rs covering basic and complex flowgraph chains.
  • Verified that generated code for complex FM demodulation pipelines passes cargo check and correctly resolves all internal dependencies.
  • Updated AGENTS.md and agent_docs/ with the new architectural standards.

Experiments the use of a macro to support multiple Futamura projection,
like for Runtime and CodeGeneration.
   - Introduced FsdrBackend trait to decouple flowgraph construction from execution.
   - Created fsdr-cli-macros crate with #[fsdr_instantiate] attribute to ensure a "One Source of Truth" for both runtime and codegen modes.
   - Refactored all ~40 block converters to be generic over the backend, enabling behavioral parity between interpretation and compilation.
   - Implemented CodegenBackend to emit optimized Rust source code with pre-computed data (like filter taps) baked in as literals.
   - Added a --generate <filepath> CLI flag to the csdr and grc commands.
   - Added integration tests in tests/codegen.rs and updated developer documentation.
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