Skip to content

Commit

Permalink
Make integer literals generic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlamonster committed Dec 5, 2023
1 parent 3730bb9 commit 8ee8c2c
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 98 deletions.
6 changes: 3 additions & 3 deletions compiler/src/passes/atomize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ pub mod atomize;
mod display;

use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Def, Typed, UnaryOp};
use crate::passes::parse::{BinaryOp, Def, Lit, Typed, UnaryOp};
use crate::passes::select::InstrSelected;
use crate::passes::validate::TLit;
use crate::passes::validate::Int;
use crate::utils::gen_sym::UniqueSym;
use derive_more::Display;
use itertools::Itertools;
Expand Down Expand Up @@ -81,7 +81,7 @@ pub enum AExpr<'p> {

#[derive(Copy, Clone, Display)]
pub enum Atom<'p> {
Val { val: TLit },
Val { val: Lit<Int> },
Var { sym: UniqueSym<'p> },
}

Expand Down
5 changes: 2 additions & 3 deletions compiler/src/passes/explicate/explicate_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::passes::atomize::{AExpr, Atom};
use crate::passes::explicate::explicate::Env;
use crate::passes::explicate::{explicate_pred, ExprExplicated, TailExplicated};

use crate::passes::parse::{Meta, Typed};
use crate::passes::validate::TLit;
use crate::passes::parse::{Lit, Meta, Typed};
use crate::utils::gen_sym::{gen_sym, UniqueSym};

pub fn explicate_assign<'p>(
Expand Down Expand Up @@ -119,7 +118,7 @@ pub fn explicate_assign<'p>(
Meta {
meta: bnd.meta,
inner: AExpr::Atom {
atm: Atom::Val { val: TLit::Unit },
atm: Atom::Val { val: Lit::Unit },
},
},
tail,
Expand Down
9 changes: 4 additions & 5 deletions compiler/src/passes/explicate/explicate_pred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::passes::explicate::explicate::Env;
use crate::passes::explicate::explicate_assign::explicate_assign;
use crate::passes::explicate::{ExprExplicated, TailExplicated};
use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Meta, UnaryOp};
use crate::passes::validate::TLit;
use crate::passes::parse::{BinaryOp, Lit, Meta, UnaryOp};
use crate::utils::gen_sym::gen_sym;

pub fn explicate_pred<'p>(
Expand All @@ -29,7 +28,7 @@ pub fn explicate_pred<'p>(
exprs: [
Atom::Var { sym },
Atom::Val {
val: TLit::Bool(true),
val: Lit::Bool(true),
},
],
},
Expand All @@ -38,7 +37,7 @@ pub fn explicate_pred<'p>(
},
AExpr::Atom {
atm: Atom::Val {
val: TLit::Bool(val),
val: Lit::Bool(val),
},
..
} => {
Expand Down Expand Up @@ -179,7 +178,7 @@ pub fn explicate_pred<'p>(
AExpr::FunRef { .. }
| AExpr::Atom {
atm: Atom::Val {
val: TLit::Int { .. } | TLit::Unit,
val: Lit::Int { .. } | Lit::Unit,
},
..
}
Expand Down
10 changes: 4 additions & 6 deletions compiler/src/passes/parse/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::passes::parse::{
BinaryOp, DefParsed, ExprParsed, InstrParsed, Lit, Meta, Param, PrgParsed, Spanned, Type, TypeDef, UnaryOp, types::Int,
BinaryOp, DefParsed, ExprParsed, InstrParsed, Lit, Meta, Param, PrgParsed, Spanned, Type, TypeDef, UnaryOp, types::IntType,
};
use crate::passes::select::{VarArg, Reg, Imm};
use functor_derive::Functor;
Expand Down Expand Up @@ -159,8 +159,8 @@ Param: Param<Spanned<&'input str>> = {
}

Type: Type<Spanned<&'input str>> = {
"I64" => Type::Int(Int::I64),
"U64" => Type::Int(Int::I64),
"I64" => Type::Int(IntType::I64),
"U64" => Type::Int(IntType::U64),
"Bool" => Type::Bool,
"Unit" => Type::Unit,
"Never" => Type::Never,
Expand Down Expand Up @@ -336,9 +336,7 @@ ExprCall<T>: ExprParsed<'input> = {

ExprAtom<T>: ExprParsed<'input> = {
<val:Int> => ExprParsed::Lit {
val: Lit::Int {
val,
},
val: Lit::Int(val),
},
<val:Bool> => ExprParsed::Lit { val: Lit::Bool(val) },
"unit" => ExprParsed::Lit { val: Lit::Unit },
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/passes/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub enum Def<IdentVars, IdentFields, Expr> {
}

