Skip to main content

RegisterAllocator

Trait RegisterAllocator 

Source
pub trait RegisterAllocator {
    type PhysReg: PhysRegConvertible;

    // Required methods
    fn alloc_scratch(&mut self) -> Option<Self::PhysReg>;
    fn free_scratch(&mut self, phys: Self::PhysReg);
    fn get_mapping(&self, vreg: &VirtualReg) -> Option<Self::PhysReg>;
    fn ensure_mapping(&mut self, vreg: VirtualReg) -> Option<Self::PhysReg>;
    fn mapped_for_register(&self, reg: &Register) -> Option<Self::PhysReg>;
    fn occupy(&mut self, phys: Self::PhysReg);
    fn release(&mut self, phys: Self::PhysReg);
    fn is_occupied(&self, phys: Self::PhysReg) -> bool;
}
Expand description

Target-facing interface for MIR register allocation.

The trait stays purposefully small: code generators typically need a lightweight scratch register pool, a stable mapping from virtual to physical registers, and explicit hooks to reserve or release physical registers that are pre-coloured by the ABI. Architecture backends can build richer policies on top of this contract without forcing every target to adopt the same strategy.

Required Associated Types§

Source

type PhysReg: PhysRegConvertible

Architecture-specific physical register handle.

Required Methods§

Source

fn alloc_scratch(&mut self) -> Option<Self::PhysReg>

Acquire a short-lived scratch register. Returns None when the dedicated pool is exhausted so the caller may spill or choose an alternate path.

Source

fn free_scratch(&mut self, phys: Self::PhysReg)

Release a scratch register obtained through RegisterAllocator::alloc_scratch.

Source

fn get_mapping(&self, vreg: &VirtualReg) -> Option<Self::PhysReg>

Look up the physical register currently assigned to the virtual register, when available.

Source

fn ensure_mapping(&mut self, vreg: VirtualReg) -> Option<Self::PhysReg>

Ensure that the virtual register has a permanent mapping. Implementers can reject unsupported register classes by returning None, signalling that the caller should spill.

Source

fn mapped_for_register(&self, reg: &Register) -> Option<Self::PhysReg>

Resolve the backing physical register for an arbitrary MIR register (virtual or physical).

Source

fn occupy(&mut self, phys: Self::PhysReg)

Mark a physical register as occupied, removing it from the allocator’s free pool if necessary.

Source

fn release(&mut self, phys: Self::PhysReg)

Release a previously occupied physical register back to the pool.

Source

fn is_occupied(&self, phys: Self::PhysReg) -> bool

Test whether the allocator currently treats the physical register as occupied.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§