* Refactor the structure and responsibilities of `CodeGenContext` This commit refactors how the `CodeGenContext` is used throughout the code generation process, making it easier to pass it around when more flexibility is desired in the MacroAssembler to perform the lowering of certain instructions. As of this change, the responsibility of the `CodeGenContext` is to provide an interface for operations that require an orchestration between the register allocator, the value stack and function's frame. The MacroAssembler is removed from the CodeGenContext as is passed as a dependency where needed, effectly using it as an independent code generation interface only. By giving more responsibilities to the `CodeGenContext` we can clearly separate the concerns of the register allocator, which previously did more than it should (e.g. popping values and spilling). This change ultimately allows passing in the `CodeGenContext` to the `MacroAssembler` when a given instruction cannot be generically described through a common interface. Allowing each implementation to decide the best way to lower a particular instruction. * winch: Add support for the WebAssembly `<i32|i64>.div_*` instructions Given that some architectures have very specific requirements on how to handle division, this change uses `CodeGenContext` as a dependency to the `div` MacroAssembler instruction to ensure that each implementation can decide on how to lower the division. This approach also allows -- in architectures where division can be expressed as an ordinary binary operation -- to rely on the `CodeGenContext::i32_binop` or `CodeGenContext::i64_binop` helpers.
57 lines
1.7 KiB
Rust
57 lines
1.7 KiB
Rust
use crate::{isa::reg::Reg, regset::RegSet};
|
|
|
|
/// The register allocator.
|
|
///
|
|
/// The register allocator uses a single-pass algorithm;
|
|
/// its implementation uses a bitset as a freelist
|
|
/// to track per-class register availability.
|
|
///
|
|
/// If a particular register is not available upon request
|
|
/// the register allocation will perform a "spill", essentially
|
|
/// moving Local and Register values in the stack to memory.
|
|
/// This processs ensures that whenever a register is requested,
|
|
/// it is going to be available.
|
|
pub(crate) struct RegAlloc {
|
|
pub scratch: Reg,
|
|
regset: RegSet,
|
|
}
|
|
|
|
impl RegAlloc {
|
|
/// Create a new register allocator
|
|
/// from a register set.
|
|
pub fn new(regset: RegSet, scratch: Reg) -> Self {
|
|
Self { regset, scratch }
|
|
}
|
|
|
|
/// Allocate the next available general purpose register,
|
|
/// spilling if none available.
|
|
pub fn any_gpr<F>(&mut self, spill: &mut F) -> Reg
|
|
where
|
|
F: FnMut(&mut RegAlloc),
|
|
{
|
|
self.regset.any_gpr().unwrap_or_else(|| {
|
|
spill(self);
|
|
self.regset.any_gpr().expect("any gpr to be available")
|
|
})
|
|
}
|
|
|
|
/// Request a specific general purpose register,
|
|
/// spilling if not available.
|
|
pub fn gpr<F>(&mut self, named: Reg, spill: &mut F) -> Reg
|
|
where
|
|
F: FnMut(&mut RegAlloc),
|
|
{
|
|
self.regset.gpr(named).unwrap_or_else(|| {
|
|
spill(self);
|
|
self.regset
|
|
.gpr(named)
|
|
.expect(&format!("gpr {:?} to be available", named))
|
|
})
|
|
}
|
|
|
|
/// Mark a particular general purpose register as available.
|
|
pub fn free_gpr(&mut self, reg: Reg) {
|
|
self.regset.free_gpr(reg);
|
|
}
|
|
}
|