pub type DefParsed<'p> = Def<Spanned<&'p str>, Spanned<&'p str>, Spanned<ExprParsed<'p>>>;
pub type ExprParsed<'p> = Expr<Spanned<&'p str>, Spanned<&'p str>, Lit<'p>, Span>;
pub type ExprParsed<'p> = Expr<Spanned<&'p str>, Spanned<&'p str>, Lit<&'p str>, Span>;
pub type InstrParsed<'p> = Instr<VarArg<Spanned<&'p str>>, Spanned<&'p str>>;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -331,11 +331,11 @@ pub enum BinaryOp {
}

/// A literal value.
#[derive(Display)]
pub enum Lit<'p> {
#[derive(Display, Debug, Copy, Clone)]
pub enum Lit<Int: Display> {
/// Integer literal, representing a signed 64-bit number.
#[display(fmt = "{val}")]
Int { val: &'p str },
#[display(fmt = "{_0}")]
Int(Int),
/// Boolean literal, representing a value of *true* or *false*.
#[display(fmt = "{}", r#"if *_0 { "true" } else { "false" }"#)]
Bool(bool),
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/passes/parse/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fmt::Display;
#[display(bound = "A: Display")]
pub enum Type<A> {
#[display(fmt = "{_0}")]
Int(Int),
Int(IntType),
#[display(fmt = "Bool")]
Bool,
#[display(fmt = "Unit")]
Expand All @@ -23,8 +23,8 @@ pub enum Type<A> {
}

/// Integer types
#[derive(Debug, Clone, Display, Eq, PartialEq)]
pub enum Int {
#[derive(Display, Debug, Copy, Clone, Eq, PartialEq)]
pub enum IntType {
I8,
U8,
I16,
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/passes/reveal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod display;
pub mod reveal;

use crate::passes::parse::{BinaryOp, Def, Typed, UnaryOp};
use crate::passes::parse::{BinaryOp, Def, Lit, Typed, UnaryOp};
use crate::passes::select::InstrSelected;
use crate::passes::validate::TLit;
use crate::passes::validate::Int;
use crate::utils::gen_sym::UniqueSym;
use derive_more::Display;
use itertools::Itertools;
Expand All @@ -20,7 +20,7 @@ pub type DefRevealed<'p> = Def<UniqueSym<'p>, &'p str, Typed<'p, RExpr<'p>>>;

pub enum RExpr<'p> {
Lit {
val: TLit,
val: Lit<Int>,
},
Var {
sym: UniqueSym<'p>,
Expand Down
52 changes: 26 additions & 26 deletions compiler/src/passes/select/select.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::passes::atomize::Atom;
use crate::passes::eliminate::{ExprEliminated, FunEliminated, PrgEliminated, TailEliminated};
use crate::passes::parse::types::Type;
use crate::passes::parse::{BinaryOp, Meta, UnaryOp};
use crate::passes::parse::{BinaryOp, Lit, Meta, UnaryOp};
use crate::passes::select::{
Block, Cnd, FunSelected, InstrSelected, VarArg, X86Selected, CALLEE_SAVED_NO_STACK,
CALLER_SAVED,
};
use crate::passes::validate::{TInt, TLit};
use crate::passes::validate::Int;
use crate::utils::gen_sym::{gen_sym, UniqueSym};
use crate::*;
use std::collections::HashMap;
Expand Down Expand Up @@ -258,20 +258,20 @@ fn select_atom(expr: Atom<'_>) -> VarArg<UniqueSym<'_>> {
match expr {
Atom::Val { val } => {
match val {
TLit::Int(int) => {
Lit::Int(int) => {
match int {
TInt::I8(_) => todo!(),
TInt::U8(_) => todo!(),
TInt::I16(_) => todo!(),
TInt::U16(_) => todo!(),
TInt::I32(_) => todo!(),
TInt::U32(_) => todo!(),
TInt::I64(int) => imm32!(int as i32), // not correct yet
TInt::U64(_) => todo!(),
Int::I8(_) => todo!(),
Int::U8(_) => todo!(),
Int::I16(_) => todo!(),
Int::U16(_) => todo!(),
Int::I32(_) => todo!(),
Int::U32(_) => todo!(),
Int::I64(int) => imm32!(int as i32), // not correct yet
Int::U64(_) => todo!(),
}
}
TLit::Bool(bool) => imm32!(bool as i32), // todo: can be smaller
TLit::Unit => imm32!(0), // todo: can be smaller
Lit::Bool(bool) => imm32!(bool as i32), // todo: can be smaller
Lit::Unit => imm32!(0), // todo: can be smaller
}
}
Atom::Var { sym } => var!(sym),
Expand All @@ -290,21 +290,21 @@ fn select_cmp(op: BinaryOp) -> Cnd {
}
}

impl From<TLit> for u32 {
fn from(value: TLit) -> Self {
impl From<Lit<Int>> for u32 {
fn from(value: Lit<Int>) -> Self {
match value {
TLit::Int(int) => match int {
TInt::I8(int) => int as u32,
TInt::U8(int) => int as u32,
TInt::I16(int) => int as u32,
TInt::U16(int) => int as u32,
TInt::I32(int) => int as u32,
TInt::U32(int) => int,
TInt::I64(int) => int as u32,
TInt::U64(int) => int as u32,
Lit::Int(int) => match int {
Int::I8(int) => int as u32,
Int::U8(int) => int as u32,
Int::I16(int) => int as u32,
Int::U16(int) => int as u32,
Int::I32(int) => int as u32,
Int::U32(int) => int,
Int::I64(int) => int as u32,
Int::U64(int) => int as u32,
},
TLit::Bool(bool) => bool as u32,
TLit::Unit => 0,
Lit::Bool(bool) => bool as u32,
Lit::Unit => 0,
}
}
}
22 changes: 11 additions & 11 deletions compiler/src/passes/validate/constrain/lit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::passes::parse::types::Int;
use crate::passes::parse::types::IntType;
use crate::passes::parse::{Constrained, Lit, Span};
use crate::passes::validate::constrain::uncover_globals::Env;
use crate::passes::validate::error::TypeError;
Expand All @@ -8,21 +8,21 @@ use crate::passes::validate::{ExprConstrained, MetaConstrained};
pub fn constrain_lit<'p>(
env: &mut Env<'_, 'p>,
span: Span,
val: Lit<'p>,
val: Lit<&'p str>,
) -> Result<Constrained<ExprConstrained<'p>>, TypeError> {
// Get the type of the literal.
let typ = match &val {
Lit::Int { val } => match val.rfind(&['i', 'u']) {
Lit::Int(val) => match val.rfind(&['i', 'u']) {
Some(suffix) => {
let int = match &val[suffix..] {
"i8" => Int::I64,
"u8" => Int::U64,
"i16" => Int::I64,
"u16" => Int::U64,
"i32" => Int::I64,
"u32" => Int::U64,
"i64" => Int::I64,
"u64" => Int::U64,
"i8" => IntType::I64,
"u8" => IntType::U64,
"i16" => IntType::I64,
"u16" => IntType::U64,
"i32" => IntType::I64,
"u32" => IntType::U64,
"i64" => IntType::I64,
"u64" => IntType::U64,
_ => unreachable!(),
};
PartialType::Int(int)
Expand Down
18 changes: 4 additions & 14 deletions compiler/src/passes/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ pub struct PrgConstrained<'p> {
}

pub type DefValidated<'p> = Def<UniqueSym<'p>, &'p str, Typed<'p, ExprValidated<'p>>>;
pub type ExprValidated<'p> = Expr<UniqueSym<'p>, &'p str, TLit, Type<UniqueSym<'p>>>;
pub type ExprValidated<'p> = Expr<UniqueSym<'p>, &'p str, Lit<Int>, Type<UniqueSym<'p>>>;

pub type DefConstrained<'p> =
Def<Spanned<UniqueSym<'p>>, Spanned<&'p str>, Constrained<ExprConstrained<'p>>>;
pub type ExprConstrained<'p> =
Expr<Spanned<UniqueSym<'p>>, Spanned<&'p str>, Lit<'p>, MetaConstrained>;
Expr<Spanned<UniqueSym<'p>>, Spanned<&'p str>, Lit<&'p str>, MetaConstrained>;

pub type DefUniquified<'p> =
Def<Spanned<UniqueSym<'p>>, Spanned<&'p str>, Spanned<ExprUniquified<'p>>>;
pub type ExprUniquified<'p> = Expr<Spanned<UniqueSym<'p>>, Spanned<&'p str>, Lit<'p>, Span>;
pub type ExprUniquified<'p> = Expr<Spanned<UniqueSym<'p>>, Spanned<&'p str>, Lit<&'p str>, Span>;
pub type InstrUniquified<'p> = Instr<VarArg<Spanned<UniqueSym<'p>>>, Spanned<UniqueSym<'p>>>;

pub struct MetaConstrained {
Expand All @@ -50,17 +50,7 @@ pub struct MetaConstrained {
}

#[derive(Copy, Clone, Debug, PartialEq, Display)]
pub enum TLit {
#[display(fmt = "{_0}")]
Int(TInt),
#[display(fmt = "{}", r#"if *_0 { "true" } else { "false" }"#)]
Bool(bool),
#[display(fmt = "unit")]
Unit,
}

#[derive(Copy, Clone, Debug, PartialEq, Display)]
pub enum TInt {
pub enum Int {
I8(i8),
U8(u8),
I16(i16),
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/passes/validate/partial_type.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::passes::parse::types::Int;
use crate::passes::parse::types::IntType;
use crate::utils::gen_sym::UniqueSym;
use crate::utils::union_find::{UnionFind, UnionIndex};
use itertools::Itertools;

#[derive(Debug, Clone)]
pub enum PartialType<'p> {
Int(Int),
Int(IntType),
IntAmbiguous,
Bool,
Unit,
Expand Down
Loading

0 comments on commit 8ee8c2c

Please sign in to comment.