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:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user