Skip to content

Commit

Permalink
Remove a lot of derived traits.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlamonster committed Nov 10, 2023
1 parent 86b0d28 commit ab01208
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 63 deletions.
2 changes: 1 addition & 1 deletion compiler/src/passes/assign/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn assign_instr<'p>(
VarArg::Imm { val } => Arg::Imm { val },
VarArg::Reg { reg } => Arg::Reg { reg },
VarArg::Deref { reg, off } => Arg::Deref { reg, off },
VarArg::XVar { sym } => color_map[&sym],
VarArg::XVar { sym } => color_map[&sym].clone(),
}
};

Expand Down
7 changes: 3 additions & 4 deletions compiler/src/passes/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ use petgraph::graphmap::GraphMap;
use petgraph::Undirected;
use std::collections::{HashMap, HashSet};

#[derive(Debug, PartialEq)]
pub struct X86Assigned<'p> {
pub blocks: HashMap<UniqueSym<'p>, Block<'p, Arg>>,
pub entry: UniqueSym<'p>,
pub stack_space: usize,
pub std: Std<'p>,
}

#[derive(Debug, PartialEq, Copy, Clone, Hash, Eq, Display)]
#[derive(Clone, Display)]
pub enum Arg {
#[display(fmt = "${val}")]
Imm { val: i64 },
Expand All @@ -40,12 +39,12 @@ pub struct LX86VarProgram<'p> {
pub std: Std<'p>,
}

#[derive(Debug, PartialEq)]
#[derive(PartialEq)]
pub struct LBlock<'p> {
pub instrs: Vec<(Instr<'p, VarArg<'p>>, HashSet<LArg<'p>>)>,
}

#[derive(Debug, PartialEq, Clone, Copy, Hash, Eq, Ord, PartialOrd)]
#[derive(Hash, Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
pub enum LArg<'p> {
Var { sym: UniqueSym<'p> },
Reg { reg: Reg },
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/passes/atomize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ use crate::passes::validate::{TExpr, TLit};
use crate::utils::gen_sym::UniqueSym;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub struct PrgAtomized<'p> {
pub defs: HashMap<UniqueSym<'p>, Def<UniqueSym<'p>, &'p str, AExpr<'p>>>,
pub entry: UniqueSym<'p>,
}

#[derive(Debug, PartialEq)]
pub enum AExpr<'p> {
Atom {
atm: Atom<'p>,
Expand Down Expand Up @@ -103,7 +101,7 @@ impl<'p> AExpr<'p> {
}
}

#[derive(Debug, PartialEq, Copy, Clone)]
#[derive(Copy, Clone)]
pub enum Atom<'p> {
Val { val: TLit },
Var { sym: UniqueSym<'p> },
Expand Down
1 change: 0 additions & 1 deletion compiler/src/passes/conclude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub mod conclude;
#[cfg(test)]
mod tests;

#[derive(Debug, PartialEq)]
pub struct X86Concluded<'p> {
pub blocks: HashMap<UniqueSym<'p>, Block<'p, Arg>>,
pub entry: UniqueSym<'p>,
Expand Down
3 changes: 0 additions & 3 deletions compiler/src/passes/eliminate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ use crate::passes::parse::{Op, Param, TypeDef};
use crate::utils::gen_sym::UniqueSym;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub struct PrgEliminated<'p> {
pub blocks: HashMap<UniqueSym<'p>, ETail<'p>>,
pub fn_params: HashMap<UniqueSym<'p>, Vec<Param<UniqueSym<'p>>>>,
pub defs: HashMap<UniqueSym<'p>, TypeDef<UniqueSym<'p>, &'p str>>,
pub entry: UniqueSym<'p>,
}

#[derive(Debug, PartialEq)]
pub enum ETail<'p> {
Return {
exprs: Vec<(Atom<'p>, Type<UniqueSym<'p>>)>,
Expand All @@ -38,7 +36,6 @@ pub enum ETail<'p> {
},
}

#[derive(Clone, Debug, PartialEq)]
pub enum EExpr<'p> {
Atom {
atm: Atom<'p>,
Expand Down
3 changes: 0 additions & 3 deletions compiler/src/passes/explicate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ use crate::passes::parse::{Op, Param, TypeDef};
use crate::utils::gen_sym::UniqueSym;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub struct PrgExplicated<'p> {
pub blocks: HashMap<UniqueSym<'p>, CTail<'p>>,
pub fn_params: HashMap<UniqueSym<'p>, Vec<Param<UniqueSym<'p>>>>,
pub defs: HashMap<UniqueSym<'p>, TypeDef<UniqueSym<'p>, &'p str>>,
pub entry: UniqueSym<'p>,
}

