Add a convenience function for displaying a BlockCall (#5677)

Add a display method to BlockCall that returns a std::fmt::Displayable result. Rework the display code in the write module of cranelift-codegen to use this method instead.
This commit is contained in:
Trevor Elliott
2023-01-31 14:26:10 -08:00
committed by GitHub
parent 253e28ca4f
commit e82995f03c
2 changed files with 32 additions and 15 deletions

View File

@@ -118,6 +118,35 @@ impl BlockCall {
{
self.values.extend(elements, pool)
}
/// Return a value that can display this block call.
pub fn display<'a>(&self, pool: &'a ValueListPool) -> DisplayBlockCall<'a> {
DisplayBlockCall { block: *self, pool }
}
}
/// Wrapper for the context needed to display a [BlockCall] value.
pub struct DisplayBlockCall<'a> {
block: BlockCall,
pool: &'a ValueListPool,
}
impl<'a> Display for DisplayBlockCall<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.block.block(&self.pool))?;
let args = self.block.args_slice(&self.pool);
if !args.is_empty() {
write!(f, "(")?;
for (ix, arg) in args.iter().enumerate() {
if ix > 0 {
write!(f, ", ")?;
}
write!(f, "{}", arg)?;
}
write!(f, ")")?;
}
Ok(())
}
}
// Include code generated by `cranelift-codegen/meta/src/gen_inst.rs`. This file contains: