Skip to content

Commit

Permalink
Resolve lots of Clippy warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlamonster committed Oct 30, 2023
1 parent 782f6d5 commit fd0a294
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 86 deletions.
18 changes: 9 additions & 9 deletions compiler/src/interpreter/x86var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'p> X86Concluded<'p> {
block_ids,
read_buffer: Vec::new(),
write_buffer: Vec::new(),
status: Default::default(),
status: Status::default(),
stats: IStats::default(),
};

Expand Down Expand Up @@ -135,10 +135,10 @@ impl<'p, I: IO> X86Interpreter<'p, I> {
self.stats.instructions_executed += 1;
match instr {
Instr::Addq { src, dst } => {
self.set_arg(dst, self.get_arg(src) + self.get_arg(dst))
self.set_arg(dst, self.get_arg(src) + self.get_arg(dst));
}
Instr::Subq { src, dst } => {
self.set_arg(dst, self.get_arg(dst) - self.get_arg(src))
self.set_arg(dst, self.get_arg(dst) - self.get_arg(src));
}
Instr::Negq { dst } => self.set_arg(dst, -self.get_arg(dst)),
Instr::Movq { src, dst } => self.set_arg(dst, self.get_arg(src)),
Expand Down Expand Up @@ -181,8 +181,8 @@ impl<'p, I: IO> X86Interpreter<'p, I> {
Instr::Divq { divisor } => {
let rax = self.regs[&Reg::RAX];
let rdx = self.regs[&Reg::RDX];
let dividend = ((rdx as i128) << 64) | rax as i128;
let divisor = self.get_arg(divisor) as i128;
let dividend = (i128::from(rdx) << 64) | i128::from(rax);
let divisor = i128::from(self.get_arg(divisor));

self.regs.insert(Reg::RAX, (dividend / divisor) as i64);
self.regs.insert(Reg::RDX, (dividend % divisor) as i64);
Expand Down Expand Up @@ -226,16 +226,16 @@ impl<'p, I: IO> X86Interpreter<'p, I> {
}
}
Instr::Andq { src, dst } => {
self.set_arg(dst, self.get_arg(src) & self.get_arg(dst))
self.set_arg(dst, self.get_arg(src) & self.get_arg(dst));
}
Instr::Orq { src, dst } => self.set_arg(dst, self.get_arg(src) | self.get_arg(dst)),
Instr::Xorq { src, dst } => {
self.set_arg(dst, self.get_arg(src) ^ self.get_arg(dst))
self.set_arg(dst, self.get_arg(src) ^ self.get_arg(dst));
}
Instr::Notq { dst } => self.set_arg(dst, !self.get_arg(dst)),
Instr::Setcc { cnd } => {
let rax = self.regs[&Reg::RAX];
let cnd = self.evaluate_cnd(*cnd) as i64;
let cnd = i64::from(self.evaluate_cnd(*cnd));
self.regs.insert(Reg::RAX, rax & !0xFF | cnd);
}
Instr::LoadLbl { sym, dst } => {
Expand Down Expand Up @@ -288,7 +288,7 @@ impl<'p, I: IO> X86Interpreter<'p, I> {
Cnd::LE => self.status.zero || self.status.sign != self.status.overflow,
Cnd::NE => !self.status.zero,
Cnd::NotOverflow => !self.status.overflow,
Cnd::NotSign => self.status.sign,
Cnd::NotSign => !self.status.sign,
Cnd::Overflow => self.status.overflow,
Cnd::ParityEven => self.status.parity_even,
Cnd::ParityOdd => !self.status.parity_even,
Expand Down
8 changes: 2 additions & 6 deletions compiler/src/passes/atomize/atomize.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::passes::atomize::{AExpr, Atom, PrgAtomized};
use crate::passes::parse::{Def, Lit};
use crate::passes::parse::Def;
use crate::passes::reveal_functions::{PrgRevealed, RExpr};
use crate::utils::gen_sym::{gen_sym, UniqueSym};

Expand Down Expand Up @@ -89,11 +89,7 @@ fn atomize_expr(expr: RExpr) -> AExpr {
bdy: Box::new(atomize_expr(*bdy)),
},
RExpr::Break { bdy } => AExpr::Break {
bdy: bdy
.map(|bdy| Box::new(atomize_expr(*bdy)))
.unwrap_or(Box::new(AExpr::Atom {
atm: Atom::Val { val: Lit::Unit },
})),
bdy: Box::new(atomize_expr(*bdy)),
},
RExpr::Seq { stmt, cnt } => AExpr::Seq {
stmt: Box::new(atomize_expr(*stmt)),
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/atomize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<'p> From<AExpr<'p>> for Expr<UniqueSym<'p>> {
bdy: Box::new((*bdy).into()),
},
AExpr::Break { bdy } => Expr::Break {
bdy: Some(Box::new((*bdy).into())),
bdy: Box::new((*bdy).into()),
},
AExpr::Seq { stmt, cnt } => Expr::Seq {
stmt: Box::new((*stmt).into()),
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/passes/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fn emit_instr<'p>(
}
Instr::Jcc { lbl, cnd } => {
rel_jumps.insert(machine_code.len() + 2, *lbl);
vec![0x0F, encode_cnd(cnd), 0x00, 0x00, 0x00, 0x00]
vec![0x0F, encode_cnd(*cnd), 0x00, 0x00, 0x00, 0x00]
}
Instr::Retq => vec![0xC3],
Instr::Syscall { .. } => vec![0x0F, 0x05],
Expand Down Expand Up @@ -137,7 +137,7 @@ fn encode_reg(reg: &Reg) -> (u8, u8) {
}
}

fn encode_cnd(cnd: &Cnd) -> u8 {
fn encode_cnd(cnd: Cnd) -> u8 {
match cnd {
Cnd::Above => 0x87,
Cnd::AboveOrEqual | Cnd::NotCarry => 0x83,
Expand Down
1 change: 1 addition & 0 deletions compiler/src/passes/explicate/explicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct Env<'a, 'p> {
}

impl<'p> PrgAtomized<'p> {
#[must_use]
pub fn explicate(self) -> PrgExplicated<'p> {
let mut blocks = HashMap::new();
let mut env = Env {
Expand Down
23 changes: 12 additions & 11 deletions compiler/src/passes/explicate/interpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ impl<'p> PrgExplicated<'p> {
}
}

pub fn interpret_atom(
&self,
atom: &Atom<'p>,
scope: &PushMap<UniqueSym<'p>, Val<UniqueSym<'p>>>,
) -> Val<UniqueSym<'p>> {
match atom {
Atom::Val { val } => (*val).into(),
Atom::Var { sym } => scope[sym],
}
}

pub fn interpret_expr(
&self,
expr: &CExpr<'p>,
Expand Down Expand Up @@ -159,4 +148,16 @@ impl<'p> PrgExplicated<'p> {
}
}
}

#[must_use]
pub fn interpret_atom(
&self,
atom: &Atom<'p>,
scope: &PushMap<UniqueSym<'p>, Val<UniqueSym<'p>>>,
) -> Val<UniqueSym<'p>> {
match atom {
Atom::Val { val } => (*val).into(),
Atom::Var { sym } => scope[sym],
}
}
}
6 changes: 1 addition & 5 deletions compiler/src/passes/interference/assign_homes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//! This pass compiles `X86VarProgram`s into `AX86Program`.
//!
//! This pass is responsible for assigning all the program variables to locations on the stack.

use crate::language::x86var::{Arg, Block, Instr, VarArg, X86Assigned, X86Colored};
use crate::utils::gen_sym::UniqueSym;
use crate::{
Expand All @@ -11,7 +7,7 @@ use crate::{
use std::collections::HashMap;

impl<'p> X86Colored<'p> {
/// See module-level documentation.
#[must_use]
pub fn assign_homes(self) -> X86Assigned<'p> {
X86Assigned {
blocks: self
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/passes/interference/coloring_interference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use itertools::Itertools;
use std::collections::{HashMap, HashSet};

impl<'p> X86WithInterference<'p> {
#[must_use]
pub fn color_interference(self) -> X86Colored<'p> {
let (color_map, stack_space) = color_graph(self.interference);

Expand All @@ -17,6 +18,7 @@ impl<'p> X86WithInterference<'p> {
}
}

#[must_use]
fn color_graph(graph: InterferenceGraph) -> (HashMap<UniqueSym, Arg>, usize) {
let mut queue = Vec::new();
let mut node_map = HashMap::<LArg, isize>::new();
Expand Down
1 change: 1 addition & 0 deletions compiler/src/passes/interference/compute_interference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::passes::interference::liveness_analysis::{handle_instr, ReadWriteOp};
use std::collections::HashMap;

impl<'p> LX86VarProgram<'p> {
#[must_use]
pub fn compute_interference(self) -> X86WithInterference<'p> {
X86WithInterference {
interference: self.build_graph(),
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/passes/interference/liveness_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};

impl<'p> X86Selected<'p> {
#[must_use]
pub fn add_liveness(self) -> LX86VarProgram<'p> {
// let graph = create_graph(&self.blocks);

Expand Down Expand Up @@ -40,12 +41,12 @@ impl<'p> X86Selected<'p> {
match liveness.get(sym) {
None => {
liveness.insert(*sym, new_liveness);
changed = true
changed = true;
}
Some(old_liveness) => {
if *old_liveness != new_liveness {
liveness.insert(*sym, new_liveness);
changed = true
changed = true;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/passes/parse/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::str::FromStr;
use crate::passes::parse::*;
use crate::passes::parse::{Def, Expr, Lit, Op, Param}
use crate::passes::parse::PrgParsed;
use crate::passes::type_check::Type;

Expand Down Expand Up @@ -164,13 +164,13 @@ ExprInStmt: Expr<&'input str> = {
cnd: Box::new(cnd),
thn: Box::new(bdy),
els: Box::new(Expr::Seq {
stmt: Box::new(Expr::Break { bdy: None }),
stmt: Box::new(Expr::Break { bdy: Box::new(Expr::Lit { val: Lit::Unit }) }),
cnt: Box::new(Expr::Lit { val: Lit::Unit }),
}),
}),
},
"break" <bdy:ExprLogicalOr?> => Expr::Break {
bdy: bdy.map(Box::new),
bdy: Box::new(bdy.unwrap_or(Expr::Lit { val: Lit::Unit })),
},
ExprLogicalOr,
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/passes/parse/grammar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// auto-generated: "lalrpop 0.20.1"
// sha3: 19eaac2958ee7627671379b88ee4fd1bbd2700d0b51bbf42e8cfb929dab502b9
// sha3: c9e9fedb1e07d89e4fbaef0611beb45686afea48b8df51c64ffcd5ddf109995e
use std::str::FromStr;
use crate::passes::parse::*;
use crate::passes::parse::PrgParsed;
Expand Down Expand Up @@ -4597,7 +4597,7 @@ fn __action18<
cnd: Box::new(cnd),
thn: Box::new(bdy),
els: Box::new(Expr::Seq {
stmt: Box::new(Expr::Break { bdy: None }),
stmt: Box::new(Expr::Break { bdy: Box::new(Expr::Lit { val: Lit::Unit }) }),
cnt: Box::new(Expr::Lit { val: Lit::Unit }),
}),
}),
Expand All @@ -4615,7 +4615,7 @@ fn __action19<
) -> Expr<&'input str>
{
Expr::Break {
bdy: bdy.map(Box::new),
bdy: Box::new(bdy.unwrap_or(Expr::Lit { val: Lit::Unit })),
}
}

Expand Down
5 changes: 1 addition & 4 deletions compiler/src/passes/parse/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ impl<A: Copy + Hash + Eq + Debug + Display> PrgGenericVar<A> {
}
},
Expr::Break { bdy } => {
return ControlFlow::Break(match bdy {
Some(bdy) => b!(self.interpret_expr(bdy, scope, io)),
None => Val::Unit,
})
return ControlFlow::Break(b!(self.interpret_expr(bdy, scope, io)))
}
Expr::Seq { stmt, cnt } => {
b!(self.interpret_expr(stmt, scope, io));
Expand Down
22 changes: 13 additions & 9 deletions compiler/src/passes/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[rustfmt::skip]
#[allow(clippy::all)]
#[allow(clippy::all, clippy::pedantic)]
mod grammar;
pub mod interpreter;
pub mod parse;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub enum Expr<A: Copy + Hash + Eq> {
bdy: Box<Expr<A>>,
},
Break {
bdy: Option<Box<Expr<A>>>,
bdy: Box<Expr<A>>,
},
Seq {
stmt: Box<Expr<A>>,
Expand Down Expand Up @@ -113,17 +113,21 @@ pub enum Lit {
}

impl Lit {
#[must_use]
pub fn int(self) -> i64 {
match self {
Lit::Int { val } => val,
_ => panic!(),
if let Lit::Int { val } = self {
val
} else {
panic!()
}
}

#[must_use]
pub fn bool(self) -> bool {
match self {
Lit::Bool { val } => val,
_ => panic!(),
if let Lit::Bool { val } = self {
val
} else {
panic!()
}
}
}
Expand All @@ -134,7 +138,7 @@ mod tests {
use test_each_file::test_each_file;

fn parse([test]: [&str; 1]) {
split_test(test);
let _ = split_test(test);
}

test_each_file! { for ["test"] in "./programs/good" as parse => parse }
Expand Down
6 changes: 1 addition & 5 deletions compiler/src/passes/patch_instructions/patch_instructions.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
//! This pass compiles `AX86Program`s into `PX86Program`.
//!
//! This pass makes sure that no instructions use more than one argument that is dereferenced.

use crate::language::x86var::{Arg, Block, Instr, X86Assigned, X86Patched};
use crate::{addq, movq, reg, subq};

impl<'p> X86Assigned<'p> {
/// See module-level documentation.
#[must_use]
pub fn patch(self) -> X86Patched<'p> {
X86Patched {
blocks: self
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/passes/reveal_functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum RExpr<'p> {
bdy: Box<RExpr<'p>>,
},
Break {
bdy: Option<Box<RExpr<'p>>>,
bdy: Box<RExpr<'p>>,
},
Seq {
stmt: Box<RExpr<'p>>,
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<'p> From<RExpr<'p>> for Expr<UniqueSym<'p>> {
bdy: Box::new((*bdy).into()),
},
RExpr::Break { bdy } => Expr::Break {
bdy: bdy.map(|bdy| Box::new((*bdy).into())),
bdy: Box::new((*bdy).into()),
},
RExpr::Seq { stmt, cnt } => Expr::Seq {
stmt: Box::new((*stmt).into()),
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/passes/reveal_functions/reveal_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::utils::gen_sym::UniqueSym;
use crate::utils::push_map::PushMap;

impl<'p> PrgUniquified<'p> {
#[must_use]
pub fn reveal(self) -> PrgRevealed<'p> {
let mut scope = PushMap::from_iter(self.defs.keys().map(|s| (*s, ())));

Expand Down Expand Up @@ -76,7 +77,7 @@ fn reveal_expr<'p>(expr: Expr<UniqueSym<'p>>, scope: &mut PushMap<UniqueSym<'p>,
bdy: Box::new(reveal_expr(*bdy, scope)),
},
Expr::Break { bdy } => RExpr::Break {
bdy: bdy.map(|bdy| Box::new(reveal_expr(*bdy, scope))),
bdy: Box::new(reveal_expr(*bdy, scope)),
},
Expr::Seq { stmt, cnt } => RExpr::Seq {
stmt: Box::new(reveal_expr(*stmt, scope)),
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/select/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct Std<'p> {
pub exit: UniqueSym<'p>,
pub print_int: UniqueSym<'p>,
Expand Down
Loading

0 comments on commit fd0a294

Please sign in to comment.