#[derive(Debug, PartialEq)]
pub enum CTail<'p> {
Return {
expr: Atom<'p>,
Expand All @@ -38,7 +36,6 @@ pub enum CTail<'p> {
},
}

#[derive(Clone, Debug, PartialEq)]
pub enum CExpr<'p> {
Atom {
atm: Atom<'p>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/passes/parse/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ Struct: Expr<'input, Spanned<&'input str>, Spanned<&'input str>> = {

StructArg : (Spanned<&'input str>, Spanned<Expr<'input, Spanned<&'input str>, Spanned<&'input str>>>) = {
<Ident> ":" <Spanned<Expr>>,
<l:@L> <sym:Ident> <r:@R> => (sym, Spanned { span: (l, r), inner: Expr::Var { sym } })
<l:@L> <sym:Ident> <r:@R> => (sym.clone(), Spanned { span: (l, r), inner: Expr::Var { sym } })
}

Never: Expr<'input, Spanned<&'input str>, Spanned<&'input str>> = {};
Expand Down
24 changes: 8 additions & 16 deletions compiler/src/passes/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ use crate::utils::gen_sym::UniqueSym;
use derive_more::Display;
use functor_derive::Functor;
use std::fmt::{Display, Formatter};
use std::hash::Hash;
use types::Type;

/// A parsed program with global definitions and an entry point.
#[derive(Debug, PartialEq)]
pub struct PrgParsed<'p> {
/// The global program definitions.
pub defs: Vec<DefParsed<'p>>,
Expand All @@ -27,8 +25,7 @@ pub struct PrgParsed<'p> {
}

/// A definition.
#[derive(Debug, PartialEq)]
pub enum Def<IdentVars: Copy + Hash + Eq + Display, IdentFields: Copy + Hash + Eq + Display, Expr> {
pub enum Def<IdentVars: Display, IdentFields: Display, Expr> {
/// A function definition.
Fn {
/// Symbol representing the function.
Expand All @@ -53,8 +50,7 @@ pub type DefUniquified<'p> =
Def<Spanned<UniqueSym<'p>>, Spanned<&'p str>, Spanned<ExprUniquified<'p>>>;
pub type ExprUniquified<'p> = Expr<'p, Spanned<UniqueSym<'p>>, Spanned<&'p str>>;

#[derive(Clone, Debug, PartialEq)]
pub enum TypeDef<A: Copy + Hash + Eq + Display, A2: Copy + Hash + Eq + Display> {
pub enum TypeDef<A: Display, A2: Display> {
/// A struct definition.
Struct {
/// Fields of the struct, consisting of field symbols and their types.
Expand All @@ -67,9 +63,7 @@ pub enum TypeDef<A: Copy + Hash + Eq + Display, A2: Copy + Hash + Eq + Display>
},
}

impl<IdentVars: Copy + Hash + Eq + Display, IdentFields: Copy + Hash + Eq + Display, Expr>
Def<IdentVars, IdentFields, Expr>
{
impl<IdentVars: Display, IdentFields: Display, Expr> Def<IdentVars, IdentFields, Expr> {
/// Returns the symbol representing the definition.
pub fn sym(&self) -> &IdentVars {
match self {
Expand All @@ -83,8 +77,8 @@ impl<IdentVars: Copy + Hash + Eq + Display, IdentFields: Copy + Hash + Eq + Disp
///
/// Parameters are generic and can use symbols that are either `&str` or
/// [`UniqueSym`](crate::utils::gen_sym::UniqueSym) for all passes after uniquify.
#[derive(Debug, PartialEq, Clone)]
pub struct Param<A: Copy + Hash + Eq + Display> {
#[derive(Clone)]
pub struct Param<A: Display> {
/// Symbol representing the parameter.
pub sym: A,
/// The type of the parameter. See [`Type`]
Expand All @@ -97,8 +91,7 @@ pub struct Param<A: Copy + Hash + Eq + Display> {
///
/// Expressions are generic and can use symbols that are either `&str` or
/// [`UniqueSym`](crate::utils::gen_sym::UniqueSym) for all passes after uniquify.
#[derive(Debug, PartialEq)]
pub enum Expr<'p, IdentVars: Copy + Hash + Eq + Display, IdentFields: Copy + Hash + Eq + Display> {
pub enum Expr<'p, IdentVars: Display, IdentFields: Display> {
/// A literal value. See [`Lit`].
Lit {
/// Value of the literal. See [`Lit`].
Expand Down Expand Up @@ -240,7 +233,7 @@ pub type SwitchArm<'p, IdentVars, IdentFields> = (
Box<Spanned<Expr<'p, IdentVars, IdentFields>>>,
);

#[derive(Debug, PartialEq, Functor, Hash, Eq, Copy, Clone)]
#[derive(Functor, Clone)]
pub struct Spanned<T> {
pub span: (usize, usize),
pub inner: T,
Expand All @@ -253,7 +246,6 @@ impl<T: Display> Display for Spanned<T> {
}

/// A primitive operation.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Op {
/// Read signed integer from stdin.
Read,
Expand Down Expand Up @@ -292,7 +284,7 @@ pub enum Op {
}

/// A literal value.
#[derive(Copy, Clone, Debug, PartialEq, Display)]
#[derive(Display)]
pub enum Lit<'p> {
/// Integer literal, representing a signed 64-bit number.
#[display(fmt = "{val}")]
Expand Down
14 changes: 5 additions & 9 deletions compiler/src/passes/parse/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use derive_more::Display;
use functor_derive::Functor;
use itertools::Itertools;
use std::fmt::Display;
use std::hash::Hash;

#[derive(Debug, Clone, PartialEq, Display)]
pub enum Type<A: Hash + Eq + Display> {
#[derive(Debug, Clone, Display)]
pub enum Type<A: Display> {
#[display(fmt = "Int")]
Int,
#[display(fmt = "Bool")]
Expand All @@ -23,12 +22,9 @@ pub enum Type<A: Hash + Eq + Display> {
Var { sym: A },
}

impl<A: Hash + Eq + Display> Type<A> {
pub fn fmap<__B: Hash + Eq + Display>(self, __f: impl Fn(A) -> __B) -> Type<__B> {
fn fmap_ref<A: Hash + Eq + Display, __B: Hash + Eq + Display>(
s: Type<A>,
__f: &impl Fn(A) -> __B,
) -> Type<__B> {
impl<A: Display> Type<A> {
pub fn fmap<__B: Display>(self, __f: impl Fn(A) -> __B) -> Type<__B> {
fn fmap_ref<A: Display, __B: Display>(s: Type<A>, __f: &impl Fn(A) -> __B) -> Type<__B> {
match s {
Type::Int => Type::Int,
Type::Bool => Type::Bool,
Expand Down
1 change: 0 additions & 1 deletion compiler/src/passes/patch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::utils::gen_sym::UniqueSym;
use functor_derive::Functor;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub struct X86Patched<'p> {
pub blocks: HashMap<UniqueSym<'p>, Block<'p, Arg>>,
pub entry: UniqueSym<'p>,
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/passes/reveal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ use crate::passes::validate::{TExpr, TLit};
use crate::utils::gen_sym::UniqueSym;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub struct PrgRevealed<'p> {
pub defs: HashMap<UniqueSym<'p>, Def<UniqueSym<'p>, &'p str, RExpr<'p>>>,
pub entry: UniqueSym<'p>,
}

#[derive(Debug, PartialEq)]
pub enum RExpr<'p> {
Lit {
val: TLit,
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/passes/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use itertools::Itertools;
use std::collections::HashMap;
use std::fmt::Display;

#[derive(Debug, PartialEq, Display)]
#[derive(Display)]
#[display(
fmt = "{}",
r#"blocks.iter().map(|(sym, block)| format!("{sym}:\n{block}")).format("\n")"#
Expand All @@ -24,13 +24,13 @@ pub struct X86Selected<'p> {
pub std: Std<'p>,
}

#[derive(Debug, PartialEq, Clone, Display, Functor)]
#[derive(Clone, Display, Functor)]
#[display(fmt = "\t{}", r#"instrs.iter().format("\n\t")"#)]
pub struct Block<'p, A: Display> {
pub instrs: Vec<Instr<'p, A>>,
}

#[derive(Copy, Clone, Debug, PartialEq, Display)]
#[derive(Copy, Clone, PartialEq, Display)]
pub enum Cnd {
Above,
AboveOrEqual,
Expand All @@ -52,7 +52,7 @@ pub enum Cnd {
Sign,
}

#[derive(Clone, Debug, PartialEq, Display, Functor)]
#[derive(Clone, PartialEq, Display, Functor)]
pub enum Instr<'p, A: Display> {
#[display(fmt = "addq\t{src}\t{dst}")]
Addq { src: A, dst: A },
Expand Down Expand Up @@ -98,7 +98,7 @@ pub enum Instr<'p, A: Display> {
CallqIndirect { src: A, arity: usize },
}

#[derive(Debug, PartialEq, Clone, Copy, Hash, Eq, Display)]
#[derive(PartialEq, Clone, Display)]
pub enum VarArg<'p> {
#[display(fmt = "${val}")]
Imm { val: i64 },
Expand Down Expand Up @@ -142,7 +142,7 @@ pub const SYSCALL_REGS: [Reg; 7] = [
Reg::R9,
];

#[derive(Debug, PartialEq, Clone, Copy, Eq, Hash, Ord, PartialOrd, Display)]
#[derive(Hash, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Display)]
#[allow(clippy::upper_case_acronyms)]
pub enum Reg {
RSP,
Expand Down
29 changes: 22 additions & 7 deletions compiler/src/passes/select/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ fn select_assign<'p>(
..
} => vec![movq!(var!(sym), dst)],
EExpr::Prim { op, args, .. } => match (op, args.as_slice()) {
(Op::Plus, [a0, a1]) => vec![movq!(select_atom(a0), dst), addq!(select_atom(a1), dst)],
(Op::Minus, [a0, a1]) => vec![movq!(select_atom(a0), dst), subq!(select_atom(a1), dst)],
(Op::Minus, [a0]) => vec![movq!(select_atom(a0), dst), negq!(dst)],
(Op::Plus, [a0, a1]) => vec![
movq!(select_atom(a0), dst.clone()),
addq!(select_atom(a1), dst),
],
(Op::Minus, [a0, a1]) => vec![
movq!(select_atom(a0), dst.clone()),
subq!(select_atom(a1), dst),
],
(Op::Minus, [a0]) => vec![movq!(select_atom(a0), dst.clone()), negq!(dst)],
(Op::Mul, [a0, a1]) => vec![
movq!(select_atom(a1), reg!(RAX)),
movq!(select_atom(a0), reg!(RBX)),
Expand Down Expand Up @@ -150,10 +156,19 @@ fn select_assign<'p>(
callq_direct!(std.print_int, 1),
movq!(select_atom(a0), dst),
],
(Op::LAnd, [a0, a1]) => vec![movq!(select_atom(a0), dst), andq!(select_atom(a1), dst)],
(Op::LOr, [a0, a1]) => vec![movq!(select_atom(a0), dst), orq!(select_atom(a1), dst)],
(Op::Not, [a0]) => vec![movq!(select_atom(a0), dst), xorq!(imm!(1), dst)],
(Op::Xor, [a0, a1]) => vec![movq!(select_atom(a0), dst), xorq!(select_atom(a1), dst)],
(Op::LAnd, [a0, a1]) => vec![
movq!(select_atom(a0), dst.clone()),
andq!(select_atom(a1), dst),
],
(Op::LOr, [a0, a1]) => vec![
movq!(select_atom(a0), dst.clone()),
orq!(select_atom(a1), dst),
],
(Op::Not, [a0]) => vec![movq!(select_atom(a0), dst.clone()), xorq!(imm!(1), dst)],
(Op::Xor, [a0, a1]) => vec![
movq!(select_atom(a0), dst.clone()),
xorq!(select_atom(a1), dst),
],
(op @ (Op::GT | Op::GE | Op::EQ | Op::LE | Op::LT | Op::NE), [a0, a1]) => {
let tmp = gen_sym("tmp");
vec![
Expand Down
2 changes: 0 additions & 2 deletions compiler/src/passes/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ use derive_more::Display;
use std::collections::HashMap;
use std::str::FromStr;

#[derive(Debug, PartialEq)]
pub struct PrgValidated<'p> {
pub defs: HashMap<&'p str, Def<UniqueSym<'p>, &'p str, TExpr<'p>>>,
pub entry: &'p str,
}

#[derive(Debug, PartialEq)]
pub enum TExpr<'p> {
Lit {
val: TLit,
Expand Down
Loading

0 comments on commit ab01208

Please sign in to comment.