Skip to content

Commit

Permalink
Remove unnecesary Display impls
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Nov 28, 2024
1 parent 1377812 commit 59b520a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 61 deletions.
16 changes: 5 additions & 11 deletions crates/ir/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,11 @@ where
where
W: io::Write,
{
let Signature {
name,
linkage,
args,
ret_ty,
} = self;

write!(w, "func {linkage} %{name}(")?;
args.write_with_delim(w, " ", ctx)?;

write!(w, "func ")?;
self.linkage.write(w, ctx)?;
write!(w, " %{}(", self.name)?;
self.args.write_with_delim(w, " ", ctx)?;
write!(w, ") -> ")?;
ret_ty.write(w, ctx)
self.ret_ty.write(w, ctx)
}
}
38 changes: 5 additions & 33 deletions crates/ir/src/global_variable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, io};
use std::io;

use cranelift_entity::PrimaryMap;
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -168,9 +168,9 @@ impl<Ctx> IrWrite<Ctx> for GvInitializer
where
Ctx: AsRef<ModuleCtx>,
{
fn write<W>(&self, w: &mut W, _ctx: &Ctx) -> io::Result<()>
fn write<W>(&self, w: &mut W, ctx: &Ctx) -> io::Result<()>
where
W: io::Write + ?Sized,
W: io::Write,
{
match self {
Self::Immediate(data) => write!(w, "{}", data),
Expand All @@ -180,7 +180,7 @@ where
if i > 0 {
write!(w, ", ")?;
}
write!(w, "{}", v)?;
v.write(w, ctx)?;
}
write!(w, "]")
}
Expand All @@ -190,42 +190,14 @@ where
if i > 0 {
write!(w, ", ")?;
}
write!(w, "{}", v)?;
v.write(w, ctx)?;
}
write!(w, "}}")
}
}
}
}

impl fmt::Display for GvInitializer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Immediate(data) => write!(f, "{}", data),
Self::Array(data) => {
write!(f, "[")?;
for (i, v) in data.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", v)?;
}
write!(f, "]")
}
Self::Struct(data) => {
write!(f, "{{")?;
for (i, v) in data.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", v)?;
}
write!(f, "}}")
}
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
11 changes: 5 additions & 6 deletions crates/ir/src/ir_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ 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()
))?;
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
Expand All @@ -148,7 +147,7 @@ impl<'a> FuncWriter<'a> {
arg_values.write_with_delim(w, ", ", &self.ctx)?;

write!(w, ") -> ")?;
self.ctx.func.sig.ret_ty().write(w, &self.ctx)?;
func.sig.ret_ty().write(w, &self.ctx)?;
writeln!(w, " {{")?;

self.level += 1;
Expand Down
13 changes: 2 additions & 11 deletions crates/ir/src/linkage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt, io, str::FromStr};
use std::{io, str::FromStr};

use crate::{ir_writer::IrWrite, module::ModuleCtx};

Expand All @@ -17,6 +17,7 @@ pub enum Linkage {
/// The symbol is defined outside of the module.
External,
}

impl<Ctx> IrWrite<Ctx> for Linkage
where
Ctx: AsRef<ModuleCtx>,
Expand All @@ -33,16 +34,6 @@ where
}
}

impl fmt::Display for Linkage {
fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result {
match self {
Self::Public => write!(f, "public"),
Self::Private => write!(f, "private"),
Self::External => write!(f, "external"),
}
}
}

impl FromStr for Linkage {
type Err = ();

Expand Down
27 changes: 27 additions & 0 deletions crates/ir/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::Type;
use crate::{
inst::InstId,
ir_writer::{FuncWriteCtx, IrWrite},
module::ModuleCtx,
GlobalVariable, I256,
};

Expand Down Expand Up @@ -378,6 +379,32 @@ impl ops::Shr for Immediate {
}
}

impl<Ctx> IrWrite<Ctx> for Immediate
where
Ctx: AsRef<ModuleCtx>,
{
fn write<W>(&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 {
Expand Down

0 comments on commit 59b520a

Please sign in to comment.