diff --git a/crates/codegen/src/loop_analysis.rs b/crates/codegen/src/loop_analysis.rs index d4b4b687..ea525a37 100644 --- a/crates/codegen/src/loop_analysis.rs +++ b/crates/codegen/src/loop_analysis.rs @@ -203,7 +203,7 @@ impl<'a, 'b> BlocksInLoopPostOrder<'a, 'b> { } } -impl<'a, 'b> Iterator for BlocksInLoopPostOrder<'a, 'b> { +impl Iterator for BlocksInLoopPostOrder<'_, '_> { type Item = BlockId; fn next(&mut self) -> Option { diff --git a/crates/codegen/src/optim/sccp.rs b/crates/codegen/src/optim/sccp.rs index 575225d4..64754c2e 100644 --- a/crates/codegen/src/optim/sccp.rs +++ b/crates/codegen/src/optim/sccp.rs @@ -481,7 +481,7 @@ impl<'a, 'i> CellState<'a, 'i> { } } -impl<'a, 'i> State for CellState<'a, 'i> { +impl State for CellState<'_, '_> { fn lookup_val(&mut self, value: ValueId) -> EvalValue { self.used_val.insert(value); diff --git a/crates/ir/src/cfg.rs b/crates/ir/src/cfg.rs index 7d4451c8..60a4cc1a 100644 --- a/crates/ir/src/cfg.rs +++ b/crates/ir/src/cfg.rs @@ -159,7 +159,7 @@ impl<'a> CfgPostOrder<'a> { } } -impl<'a> Iterator for CfgPostOrder<'a> { +impl Iterator for CfgPostOrder<'_> { type Item = BlockId; fn next(&mut self) -> Option { diff --git a/crates/ir/src/dfg.rs b/crates/ir/src/dfg.rs index a83389bb..2a009bb3 100644 --- a/crates/ir/src/dfg.rs +++ b/crates/ir/src/dfg.rs @@ -10,7 +10,7 @@ use crate::{ control_flow::{self, Branch, Jump, Phi}, InstId, SideEffect, }, - ir_writer::{FuncWriteCtx, WriteWithFunc}, + ir_writer::{FuncWriteCtx, IrWrite}, module::ModuleCtx, GlobalVariable, Inst, InstDowncast, InstDowncastMut, InstSetBase, }; @@ -301,8 +301,11 @@ impl DataFlowGraph { pub struct BlockId(pub u32); entity_impl!(BlockId, "block"); -impl WriteWithFunc for BlockId { - fn write(&self, _ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()> { +impl<'a> IrWrite> for BlockId { + fn write(&self, w: &mut W, _ctx: &FuncWriteCtx<'a>) -> io::Result<()> + where + W: io::Write + ?Sized, + { write!(w, "block{}", self.0) } } diff --git a/crates/ir/src/function.rs b/crates/ir/src/function.rs index b8ef1bf9..6c5f4ba8 100644 --- a/crates/ir/src/function.rs +++ b/crates/ir/src/function.rs @@ -3,11 +3,7 @@ use std::io; use smallvec::SmallVec; use super::{DataFlowGraph, Layout, Type, ValueId}; -use crate::{ - ir_writer::{FuncWriteCtx, WriteWithFunc, WriteWithModule}, - module::ModuleCtx, - InstSetBase, Linkage, -}; +use crate::{ir_writer::IrWrite, module::ModuleCtx, InstSetBase, Linkage}; pub struct Function { /// Signature of the function. @@ -97,27 +93,19 @@ impl Signature { } } -impl WriteWithFunc for Signature { - fn write(&self, ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()> { - let Signature { - name, - linkage, - args, - ret_ty, - } = self; - - write!(w, "func {linkage} %{name}(")?; - let mut args = args.iter(); - if let Some(arg) = args.next() { - arg.write(ctx.module_ctx(), &mut *w)?; - }; - - for arg in args { - write!(w, " ")?; - arg.write(ctx.module_ctx(), &mut *w)?; - } +impl IrWrite for Signature +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + write!(w, "func ")?; + self.linkage.write(w, ctx)?; + write!(w, " %{}(", self.name)?; + self.args.write_with_delim(w, " ", ctx)?; write!(w, ") -> ")?; - - ret_ty.write(ctx.module_ctx(), &mut *w) + self.ret_ty.write(w, ctx) } } diff --git a/crates/ir/src/global_variable.rs b/crates/ir/src/global_variable.rs index 858a57ea..874089fd 100644 --- a/crates/ir/src/global_variable.rs +++ b/crates/ir/src/global_variable.rs @@ -1,9 +1,9 @@ -use std::{fmt, io}; +use std::io; use cranelift_entity::PrimaryMap; use rustc_hash::FxHashMap; -use crate::{ir_writer::WriteWithModule, module::ModuleCtx, Immediate, Linkage, Type}; +use crate::{ir_writer::IrWrite, module::ModuleCtx, Immediate, Linkage, Type}; #[derive(Debug, Default)] pub struct GlobalVariableStore { @@ -34,7 +34,7 @@ impl GlobalVariableStore { } pub fn init_data(&self, gv: GlobalVariable) -> Option<&GvInitializer> { - self.gv_data[gv].data.as_ref() + self.gv_data[gv].initializer.as_ref() } pub fn is_const(&self, gv: GlobalVariable) -> bool { @@ -68,10 +68,16 @@ impl GlobalVariable { module.with_gv_store(|s| s.ty(self)) } } - -impl WriteWithModule for GlobalVariable { - fn write(&self, module: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()> { - module.with_gv_store(|s| s.gv_data(*self).write(module, w)) +impl IrWrite for GlobalVariable +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + ctx.as_ref() + .with_gv_store(|s| s.gv_data(*self).write(w, ctx)) } } @@ -81,7 +87,7 @@ pub struct GlobalVariableData { pub ty: Type, pub linkage: Linkage, pub is_const: bool, - pub data: Option, + pub initializer: Option, } impl GlobalVariableData { @@ -97,7 +103,7 @@ impl GlobalVariableData { ty, linkage, is_const, - data, + initializer: data, } } @@ -107,22 +113,31 @@ impl GlobalVariableData { ty, linkage, is_const: true, - data: Some(data), + initializer: Some(data), } } } -impl WriteWithModule for GlobalVariableData { - fn write(&self, module: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()> { - write!(w, "global {} ", self.linkage)?; +impl IrWrite for GlobalVariableData +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + write!(w, "global ")?; + self.linkage.write(w, ctx)?; if self.is_const { - write!(w, "const ")?; + write!(w, " const")?; } - self.ty.write(module, &mut *w)?; + write!(w, " ")?; + self.ty.write(w, ctx)?; write!(w, " ${}", self.symbol)?; - if let Some(data) = &self.data { - write!(w, " = {data}")?; + if let Some(initializer) = &self.initializer { + write!(w, " = ")?; + initializer.write(w, ctx)?; } Ok(()) } @@ -149,29 +164,35 @@ impl GvInitializer { } } -impl fmt::Display for GvInitializer { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { +impl IrWrite for GvInitializer +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { match self { - Self::Immediate(data) => write!(f, "{}", data), + Self::Immediate(data) => write!(w, "{}", data), Self::Array(data) => { - write!(f, "[")?; + write!(w, "[")?; for (i, v) in data.iter().enumerate() { if i > 0 { - write!(f, ", ")?; + write!(w, ", ")?; } - write!(f, "{}", v)?; + v.write(w, ctx)?; } - write!(f, "]") + write!(w, "]") } Self::Struct(data) => { - write!(f, "{{")?; + write!(w, "{{")?; for (i, v) in data.iter().enumerate() { if i > 0 { - write!(f, ", ")?; + write!(w, ", ")?; } - write!(f, "{}", v)?; + v.write(w, ctx)?; } - write!(f, "}}") + write!(w, "}}") } } } diff --git a/crates/ir/src/graphviz/block.rs b/crates/ir/src/graphviz/block.rs index f7d9bd74..34fad190 100644 --- a/crates/ir/src/graphviz/block.rs +++ b/crates/ir/src/graphviz/block.rs @@ -4,7 +4,7 @@ use dot2::label; use super::function::DUMMY_BLOCK; use crate::{ - ir_writer::{FuncWriteCtx, ValueWithTy, WriteWithFunc}, + ir_writer::{FuncWriteCtx, IrWrite, ValueWithTy}, BlockId, ControlFlowGraph, Function, }; @@ -28,7 +28,7 @@ impl<'a> BlockNode<'a> { } } -impl<'a> BlockNode<'a> { +impl BlockNode<'_> { pub(super) fn label(self) -> label::Text<'static> { let Self { block, ctx, .. } = self; let Function { sig, layout, .. } = &ctx.func; diff --git a/crates/ir/src/graphviz/function.rs b/crates/ir/src/graphviz/function.rs index bd625b58..98f06aba 100644 --- a/crates/ir/src/graphviz/function.rs +++ b/crates/ir/src/graphviz/function.rs @@ -5,7 +5,7 @@ use dot2::{label::Text, GraphWalk, Id, Labeller, Style}; use super::block::BlockNode; use crate::{ inst::control_flow::Phi, - ir_writer::{FuncWriteCtx, WriteWithFunc}, + ir_writer::{FuncWriteCtx, IrWrite}, prelude::*, BlockId, ControlFlowGraph, }; @@ -124,7 +124,7 @@ pub(super) struct BlockEdge<'a> { ctx: &'a FuncWriteCtx<'a>, } -impl<'a> BlockEdge<'a> { +impl BlockEdge<'_> { fn label(self) -> Text<'static> { let Self { from, to, ctx } = self; let to = to.block; diff --git a/crates/ir/src/inst/arith.rs b/crates/ir/src/inst/arith.rs index 656a1795..caaf7ecb 100644 --- a/crates/ir/src/inst/arith.rs +++ b/crates/ir/src/inst/arith.rs @@ -1,13 +1,12 @@ use macros::Inst; -use crate::{inst::impl_inst_write, ValueId}; +use crate::ValueId; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Neg { #[inst(value)] arg: ValueId, } -impl_inst_write!(Neg); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Add { @@ -16,7 +15,6 @@ pub struct Add { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Add); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Mul { @@ -25,7 +23,6 @@ pub struct Mul { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Mul); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Sub { @@ -34,7 +31,6 @@ pub struct Sub { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Sub); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Sdiv { @@ -43,7 +39,6 @@ pub struct Sdiv { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Sdiv); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Udiv { @@ -52,7 +47,6 @@ pub struct Udiv { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Udiv); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Umod { @@ -61,7 +55,6 @@ pub struct Umod { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Umod); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Smod { @@ -70,7 +63,6 @@ pub struct Smod { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Smod); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Shl { @@ -79,7 +71,6 @@ pub struct Shl { #[inst(value)] value: ValueId, } -impl_inst_write!(Shl); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Shr { @@ -88,7 +79,6 @@ pub struct Shr { #[inst(value)] value: ValueId, } -impl_inst_write!(Shr); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Sar { @@ -97,4 +87,3 @@ pub struct Sar { #[inst(value)] value: ValueId, } -impl_inst_write!(Sar); diff --git a/crates/ir/src/inst/cast.rs b/crates/ir/src/inst/cast.rs index fb6b54c4..d03e904f 100644 --- a/crates/ir/src/inst/cast.rs +++ b/crates/ir/src/inst/cast.rs @@ -1,6 +1,6 @@ use macros::Inst; -use crate::{inst::impl_inst_write, Type, ValueId}; +use crate::{Type, ValueId}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Sext { @@ -8,7 +8,6 @@ pub struct Sext { from: ValueId, ty: Type, } -impl_inst_write!(Sext, (from: ValueId, ty: Type)); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Zext { @@ -16,7 +15,6 @@ pub struct Zext { from: ValueId, ty: Type, } -impl_inst_write!(Zext, (from: ValueId, ty: Type)); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Trunc { @@ -24,7 +22,6 @@ pub struct Trunc { from: ValueId, ty: Type, } -impl_inst_write!(Trunc, (from: ValueId, ty: Type)); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Bitcast { @@ -32,7 +29,6 @@ pub struct Bitcast { from: ValueId, ty: Type, } -impl_inst_write!(Bitcast, (from: ValueId, ty: Type)); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct IntToPtr { @@ -40,7 +36,6 @@ pub struct IntToPtr { from: ValueId, ty: Type, } -impl_inst_write!(IntToPtr, (from: ValueId, ty: Type)); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct PtrToInt { @@ -48,4 +43,3 @@ pub struct PtrToInt { from: ValueId, ty: Type, } -impl_inst_write!(PtrToInt, (from: ValueId, ty: Type)); diff --git a/crates/ir/src/inst/cmp.rs b/crates/ir/src/inst/cmp.rs index baf7fd36..eb37a140 100644 --- a/crates/ir/src/inst/cmp.rs +++ b/crates/ir/src/inst/cmp.rs @@ -1,6 +1,6 @@ use macros::Inst; -use crate::{inst::impl_inst_write, ValueId}; +use crate::ValueId; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Lt { @@ -9,7 +9,6 @@ pub struct Lt { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Lt); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Gt { @@ -18,7 +17,6 @@ pub struct Gt { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Gt); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Slt { @@ -27,7 +25,6 @@ pub struct Slt { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Slt); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Sgt { @@ -36,7 +33,6 @@ pub struct Sgt { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Sgt); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Le { @@ -45,7 +41,6 @@ pub struct Le { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Le); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Ge { @@ -54,7 +49,6 @@ pub struct Ge { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Ge); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Sle { @@ -63,7 +57,6 @@ pub struct Sle { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Sle); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Sge { @@ -72,7 +65,6 @@ pub struct Sge { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Sge); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Eq { @@ -81,7 +73,6 @@ pub struct Eq { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Eq); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Ne { @@ -90,11 +81,9 @@ pub struct Ne { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Ne); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct IsZero { #[inst(value)] lhs: ValueId, } -impl_inst_write!(IsZero); diff --git a/crates/ir/src/inst/control_flow.rs b/crates/ir/src/inst/control_flow.rs index 3b473159..2b2ffffd 100644 --- a/crates/ir/src/inst/control_flow.rs +++ b/crates/ir/src/inst/control_flow.rs @@ -1,27 +1,13 @@ -use std::io; - use macros::{inst_prop, Inst}; use smallvec::SmallVec; -use super::InstWrite; -use crate::{ - ir_writer::{FuncWriteCtx, WriteWithFunc}, - module::FuncRef, - BlockId, Inst, InstSetBase, ValueId, -}; +use crate::{module::FuncRef, BlockId, Inst, InstSetBase, ValueId}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(terminator)] pub struct Jump { dest: BlockId, } -impl InstWrite for Jump { - fn write(&self, ctx: &FuncWriteCtx, mut w: &mut dyn io::Write) -> io::Result<()> { - let name = self.as_text(); - write!(w, "{name} ")?; - self.dest.write(ctx, &mut w) - } -} #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] #[inst(terminator)] @@ -31,17 +17,6 @@ pub struct Br { nz_dest: BlockId, z_dest: BlockId, } -impl InstWrite for Br { - fn write(&self, ctx: &FuncWriteCtx, mut w: &mut dyn io::Write) -> io::Result<()> { - write!(w, "{}", self.as_text())?; - write!(w, " ")?; - self.cond.write(ctx, &mut w)?; - write!(w, " ")?; - self.nz_dest.write(ctx, &mut w)?; - write!(w, " ")?; - self.z_dest.write(ctx, &mut w) - } -} #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] #[inst(terminator)] @@ -53,33 +28,13 @@ pub struct BrTable { #[inst(value)] table: Vec<(ValueId, BlockId)>, } -impl InstWrite for BrTable { - fn write(&self, ctx: &FuncWriteCtx, mut w: &mut dyn io::Write) -> io::Result<()> { - write!(w, "{}", self.as_text())?; - write!(w, " ")?; - self.scrutinee.write(ctx, &mut w)?; - if let Some(default) = self.default { - write!(w, " ")?; - default.write(ctx, &mut w)?; - }; - - for (value, block) in &self.table { - write!(w, " (")?; - value.write(ctx, &mut w)?; - write!(w, " ")?; - block.write(ctx, &mut w)?; - write!(w, ")")?; - } - - Ok(()) - } -} #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] pub struct Phi { #[inst(value)] args: Vec<(ValueId, BlockId)>, } + impl Phi { pub fn append_phi_arg(&mut self, value: ValueId, block: BlockId) { self.args.push((value, block)) @@ -98,45 +53,19 @@ impl Phi { self.args.retain(|(_, block)| f(*block)) } } -impl InstWrite for Phi { - fn write(&self, ctx: &FuncWriteCtx, mut w: &mut dyn io::Write) -> io::Result<()> { - write!(w, "{}", self.as_text())?; - - for (value, block) in &self.args { - write!(w, " (")?; - value.write(ctx, &mut w)?; - write!(w, " ")?; - block.write(ctx, &mut w)?; - write!(w, ")")?; - } - - Ok(()) - } -} +// TODO: We need to perform analysis or modify function signature definition to +// know if +// * the function call has side effect +// * the function call is terminator #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(super::SideEffect::Write))] -#[inst(terminator)] pub struct Call { callee: FuncRef, #[inst(value)] args: SmallVec<[ValueId; 8]>, } -impl InstWrite for Call { - fn write(&self, ctx: &FuncWriteCtx, mut w: &mut dyn io::Write) -> io::Result<()> { - let name = self.as_text(); - ctx.func.ctx().func_sig(self.callee, |sig| { - let callee = sig.name(); - write!(w, "{name} %{callee}") - })?; - for value in &self.args { - write!(w, " ")?; - value.write(ctx, &mut w)?; - } - Ok(()) - } -} #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(super::SideEffect::Write))] @@ -145,16 +74,6 @@ pub struct Return { #[inst(value)] arg: Option, } -impl InstWrite for Return { - fn write(&self, ctx: &FuncWriteCtx, mut w: &mut dyn io::Write) -> io::Result<()> { - write!(w, "{}", self.as_text())?; - if let Some(arg) = self.arg { - write!(w, " ")?; - arg.write(ctx, &mut w)?; - } - Ok(()) - } -} #[inst_prop] pub trait Branch { diff --git a/crates/ir/src/inst/data.rs b/crates/ir/src/inst/data.rs index 1219330c..3da84681 100644 --- a/crates/ir/src/inst/data.rs +++ b/crates/ir/src/inst/data.rs @@ -1,10 +1,7 @@ -use std::io; - use macros::Inst; use smallvec::SmallVec; -use super::{Inst, InstWrite}; -use crate::{inst::impl_inst_write, ir_writer::FuncWriteCtx, module::FuncRef, Type, ValueId}; +use crate::{module::FuncRef, Type, ValueId}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(super::SideEffect::Read))] @@ -13,7 +10,6 @@ pub struct Mload { addr: ValueId, ty: Type, } -impl_inst_write!(Mload, (addr: ValueId, ty: Type)); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(super::SideEffect::Write))] @@ -24,36 +20,23 @@ pub struct Mstore { value: ValueId, ty: Type, } -impl_inst_write!(Mstore, (addr: ValueId, value: ValueId, ty: Type)); #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] pub struct Gep { #[inst(value)] values: SmallVec<[ValueId; 8]>, } -impl_inst_write!(Gep); #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] pub struct GetFunctionPtr { func: FuncRef, } -impl InstWrite for GetFunctionPtr { - fn write(&self, ctx: &FuncWriteCtx, w: &mut dyn io::Write) -> io::Result<()> { - let name = self.as_text(); - ctx.func.ctx().func_sig(self.func, |sig| { - let callee = sig.name(); - write!(w, "{name} %{callee}") - })?; - Ok(()) - } -} #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(super::SideEffect::Write))] pub struct Alloca { ty: Type, } -impl_inst_write!(Alloca); #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] pub struct InsertValue { @@ -64,7 +47,6 @@ pub struct InsertValue { #[inst(value)] value: ValueId, } -impl_inst_write!(InsertValue); #[derive(Debug, Clone, PartialEq, Eq, Hash, Inst)] pub struct ExtractValue { @@ -73,4 +55,3 @@ pub struct ExtractValue { #[inst(value)] idx: ValueId, } -impl_inst_write!(ExtractValue); diff --git a/crates/ir/src/inst/evm/mod.rs b/crates/ir/src/inst/evm/mod.rs index 52222d2a..d42af28e 100644 --- a/crates/ir/src/inst/evm/mod.rs +++ b/crates/ir/src/inst/evm/mod.rs @@ -1,10 +1,7 @@ -use std::io; - use macros::Inst; pub mod inst_set; -use super::{Inst, InstWrite}; -use crate::{inst::impl_inst_write, ir_writer::FuncWriteCtx, module::FuncRef, value::ValueId}; +use crate::{module::FuncRef, value::ValueId}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmUdiv { @@ -13,7 +10,6 @@ pub struct EvmUdiv { #[inst(value)] rhs: ValueId, } -impl_inst_write!(EvmUdiv); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmSdiv { @@ -22,7 +18,6 @@ pub struct EvmSdiv { #[inst(value)] rhs: ValueId, } -impl_inst_write!(EvmSdiv); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmUmod { @@ -31,7 +26,6 @@ pub struct EvmUmod { #[inst(value)] rhs: ValueId, } -impl_inst_write!(EvmUmod); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmSmod { @@ -40,19 +34,16 @@ pub struct EvmSmod { #[inst(value)] rhs: ValueId, } -impl_inst_write!(EvmSmod); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] #[inst(terminator)] pub struct EvmStop {} -impl_inst_write!(EvmStop); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] #[inst(terminator)] pub struct EvmInvalid {} -impl_inst_write!(EvmInvalid); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmAddMod { @@ -63,7 +54,6 @@ pub struct EvmAddMod { #[inst(value)] modulus: ValueId, } -impl_inst_write!(EvmAddMod); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmMulMod { @@ -74,7 +64,6 @@ pub struct EvmMulMod { #[inst(value)] modulus: ValueId, } -impl_inst_write!(EvmMulMod); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmExp { @@ -83,7 +72,6 @@ pub struct EvmExp { #[inst(value)] exponent: ValueId, } -impl_inst_write!(EvmExp); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmByte { @@ -92,7 +80,6 @@ pub struct EvmByte { #[inst(value)] value: ValueId, } -impl_inst_write!(EvmByte); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct EvmKeccak256 { @@ -101,12 +88,10 @@ pub struct EvmKeccak256 { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmKeccak256); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmAddress {} -impl_inst_write!(EvmAddress); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] @@ -114,34 +99,28 @@ pub struct EvmBalance { #[inst(value)] contract_addr: ValueId, } -impl_inst_write!(EvmBalance); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmOrigin {} -impl_inst_write!(EvmOrigin); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmCaller {} -impl_inst_write!(EvmCaller); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmCallValue {} -impl_inst_write!(EvmCallValue); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmCallDataLoad { data_offset: ValueId, } -impl_inst_write!(EvmCallDataLoad); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmCallDataSize {} -impl_inst_write!(EvmCallDataSize); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -153,12 +132,10 @@ pub struct EvmCallDataCopy { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmCallDataCopy); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmCodeSize {} -impl_inst_write!(EvmCodeSize); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -170,12 +147,10 @@ pub struct EvmCodeCopy { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmCodeCopy); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmGasPrice {} -impl_inst_write!(EvmGasPrice); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] @@ -183,7 +158,6 @@ pub struct EvmExtCodeSize { #[inst(value)] ext_addr: ValueId, } -impl_inst_write!(EvmExtCodeSize); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -197,12 +171,10 @@ pub struct EvmExtCodeCopy { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmExtCodeCopy); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmReturnDataSize {} -impl_inst_write!(EvmReturnDataSize); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -214,7 +186,6 @@ pub struct EvmReturnDataCopy { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmReturnDataCopy); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] @@ -222,7 +193,6 @@ pub struct EvmExtCodeHash { #[inst(value)] ext_addr: ValueId, } -impl_inst_write!(EvmExtCodeHash); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] @@ -230,7 +200,6 @@ pub struct EvmBlockHash { #[inst(value)] block_num: ValueId, } -impl_inst_write!(EvmBlockHash); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] @@ -238,42 +207,34 @@ pub struct EvmCoinBase { #[inst(value)] block_num: ValueId, } -impl_inst_write!(EvmCoinBase); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmTimestamp {} -impl_inst_write!(EvmTimestamp); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmNumber {} -impl_inst_write!(EvmNumber); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmPrevRandao {} -impl_inst_write!(EvmPrevRandao); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmGasLimit {} -impl_inst_write!(EvmGasLimit); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmChainId {} -impl_inst_write!(EvmChainId); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmSelfBalance {} -impl_inst_write!(EvmSelfBalance); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmBaseFee {} -impl_inst_write!(EvmBaseFee); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] @@ -281,12 +242,10 @@ pub struct EvmBlobHash { #[inst(value)] idx: ValueId, } -impl_inst_write!(EvmBlobHash); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmBlobBaseFee {} -impl_inst_write!(EvmBlobBaseFee); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -296,7 +255,6 @@ pub struct EvmMstore8 { #[inst(value)] val: ValueId, } -impl_inst_write!(EvmMstore8); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] @@ -304,7 +262,6 @@ pub struct EvmSload { #[inst(value)] key: ValueId, } -impl_inst_write!(EvmSload); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -314,17 +271,14 @@ pub struct EvmSstore { #[inst(value)] val: ValueId, } -impl_inst_write!(EvmSstore); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmMsize {} -impl_inst_write!(EvmMsize); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] pub struct EvmGas {} -impl_inst_write!(EvmGas); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Read))] @@ -332,7 +286,6 @@ pub struct EvmTload { #[inst(value)] key: ValueId, } -impl_inst_write!(EvmTload); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -342,7 +295,6 @@ pub struct EvmTstore { #[inst(value)] val: ValueId, } -impl_inst_write!(EvmTstore); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -354,7 +306,6 @@ pub struct EvmMcopy { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmMcopy); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -364,7 +315,6 @@ pub struct EvmLog0 { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmLog0); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -376,7 +326,6 @@ pub struct EvmLog1 { #[inst(value)] topic0: ValueId, } -impl_inst_write!(EvmLog1); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -390,7 +339,6 @@ pub struct EvmLog2 { #[inst(value)] topic1: ValueId, } -impl_inst_write!(EvmLog2); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -406,7 +354,6 @@ pub struct EvmLog3 { #[inst(value)] topic2: ValueId, } -impl_inst_write!(EvmLog3); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -424,7 +371,6 @@ pub struct EvmLog4 { #[inst(value)] topic3: ValueId, } -impl_inst_write!(EvmLog4); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -436,7 +382,6 @@ pub struct EvmCreate { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmCreate); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -456,7 +401,6 @@ pub struct EvmCall { #[inst(value)] ret_offset: ValueId, } -impl_inst_write!(EvmCall); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -476,7 +420,6 @@ pub struct EvmCallCode { #[inst(value)] ret_offset: ValueId, } -impl_inst_write!(EvmCallCode); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -487,7 +430,6 @@ pub struct EvmReturn { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmReturn); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -505,7 +447,6 @@ pub struct EvmDelegateCall { #[inst(value)] ret_len: ValueId, } -impl_inst_write!(EvmDelegateCall); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -519,7 +460,6 @@ pub struct EvmCreate2 { #[inst(value)] salt: ValueId, } -impl_inst_write!(EvmCreate2); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -537,7 +477,6 @@ pub struct EvmStaticCall { #[inst(value)] ret_len: ValueId, } -impl_inst_write!(EvmStaticCall); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -548,7 +487,6 @@ pub struct EvmRevert { #[inst(value)] len: ValueId, } -impl_inst_write!(EvmRevert); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -557,7 +495,6 @@ pub struct EvmSelfDestruct { #[inst(value)] addr: ValueId, } -impl_inst_write!(EvmSelfDestruct); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] #[inst(side_effect(crate::inst::SideEffect::Write))] @@ -565,7 +502,6 @@ pub struct EvmMalloc { #[inst(value)] size: ValueId, } -impl_inst_write!(EvmMalloc); /// An instruction that takes the main function of a contract /// as an argument and returns the size of the contract. @@ -573,13 +509,3 @@ impl_inst_write!(EvmMalloc); pub struct EvmContractSize { contract: FuncRef, } -impl InstWrite for EvmContractSize { - fn write(&self, ctx: &FuncWriteCtx, w: &mut dyn io::Write) -> io::Result<()> { - let name = self.as_text(); - ctx.func.ctx().func_sig(self.contract, |sig| { - let callee = sig.name(); - write!(w, "{name} %{callee}") - })?; - Ok(()) - } -} diff --git a/crates/ir/src/inst/logic.rs b/crates/ir/src/inst/logic.rs index 9ab2236a..52a63883 100644 --- a/crates/ir/src/inst/logic.rs +++ b/crates/ir/src/inst/logic.rs @@ -1,13 +1,12 @@ use macros::Inst; -use crate::{inst::impl_inst_write, ValueId}; +use crate::ValueId; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Not { #[inst(value)] arg: ValueId, } -impl_inst_write!(Not); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct And { @@ -16,7 +15,6 @@ pub struct And { #[inst(value)] rhs: ValueId, } -impl_inst_write!(And); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Or { @@ -25,7 +23,6 @@ pub struct Or { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Or); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Inst)] pub struct Xor { @@ -34,4 +31,3 @@ pub struct Xor { #[inst(value)] rhs: ValueId, } -impl_inst_write!(Xor); diff --git a/crates/ir/src/inst/mod.rs b/crates/ir/src/inst/mod.rs index d7484e74..8dba2fc2 100644 --- a/crates/ir/src/inst/mod.rs +++ b/crates/ir/src/inst/mod.rs @@ -19,7 +19,7 @@ use rustc_hash::FxHashSet; use smallvec::SmallVec; use crate::{ - ir_writer::{FuncWriteCtx, WriteWithFunc}, + ir_writer::{FuncWriteCtx, IrWrite}, InstSetBase, ValueId, }; @@ -63,11 +63,14 @@ pub trait InstExt: Inst { fn belongs_to(isb: &dyn InstSetBase) -> Option<&dyn HasInst>; } -impl WriteWithFunc for InstId { - fn write(&self, ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()> { +impl IrWrite> for InstId { + fn write(&self, w: &mut W, ctx: &FuncWriteCtx) -> io::Result<()> + where + W: io::Write, + { let inst = ctx.func.dfg.inst(*self); let write: &dyn InstWrite = InstDowncast::downcast(ctx.func.inst_set(), inst).unwrap(); - write.write(ctx, w) + write.write(w, ctx) } } @@ -213,64 +216,6 @@ impl SideEffect { #[inst_prop] trait InstWrite { - fn write(&self, ctx: &FuncWriteCtx, w: &mut dyn io::Write) -> io::Result<()>; + fn write(&self, w: &mut dyn io::Write, ctx: &FuncWriteCtx) -> io::Result<()>; type Members = All; } - -macro_rules! impl_inst_write { - ($ty:ty) => { - impl $crate::inst::InstWrite for $ty { - fn write( - &self, - ctx: &crate::ir_writer::FuncWriteCtx, - mut w: &mut dyn std::io::Write, - ) -> std::io::Result<()> { - w.write_fmt(format_args!("{} ", crate::Inst::as_text(self)))?; - let values = crate::Inst::collect_values(self); - let mut values = values.iter(); - - if let Some(value) = values.next() { - $crate::ir_writer::WriteWithFunc::write(value, ctx, &mut w)? - } - for value in values { - write!(w, " ")?; - $crate::ir_writer::WriteWithFunc::write(value, ctx, &mut w)? - } - Ok(()) - } - } - }; - - ($ty:ty, ($($field:ident: $kind:ident),+)) => { - impl $crate::inst::InstWrite for $ty { - fn write( - &self, - ctx: &crate::ir_writer::FuncWriteCtx, - mut w: &mut dyn std::io::Write, - ) -> std::io::Result<()> { - w.write_fmt(format_args!("{} ", crate::Inst::as_text(self)))?; - $crate::inst::__write_args!(self, ctx, &mut w, ($($field: $kind),+)); - Ok(()) - } - } - }; -} - -macro_rules! __write_args { - ($self:expr, $ctx:expr, $w:expr, ($field:ident: $kind:ident $(,$fields:ident: $kinds:ident)+)) => { - $crate::inst::__write_args!($self, $ctx, $w, ($field: $kind)); - write!(&mut $w, " ")?; - $crate::inst::__write_args!($self, $ctx, $w, ($($fields: $kinds),+)); - }; - - ($self:expr, $ctx:expr, $w:expr, ($field:ident: Type)) => { - $crate::ir_writer::WriteWithModule::write($self.$field(), $ctx.func.ctx(), $w)? - }; - - ($self:expr, $ctx:expr, $w:expr, ($field:ident: ValueId)) => { - $crate::ir_writer::WriteWithFunc::write($self.$field(), $ctx, $w)? - }; -} - -use __write_args; -use impl_inst_write; diff --git a/crates/ir/src/ir_writer.rs b/crates/ir/src/ir_writer.rs index e0b635d9..951feafe 100644 --- a/crates/ir/src/ir_writer.rs +++ b/crates/ir/src/ir_writer.rs @@ -1,5 +1,7 @@ use std::io; +use smallvec::{Array, SmallVec}; + use super::{BlockId, Function}; use crate::{ module::{FuncRef, ModuleCtx}, @@ -30,7 +32,7 @@ impl<'a> ModuleWriter<'a> { let mut has_type_def = false; for s in s.all_struct_data() { has_type_def = true; - s.write(&self.module.ctx, &mut *w)?; + s.write(w, &self.module.ctx)?; writeln!(w)?; } @@ -44,7 +46,7 @@ impl<'a> ModuleWriter<'a> { // Write module level global variables. self.module.ctx.with_gv_store(|s| { for gv in s.all_gv_data() { - gv.write(&self.module.ctx, &mut *w)?; + gv.write(w, &self.module.ctx)?; writeln!(w)?; } @@ -79,6 +81,12 @@ pub struct FuncWriteCtx<'a> { pub dbg: &'a dyn DebugProvider, } +impl AsRef for FuncWriteCtx<'_> { + fn as_ref(&self) -> &ModuleCtx { + self.module_ctx() + } +} + impl<'a> FuncWriteCtx<'a> { pub fn with_debug_provider( func: &'a Function, @@ -125,29 +133,27 @@ impl<'a> FuncWriter<'a> { } pub fn write(&mut self, w: &mut impl io::Write) -> io::Result<()> { - w.write_fmt(format_args!( - "func {} %{}(", - self.ctx.func.sig.linkage(), - self.ctx.func.sig.name() - ))?; - write_iter_with_delim( - &mut *w, - &self.ctx, - self.ctx.func.arg_values.iter().map(|v| ValueWithTy(*v)), - ", ", - )?; - write!(w, ") -> ")?; - self.ctx + let func = &self.ctx.func; + write!(w, "func ")?; + func.sig.linkage().write(w, &self.ctx)?; + write!(w, " %{}(", func.sig.name())?; + let arg_values: SmallVec<[ValueWithTy; 8]> = self + .ctx .func - .sig - .ret_ty() - .write(self.ctx.module_ctx(), &mut *w)?; + .arg_values + .iter() + .map(|value| ValueWithTy(*value)) + .collect(); + arg_values.write_with_delim(w, ", ", &self.ctx)?; + + write!(w, ") -> ")?; + func.sig.ret_ty().write(w, &self.ctx)?; writeln!(w, " {{")?; self.level += 1; for block in self.ctx.func.layout.iter_block() { - self.write_block_with_inst(block, &mut *w)?; + self.write_block_with_inst(w, block)?; if !self.ctx.func.layout.is_last_block(block) { writeln!(w)?; } @@ -163,27 +169,27 @@ impl<'a> FuncWriter<'a> { unsafe { String::from_utf8_unchecked(s) } } - fn write_block_with_inst(&mut self, block: BlockId, w: &mut impl io::Write) -> io::Result<()> { - self.indent(&mut *w)?; - block.write(&self.ctx, &mut *w)?; + fn write_block_with_inst(&mut self, w: &mut impl io::Write, block: BlockId) -> io::Result<()> { + self.indent(w)?; + block.write(w, &self.ctx)?; self.enter(&mut *w)?; for inst in self.ctx.func.layout.iter_inst(block) { - self.write_inst_in_block(inst, &mut *w)?; + self.write_inst_in_block(w, inst)?; } self.leave(); Ok(()) } - fn write_inst_in_block(&mut self, inst: InstId, w: &mut impl io::Write) -> io::Result<()> { + fn write_inst_in_block(&mut self, w: &mut impl io::Write, inst: InstId) -> io::Result<()> { self.indent(&mut *w)?; let inst_with_res = InstStatement(inst); - inst_with_res.write(&self.ctx, &mut *w)?; + inst_with_res.write(w, &self.ctx)?; writeln!(w) } - fn indent(&self, mut w: impl io::Write) -> io::Result<()> { + fn indent(&self, w: &mut impl io::Write) -> io::Result<()> { write!(w, "{}", " ".repeat(self.level as usize * 4)) } @@ -197,94 +203,201 @@ impl<'a> FuncWriter<'a> { } } -pub trait WriteWithModule { - fn write(&self, module: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()>; +pub trait IrWrite { + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write; - fn dump_string(&self, module: &ModuleCtx) -> String { - let mut s = Vec::new(); - self.write(module, &mut s).unwrap(); - unsafe { String::from_utf8_unchecked(s) } + fn write_with_delim(&self, w: &mut W, _delim: &str, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + self.write(w, ctx) + } + + fn dump_string(&self, ctx: &Ctx) -> String { + let mut bytes = Vec::new(); + self.write(&mut bytes, ctx).unwrap(); + unsafe { String::from_utf8_unchecked(bytes) } + } + + fn has_content(&self) -> bool { + true } } -impl WriteWithModule for &T +impl IrWrite for (T, U) where - T: WriteWithModule, + T: IrWrite, + U: IrWrite, { - fn write(&self, module: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()> { - (*self).write(module, w) + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + self.write_with_delim(w, " ", ctx) + } + + fn write_with_delim(&self, w: &mut W, delim: &str, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + write!(w, "(")?; + self.0.write(w, ctx)?; + if self.has_content() { + write!(w, "{delim}")?; + self.1.write(w, ctx)?; + } + write!(w, ")") + } + + fn has_content(&self) -> bool { + self.1.has_content() || self.has_content() } } -pub trait WriteWithFunc { - fn write(&self, ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()>; +impl IrWrite for [T] +where + T: IrWrite, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + self.write_with_delim(w, " ", ctx) + } - fn dump_string(&self, ctx: &FuncWriteCtx) -> String { - let mut s = Vec::new(); - self.write(ctx, &mut s).unwrap(); - unsafe { String::from_utf8_unchecked(s) } + fn write_with_delim(&self, w: &mut W, delim: &str, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + let mut iter = self.iter(); + let mut has_written = false; + + if let Some(value) = iter.next() { + has_written |= value.has_content(); + value.write(w, ctx)?; + } + + for value in iter { + if has_written { + write!(w, "{delim}")?; + } + value.write_with_delim(w, delim, ctx)?; + has_written |= value.has_content(); + } + + Ok(()) + } + + fn has_content(&self) -> bool { + self.iter().any(|x| x.has_content()) + } +} + +impl IrWrite for Vec +where + T: IrWrite, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + self.as_slice().write(w, ctx) + } + + fn write_with_delim(&self, w: &mut W, delim: &str, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + self.as_slice().write_with_delim(w, delim, ctx) + } + + fn has_content(&self) -> bool { + self.as_slice().has_content() + } +} + +impl IrWrite for SmallVec<[T; N]> +where + [T; N]: Array, + T: IrWrite, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + self.as_slice().write(w, ctx) + } + + fn write_with_delim(&self, w: &mut W, delim: &str, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + self.as_slice().write_with_delim(w, delim, ctx) + } + + fn has_content(&self) -> bool { + self.as_slice().has_content() } } -impl WriteWithFunc for &T +impl IrWrite for Option where - T: WriteWithFunc, + T: IrWrite, { - fn write(&self, ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()> { - (*self).write(ctx, w) + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + if let Some(content) = self { + content.write(w, ctx)?; + } + + Ok(()) + } + + fn has_content(&self) -> bool { + self.as_ref().map(|x| x.has_content()).unwrap_or_default() } } #[derive(Clone, Copy)] pub struct ValueWithTy(pub ValueId); -impl WriteWithFunc for ValueWithTy { - fn write(&self, ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()> { +impl IrWrite> for ValueWithTy { + fn write(&self, w: &mut W, ctx: &FuncWriteCtx) -> io::Result<()> + where + W: io::Write, + { let value = self.0; - value.write(ctx, w)?; + value.write(w, ctx)?; if let Value::Immediate { .. } = ctx.dfg().value(self.0) { Ok(()) } else { let ty = ctx.dfg().value_ty(self.0); write!(w, ".")?; - ty.write(ctx.module_ctx(), &mut *w) + ty.write(w, ctx) } } } pub struct InstStatement(pub InstId); -impl WriteWithFunc for InstStatement { - fn write(&self, ctx: &FuncWriteCtx, w: &mut impl io::Write) -> io::Result<()> { +impl IrWrite> for InstStatement { + fn write(&self, w: &mut W, ctx: &FuncWriteCtx) -> io::Result<()> + where + W: io::Write, + { if let Some(result) = ctx.func.dfg.inst_result(self.0) { let result_with_ty = ValueWithTy(result); - result_with_ty.write(ctx, &mut *w)?; + result_with_ty.write(w, ctx)?; write!(w, " = ")?; }; - self.0.write(ctx, w)?; + self.0.write(w, ctx)?; write!(w, ";") } } -pub fn write_iter_with_delim( - w: &mut impl io::Write, - ctx: &FuncWriteCtx, - iter: impl Iterator, - delim: &str, -) -> io::Result<()> -where - T: WriteWithFunc, -{ - let mut iter = iter.peekable(); - while let Some(item) = iter.next() { - item.write(ctx, &mut *w)?; - if iter.peek().is_some() { - write!(w, "{delim}")?; - } - } - - Ok(()) -} - pub trait DebugProvider { #[allow(unused)] fn value_name(&self, func: &Function, func_ref: FuncRef, value: ValueId) -> Option<&str> { diff --git a/crates/ir/src/layout.rs b/crates/ir/src/layout.rs index e6d3ad72..20362d15 100644 --- a/crates/ir/src/layout.rs +++ b/crates/ir/src/layout.rs @@ -301,7 +301,7 @@ struct BlockIter<'a> { blocks: &'a SecondaryMap, } -impl<'a> Iterator for BlockIter<'a> { +impl Iterator for BlockIter<'_> { type Item = BlockId; fn next(&mut self) -> Option { @@ -316,7 +316,7 @@ struct InstIter<'a> { insts: &'a SecondaryMap, } -impl<'a> Iterator for InstIter<'a> { +impl Iterator for InstIter<'_> { type Item = InstId; fn next(&mut self) -> Option { diff --git a/crates/ir/src/linkage.rs b/crates/ir/src/linkage.rs index 5f89ae01..ac4f4856 100644 --- a/crates/ir/src/linkage.rs +++ b/crates/ir/src/linkage.rs @@ -1,25 +1,35 @@ -use std::{fmt, str::FromStr}; +use std::{io, str::FromStr}; + +use crate::{ir_writer::IrWrite, module::ModuleCtx}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)] /// Linkage of symbols. pub enum Linkage { - /// The symbol is defined in the module, and can be used from the outside of the module. + /// The symbol is defined in the module, and can be used from the outside of + /// the module. Public, #[default] - /// The symbol is defined in the module, and can NOT be called from another module. + /// The symbol is defined in the module, and can NOT be called from another + /// module. Private, /// The symbol is defined outside of the module. External, } -impl fmt::Display for Linkage { - fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result { +impl IrWrite for Linkage +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, _ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { match self { - Self::Public => write!(f, "public"), - Self::Private => write!(f, "private"), - Self::External => write!(f, "external"), + Self::Public => write!(w, "public"), + Self::Private => write!(w, "private"), + Self::External => write!(w, "external"), } } } diff --git a/crates/ir/src/module.rs b/crates/ir/src/module.rs index c0048884..b2e0fa26 100644 --- a/crates/ir/src/module.rs +++ b/crates/ir/src/module.rs @@ -7,6 +7,7 @@ use sonatina_triple::TargetTriple; use crate::{ global_variable::GlobalVariableStore, + ir_writer::IrWrite, isa::{Endian, Isa, TypeLayout, TypeLayoutError}, types::TypeStore, Function, InstSetBase, Signature, Type, @@ -109,6 +110,11 @@ pub struct ModuleCtx { type_store: Arc>, gv_store: Arc>, } +impl AsRef for ModuleCtx { + fn as_ref(&self) -> &ModuleCtx { + self + } +} impl ModuleCtx { pub fn new(isa: &T) -> Self { @@ -189,3 +195,16 @@ impl FuncRef { ctx.func_sig(self, |sig| sig.func_ptr_type(ctx)) } } + +impl IrWrite for FuncRef +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> std::io::Result<()> + where + W: std::io::Write + ?Sized, + { + ctx.as_ref() + .func_sig(*self, |sig| write!(w, "%{}", sig.name())) + } +} diff --git a/crates/ir/src/types.rs b/crates/ir/src/types.rs index 9048a197..afa240a4 100644 --- a/crates/ir/src/types.rs +++ b/crates/ir/src/types.rs @@ -6,7 +6,7 @@ use indexmap::IndexMap; use rustc_hash::FxHashMap; use smallvec::SmallVec; -use crate::{ir_writer::WriteWithModule, module::ModuleCtx}; +use crate::{ir_writer::IrWrite, module::ModuleCtx}; #[derive(Debug, Default)] pub struct TypeStore { @@ -203,8 +203,14 @@ impl cmp::PartialOrd for Type { } } -impl WriteWithModule for Type { - fn write(&self, ctx: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()> { +impl IrWrite for Type +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { match self { Type::I1 => write!(w, "i1"), Type::I8 => write!(w, "i8"), @@ -213,7 +219,7 @@ impl WriteWithModule for Type { Type::I64 => write!(w, "i64"), Type::I128 => write!(w, "i128"), Type::I256 => write!(w, "i256"), - Type::Compound(cmpd_ty) => cmpd_ty.write(ctx, w), + Type::Compound(cmpd_ty) => cmpd_ty.write(w, ctx), Type::Unit => write!(w, "unit"), } } @@ -224,41 +230,40 @@ impl WriteWithModule for Type { pub struct CompoundType(u32); cranelift_entity::entity_impl!(CompoundType); -impl WriteWithModule for CompoundType { - fn write(&self, ctx: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()> { - ctx.with_ty_store(|s| match s.resolve_compound(*self) { - CompoundTypeData::Array { elem: ty, len } => { - write!(w, "[")?; - ty.write(ctx, &mut *w)?; - write!(w, "; {len}]") - } - CompoundTypeData::Ptr(ty) => { - write!(w, "*")?; - ty.write(ctx, w) - } - CompoundTypeData::Struct(StructData { name, packed, .. }) => { - if *packed { - write!(w, "@<{name}>") - } else { - write!(w, "@{name}") +impl IrWrite for CompoundType +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + ctx.as_ref() + .with_ty_store(|s| match s.resolve_compound(*self) { + CompoundTypeData::Array { elem: ty, len } => { + write!(w, "[")?; + ty.write(w, ctx)?; + write!(w, "; {len}]") } - } - - CompoundTypeData::Func { args, ret_ty: ret } => { - write!(w, "(")?; - let mut args = args.iter(); - if let Some(arg_ty) = args.next() { - arg_ty.write(ctx, w)?; - }; - for arg_ty in args { - write!(w, ", ")?; - arg_ty.write(ctx, w)?; + CompoundTypeData::Ptr(ty) => { + write!(w, "*")?; + ty.write(w, ctx) + } + CompoundTypeData::Struct(StructData { name, packed, .. }) => { + if *packed { + write!(w, "@<{name}>") + } else { + write!(w, "@{name}") + } } - write!(w, ") -> ")?; - ret.write(ctx, w) - } - }) + CompoundTypeData::Func { args, ret_ty: ret } => { + write!(w, "(")?; + args.write_with_delim(w, ", ", ctx)?; + write!(w, ") -> ")?; + ret.write(w, ctx) + } + }) } } @@ -283,8 +288,14 @@ pub struct StructData { pub packed: bool, } -impl WriteWithModule for StructData { - fn write(&self, module: &ModuleCtx, w: &mut impl io::Write) -> io::Result<()> { +impl IrWrite for StructData +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { write!(w, "type @{} = ", self.name)?; if self.packed { write!(w, "<{{")?; @@ -292,13 +303,7 @@ impl WriteWithModule for StructData { write!(w, "{{")?; } - let mut delim = ""; - for &ty in &self.fields { - write!(w, "{delim}")?; - ty.write(module, &mut *w)?; - delim = ", "; - } - + self.fields.write_with_delim(w, ", ", ctx)?; if self.packed { write!(w, "}}>;") } else { diff --git a/crates/ir/src/value.rs b/crates/ir/src/value.rs index 2572348c..6d950b06 100644 --- a/crates/ir/src/value.rs +++ b/crates/ir/src/value.rs @@ -1,11 +1,12 @@ //! This module contains Sonatine IR value definition. use core::fmt; -use std::ops; +use std::{io, ops}; use super::Type; use crate::{ inst::InstId, - ir_writer::{WriteWithFunc, WriteWithModule}, + ir_writer::{FuncWriteCtx, IrWrite}, + module::ModuleCtx, GlobalVariable, I256, }; @@ -14,6 +15,39 @@ use crate::{ pub struct ValueId(pub u32); cranelift_entity::entity_impl!(ValueId); +impl IrWrite> for ValueId { + fn write(&self, w: &mut W, ctx: &FuncWriteCtx) -> io::Result<()> + where + W: io::Write, + { + if let Some(name) = ctx.dbg.value_name(ctx.func, ctx.func_ref, *self) { + write!(w, "{name}") + } else { + match ctx.func.dfg.value(*self) { + Value::Immediate { imm, ty } => { + write!(w, "{}.", imm)?; + ty.write(w, ctx) + } + + Value::Global { gv, .. } => ctx + .func + .dfg + .ctx + .with_gv_store(|s| write!(w, "${}", s.gv_data(*gv).symbol)), + + Value::Undef { ty } => { + write!(w, "undef.")?; + ty.write(w, ctx) + } + + Value::Arg { .. } | Value::Inst { .. } => { + write!(w, "v{}", self.0) + } + } + } + } +} + /// An value data definition. #[derive(Debug, Clone)] pub enum Value { @@ -46,40 +80,6 @@ pub enum Value { }, } -impl WriteWithFunc for ValueId { - fn write( - &self, - ctx: &crate::ir_writer::FuncWriteCtx, - w: &mut impl std::io::Write, - ) -> std::io::Result<()> { - if let Some(name) = ctx.dbg.value_name(ctx.func, ctx.func_ref, *self) { - write!(w, "{name}") - } else { - match ctx.func.dfg.value(*self) { - Value::Immediate { imm, ty } => { - write!(w, "{}.", imm)?; - ty.write(ctx.func.ctx(), w) - } - - Value::Global { gv, .. } => ctx - .func - .dfg - .ctx - .with_gv_store(|s| write!(w, "${}", s.gv_data(*gv).symbol)), - - Value::Undef { ty } => { - write!(w, "undef.")?; - ty.write(ctx.func.ctx(), w) - } - - Value::Arg { .. } | Value::Inst { .. } => { - write!(w, "v{}", self.0) - } - } - } - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Immediate { I1(bool), @@ -379,6 +379,32 @@ impl ops::Shr for Immediate { } } +impl IrWrite for Immediate +where + Ctx: AsRef, +{ + fn write(&self, w: &mut W, _ctx: &Ctx) -> io::Result<()> + where + W: io::Write, + { + match self { + Self::I1(v) => { + if *v { + write!(w, "1") + } else { + write!(w, "0") + } + } + Self::I8(v) => write!(w, "{}", v), + Self::I16(v) => write!(w, "{}", v), + Self::I32(v) => write!(w, "{}", v), + Self::I64(v) => write!(w, "{}", v), + Self::I128(v) => write!(w, "{}", v), + Self::I256(v) => write!(w, "{}", v), + } + } +} + impl fmt::Display for Immediate { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { diff --git a/crates/macros/src/inst.rs b/crates/macros/src/inst.rs index 47b2e76c..f2c3852a 100644 --- a/crates/macros/src/inst.rs +++ b/crates/macros/src/inst.rs @@ -57,11 +57,13 @@ impl InstStruct { let impl_inst = self.impl_inst(); let impl_inst_ext = self.impl_inst_ext(); let impl_inst_cast = self.impl_inst_cast(); + let impl_inst_write = self.impl_inst_write(); Ok(quote! { #impl_method #impl_inst_cast #impl_inst #impl_inst_ext + #impl_inst_write }) } @@ -278,6 +280,29 @@ impl InstStruct { } } + fn impl_inst_write(&self) -> proc_macro2::TokenStream { + let struct_name = &self.struct_name; + let fields = self.fields.iter().map(|f| { + let f = &f.ident; + quote!{ + if crate::ir_writer::IrWrite::::has_content(self.#f()) { + write!(&mut w, " ")?; + crate::ir_writer::IrWrite::write(self.#f(), &mut w, ctx)?; + } + } + }); + + quote! { + impl crate::inst::InstWrite for #struct_name { + fn write(&self, mut w: &mut dyn std::io::Write, ctx: &crate::ir_writer::FuncWriteCtx) -> std::io::Result<()> { + write!(w, "{}", crate::Inst::as_text(self))?; + #(#fields)* + Ok(()) + } + } + } + } + fn impl_inst_ext(&self) -> proc_macro2::TokenStream { let struct_name = &self.struct_name; let has_inst_method = ty_name_to_method_name(struct_name); diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 35c8b3ce..0f2b0090 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -8,7 +8,7 @@ use ir::{ builder::{FunctionBuilder, ModuleBuilder}, func_cursor::{CursorLocation, FuncCursor, InstInserter}, global_variable::GvInitializer, - ir_writer::{DebugProvider, WriteWithModule}, + ir_writer::{DebugProvider, IrWrite}, isa::evm::Evm, module::{FuncRef, Module, ModuleCtx}, Function, GlobalVariable, GlobalVariableData, Immediate, Signature, Type, diff --git a/crates/parser/src/syntax.rs b/crates/parser/src/syntax.rs index e72b2661..203e7bee 100644 --- a/crates/parser/src/syntax.rs +++ b/crates/parser/src/syntax.rs @@ -202,7 +202,7 @@ impl<'i, E> Node<'i, E> { } } -impl<'i, E> std::default::Default for Node<'i, E> { +impl std::default::Default for Node<'_, E> { fn default() -> Self { Self { rule: Rule::EOI, diff --git a/crates/parser/tests/syntax.rs b/crates/parser/tests/syntax.rs index e7bf712e..3440db22 100644 --- a/crates/parser/tests/syntax.rs +++ b/crates/parser/tests/syntax.rs @@ -66,7 +66,7 @@ fn report_error(err: pest::error::Error, fixture: &Fixture<&str>) { struct PairsWrapper<'i>(Pairs<'i, Rule>); -impl<'i> fmt::Debug for PairsWrapper<'i> { +impl fmt::Debug for PairsWrapper<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for pair in self.0.clone() { let rule = pair.as_rule(); diff --git a/crates/verifier/src/error/kind.rs b/crates/verifier/src/error/kind.rs index bae5ae54..cf119246 100644 --- a/crates/verifier/src/error/kind.rs +++ b/crates/verifier/src/error/kind.rs @@ -1,7 +1,7 @@ use std::fmt; use sonatina_ir::{ - ir_writer::{FuncWriteCtx, ValueWithTy, WriteWithFunc, WriteWithModule}, + ir_writer::{FuncWriteCtx, IrWrite, ValueWithTy}, module::FuncRef, BlockId, Function, InstId, Type, ValueId, }; @@ -67,7 +67,7 @@ impl<'a> DisplayErrorKind<'a> { } } -impl<'a> fmt::Display for DisplayErrorKind<'a> { +impl fmt::Display for DisplayErrorKind<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { use ErrorKind::*; match self.kind { diff --git a/crates/verifier/src/error/mod.rs b/crates/verifier/src/error/mod.rs index 4aec1734..85bd6fef 100644 --- a/crates/verifier/src/error/mod.rs +++ b/crates/verifier/src/error/mod.rs @@ -45,7 +45,7 @@ impl<'a> Error<'a> { } } -impl<'a> fmt::Display for Error<'a> { +impl fmt::Display for Error<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let ErrorData { kind, trace_info } = self.err; let kind = DisplayErrorKind::new(kind, self.func, self.func_ref); diff --git a/crates/verifier/src/error/trace_info.rs b/crates/verifier/src/error/trace_info.rs index 50ce2ad4..058b269d 100644 --- a/crates/verifier/src/error/trace_info.rs +++ b/crates/verifier/src/error/trace_info.rs @@ -2,7 +2,7 @@ use std::fmt; use cranelift_entity::packed_option::PackedOption; use sonatina_ir::{ - ir_writer::{FuncWriteCtx, InstStatement, ValueWithTy, WriteWithFunc, WriteWithModule}, + ir_writer::{FuncWriteCtx, InstStatement, IrWrite, ValueWithTy}, module::FuncRef, types::CompoundType, BlockId, Function, GlobalVariable, InstId, Type, ValueId, @@ -67,7 +67,7 @@ impl<'a, 'b> DisplayTraceInfo<'a, 'b> { } } -impl<'a, 'b> fmt::Display for DisplayTraceInfo<'a, 'b> { +impl fmt::Display for DisplayTraceInfo<'_, '_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let TraceInfo { block,