Skip to main content

LutBuilder

Struct LutBuilder 

Source
pub struct LutBuilder { /* private fields */ }
Expand description

Builder for generating a function-pointer LUT and handler stubs, with optional grouping of instructions under shared const-generic handlers.

Use this when you want multiple instructions to share one handler function via a const OP: u32 generic parameter. See the crate documentation for the full pattern.

§Example (build.rs)

chipi::LutBuilder::new("cpu.chipi")
    .handler_mod("crate::cpu::interpreter")
    .ctx_type("crate::Cpu")
    .lut_mod("crate::cpu::lut")
    .group("alu", ["addi", "addis", "ori", "oris"])
    .group("mem", ["lwz", "stw", "lbz", "stb"])
    .build_lut(out_dir.join("cpu_lut.rs").to_str().unwrap())?;

Implementations§

Source§

impl LutBuilder

Source

pub fn new(input: impl Into<String>) -> Self

Create a new builder targeting the given .chipi spec file.

Source

pub fn handler_mod(self, m: impl Into<String>) -> Self

Set the Rust module path where handler functions live (e.g. "crate::cpu::interpreter").

Source

pub fn ctx_type(self, t: impl Into<String>) -> Self

Set the mutable context type passed to every handler (e.g. "crate::Cpu").

Source

pub fn lut_mod(self, path: impl Into<String>) -> Self

Set the Rust module path where the generated OP_* constants live (e.g. "crate::cpu::lut"). Required when using groups so that stubs can use {lut_mod}::* to import the constants.

Source

pub fn instr_type(self, t: impl Into<String>) -> Self

Override the type of the second parameter of every handler function.

Defaults to u32 (raw opcode word). Set to a wrapper type such as "crate::cpu::semantics::Instruction" to have handlers receive a richer type instead. You must also call Self::raw_expr to tell chipi how to extract the underlying u32 for table indexing.

Source

pub fn raw_expr(self, expr: impl Into<String>) -> Self

Expression that yields a u32 from the instr local inside a generated dispatch function. Only meaningful when Self::instr_type is set.

For a newtype struct Instruction(pub u32) this is "instr.0" (the default when instr_type is set). For a struct with a raw() method use "instr.raw()".

Source

pub fn dispatch(self, strategy: Dispatch) -> Self

Set the dispatch strategy.

  • Dispatch::FnPtrLut (default): static [Handler; N] arrays with indirect calls. Each tree level gets its own table.
  • Dispatch::JumpTable: a single #[inline(always)] function with nested match statements. The compiler can inline handler calls for zero-overhead dispatch when handlers are also #[inline(always)].
Source

pub fn group( self, name: impl Into<String>, instrs: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Register a group: name is the shared handler function name (e.g. "alu"), instrs lists the instruction names that route to it.

Each instruction in instrs will appear in the LUT as handler_mod::alu::<{ OP_INSTR }> instead of handler_mod::instr. The generated stub is pub fn alu<const OP: u32>(...) with a match OP body.

Source

pub fn from_config(target: &LutTarget) -> Self

Create a LutBuilder from a config::LutTarget.

Source

pub fn run_target(target: &LutTarget) -> Result<(), Box<dyn Error>>

Run all outputs defined in a config::LutTarget.

Generates the LUT file, and optionally the instruction type and stubs if configured. Stubs are only generated if the target file does not exist.

Source

pub fn build_lut(&self, output: &str) -> Result<(), Box<dyn Error>>

Generate the LUT source file.

Source

pub fn build_instr_type(&self, output: &str) -> Result<(), Box<dyn Error>>

Generate an instruction newtype with field accessor methods.

Collects all unique fields from the spec and generates a pub struct Name(pub u32) with one #[inline] accessor per field.

The struct name is derived from the last path segment of .instr_type() (e.g., "crate::cpu::Instruction" -> "Instruction"), or defaults to "Instruction" if .instr_type() was not called.

Fields with conflicting definitions across instructions generate separate accessors with bit range suffixes (e.g., d_15_0 and d_11_0).

§Example
chipi::LutBuilder::new("cpu.chipi")
    .instr_type("crate::cpu::Instruction")
    .build_instr_type(out_dir.join("instruction.rs").to_str().unwrap())?;

Trait Implementations§

Source§

impl Default for LutBuilder

Source§

fn default() -> LutBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.