Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lair compiler steps #361

Merged
merged 8 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/lair/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,10 @@ mod tests {
let expected_trace = RowMajorMatrix::new(
[
// nonce, 4 inputs, 1 output, last_nonce, last_count, 6 coeffs, 2 selectors
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
1, 1, 3, 8, 2, 1, 0, 1, 1, 0, 0, 0, 1, 0,
2, 0, 0, 4, 1, 1, 0, 1, 0, 0, 1509949441, 0, 1, 0,
3, 0, 0, 0, 9, 1, 0, 1, 0, 0, 0, 447392427, 1, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
1, 1, 3, 8, 2, 1, 0, 1, 1, 0, 0, 0, 0, 1,
2, 0, 0, 4, 1, 1, 0, 1, 0, 0, 1509949441, 0, 0, 1,
3, 0, 0, 0, 9, 1, 0, 1, 0, 0, 0, 447392427, 0, 1,
]
.into_iter()
.map(F::from_canonical_u32)
Expand Down
40 changes: 31 additions & 9 deletions src/lair/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ use super::{List, Name};
/// The type for variable references
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct Var {
pub name: &'static str,
pub name: Ident,
pub size: usize,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub enum Ident {
User(&'static str),
Internal(usize),
}

impl Var {
pub fn atom(name: &'static str) -> Self {
Self { name, size: 1 }
Self {
name: Ident::User(name),
size: 1,
}
}
}

Expand All @@ -29,6 +38,15 @@ impl std::fmt::Display for Var {
}
}

impl std::fmt::Display for Ident {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Ident::User(n) => write!(f, "{}", n),
Ident::Internal(n) => write!(f, "${}", n),
}
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct VarList(List<Var>);

Expand Down Expand Up @@ -159,9 +177,13 @@ impl<F> BlockE<F> {
pub enum CtrlE<F> {
/// `Match(x, cases)` matches on `x` in order to decide which case to execute.
/// The list collects all the values that map to the same branch
Match(Var, CasesE<List<F>, F>),
Match(Var, CasesE<List<F>, (BlockE<F>, CaseType)>),
/// `MatchMany(x, cases)` matches on array `x` in order to decide which case to execute
MatchMany(Var, CasesE<List<F>, F>),
MatchMany(Var, CasesE<List<F>, (BlockE<F>, CaseType)>),
/// Nondeterministic version of `Match`
Choose(Var, CasesE<List<F>, BlockE<F>>),
/// Nondeterministic version of `MatchMany`
ChooseMany(Var, CasesE<List<F>, BlockE<F>>),
/// `If(b, t, f)` executes block `f` if `b` is zero and `t` otherwise
If(Var, Box<BlockE<F>>, Box<BlockE<F>>),
/// Contains the variables whose bindings will construct the output of the
Expand All @@ -186,14 +208,14 @@ pub enum CaseType {
/// matches and an optional default case in case there's no match. Each code path
/// is encoded as its own block
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CasesE<K, F> {
pub branches: Vec<(K, BlockE<F>, CaseType)>,
pub default: Option<(Box<BlockE<F>>, CaseType)>,
pub struct CasesE<K, B> {
pub branches: Vec<(K, B)>,
pub default: Option<Box<B>>,
}

impl<K, F> CasesE<K, F> {
impl<K, B> CasesE<K, B> {
#[inline]
pub fn no_default(branches: Vec<(K, BlockE<F>, CaseType)>) -> Self {
pub fn no_default(branches: Vec<(K, B)>) -> Self {
Self {
branches,
default: None,
Expand Down
40 changes: 16 additions & 24 deletions src/lair/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ macro_rules! func {

#[macro_export]
macro_rules! var {
($variable:ident) => {
$crate::lair::expr::Var {
name: stringify!($variable),
size: 1,
}
};
($variable:ident, $size:expr) => {
$crate::lair::expr::Var {
name: stringify!($variable),
size: $size,
}
};
($variable:ident) => {{
let name = $crate::lair::expr::Ident::User(stringify!($variable));
$crate::lair::expr::Var { name, size: 1 }
}};
($variable:ident, $size:expr) => {{
let name = $crate::lair::expr::Ident::User(stringify!($variable));
$crate::lair::expr::Var { name, size: $size }
}};
}

#[macro_export]
Expand Down Expand Up @@ -312,14 +308,13 @@ macro_rules! block {
let f = $crate::lair::field_from_i32;
branches.push((
[f($num), $(f($other_num), )*].into(),
$crate::block_init!( $branch ),
constrained,
($crate::block_init!( $branch ), constrained)
));
)*
}
let default = None $( .or ({
let constrained = $crate::constrained!({ $($def)* });
Some((Box::new($crate::block_init!({ $($def)* })), constrained))
Some(Box::new(($crate::block_init!({ $($def)* }), constrained)))
}) )?;
let cases = $crate::lair::expr::CasesE { branches, default };
let ctrl = $crate::lair::expr::CtrlE::Match($var, cases);
Expand All @@ -334,8 +329,7 @@ macro_rules! block {
let arr = $arr.map(|x| $crate::lair::field_from_i32(x.into())).into_iter().collect();
branches.push((
arr,
$crate::block_init!( $branch ),
constrained,
($crate::block_init!( $branch ), constrained)
));
$({
let other_arr = $other_arr.map($crate::lair::field_from_i32).into_iter().collect();
Expand All @@ -349,7 +343,7 @@ macro_rules! block {
}
let default = None $( .or ({
let constrained = $crate::constrained!({ $($def)* });
Some((Box::new($crate::block_init!({ $($def)* })), constrained))
Some(Box::new(($crate::block_init!({ $($def)* }), constrained)))
}) )?;
let cases = $crate::lair::expr::CasesE { branches, default };
let ctrl = $crate::lair::expr::CtrlE::MatchMany($var, cases);
Expand All @@ -364,14 +358,13 @@ macro_rules! block {
let constrained = $crate::constrained!($branch);
branches.push((
[$raw.to_field(), $($other_raw.to_field(),)*].into(),
$crate::block_init!( $branch ),
constrained,
($crate::block_init!( $branch ), constrained)
));
)*
}
let default = None $( .or ({
let constrained = $crate::constrained!({ $($def)* });
Some((Box::new($crate::block_init!({ $($def)* })), constrained))
Some(Box::new(($crate::block_init!({ $($def)* }), constrained)))
}) )?;
let cases = $crate::lair::expr::CasesE { branches, default };
let ctrl = $crate::lair::expr::CtrlE::Match($var, cases);
Expand All @@ -386,14 +379,13 @@ macro_rules! block {
let constrained = $crate::constrained!($branch);
branches.push((
[$cloj($raw), $($cloj($other_raw),)*].into(),
$crate::block_init!( $branch ),
constrained,
($crate::block_init!( $branch ), constrained)
));
)*
}
let default = None $( .or ({
let constrained = $crate::constrained!({ $($def)* });
Some((Box::new($crate::block_init!({ $($def)* })), constrained))
Some(Box::new(($crate::block_init!({ $($def)* }), constrained)))
}) )?;
let cases = $crate::lair::expr::CasesE { branches, default };
let ctrl = $crate::lair::expr::CtrlE::Match($var, cases);
Expand Down
Loading