Skip to content

Commit

Permalink
Added names to funcs for debugging purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-barrett committed Jul 30, 2023
1 parent 8cba258 commit 8d2af27
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/lem/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,8 @@ impl Func {
// Retrieve preimage hashes and tags create the full preimage pointers
// and add them to bound allocations
for i in 0..preallocated_preimg.len() / 2 {
let preimg_tag = &preallocated_preimg[2*i];
let preimg_hash = &preallocated_preimg[2*i + 1];
let preimg_tag = &preallocated_preimg[2 * i];
let preimg_hash = &preallocated_preimg[2 * i + 1];
let preimg_ptr =
AllocatedPtr::from_parts(preimg_tag.clone(), preimg_hash.clone());
bound_allocations.insert($preimg[i].clone(), preimg_ptr);
Expand Down
28 changes: 14 additions & 14 deletions src/lem/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) fn eval_step() -> Func {
let apply_cont = apply_cont();
let make_thunk = make_thunk();

func!((expr, env, cont): 3 => {
func!(step(expr, env, cont): 3 => {
let (expr, env, cont, ctrl) = reduce(expr, env, cont);
let (expr, env, cont, ctrl) = apply_cont(expr, env, cont, ctrl);
let (expr, env, cont, _ctrl) = make_thunk(expr, env, cont, ctrl);
Expand All @@ -18,7 +18,7 @@ pub(crate) fn eval_step() -> Func {
}

fn safe_uncons() -> Func {
func!((xs): 2 => {
func!(safe_uncons(xs): 2 => {
let nil: Expr::Nil;
let nilstr = Symbol("");
match xs.tag {
Expand All @@ -41,7 +41,7 @@ fn safe_uncons() -> Func {
}

fn make_tail_continuation() -> Func {
func!((env, continuation): 1 => {
func!(make_tail_continuation(env, continuation): 1 => {
match continuation.tag {
Cont::Tail => {
return (continuation);
Expand All @@ -55,7 +55,7 @@ fn make_tail_continuation() -> Func {
fn reduce() -> Func {
// Auxiliary functions
let safe_uncons = safe_uncons();
let env_to_use = func!((smaller_env, smaller_rec_env): 1 => {
let env_to_use = func!(env_to_use(smaller_env, smaller_rec_env): 1 => {
match smaller_rec_env.tag {
Expr::Nil => {
return (smaller_env)
Expand All @@ -64,7 +64,7 @@ fn reduce() -> Func {
let env: Expr::Cons = hash2(smaller_rec_env, smaller_env);
return (env)
});
let extract_arg = func!((args): 2 => {
let extract_arg = func!(extract_arg(args): 2 => {
match args.tag {
Expr::Nil => {
let dummy = Symbol("dummy");
Expand All @@ -77,7 +77,7 @@ fn reduce() -> Func {
}
}
});
let expand_bindings = func!((head, body, body1, rest_bindings): 1 => {
let expand_bindings = func!(expand_bindings(head, body, body1, rest_bindings): 1 => {
match rest_bindings.tag {
Expr::Nil => {
return (body1)
Expand All @@ -87,7 +87,7 @@ fn reduce() -> Func {
let expanded: Expr::Cons = hash2(head, expanded_0);
return (expanded)
});
let choose_let_cont = func!((head, var, env, expanded, cont): 1 => {
let choose_let_cont = func!(choose_let_cont(head, var, env, expanded, cont): 1 => {
match head.val {
Symbol("let") => {
let cont: Cont::Let = hash4(var, env, expanded, cont);
Expand All @@ -99,7 +99,7 @@ fn reduce() -> Func {
}
}
});
let choose_unop = func!((head): 1 => {
let choose_unop = func!(choose_unop(head): 1 => {
let nil: Expr::Nil;
let t = Symbol("t");
match head.val {
Expand All @@ -120,7 +120,7 @@ fn reduce() -> Func {
return (nil)
});

let choose_binop = func!((head): 1 => {
let choose_binop = func!(choose_binop(head): 1 => {
let nil: Expr::Nil;
let t = Symbol("t");
match head.val {
Expand All @@ -144,7 +144,7 @@ fn reduce() -> Func {
};
return (nil)
});
let is_potentially_fun = func!((head): 1 => {
let is_potentially_fun = func!(is_potentially_fun(head): 1 => {
let t = Symbol("t");
let nil: Expr::Nil;
match head.tag {
Expand All @@ -164,7 +164,7 @@ fn reduce() -> Func {
return (nil)
});

func!((expr, env, cont): 4 => {
func!(reduce(expr, env, cont): 4 => {
// Useful constants
let ret: Ctrl::Return;
let apply: Ctrl::ApplyContinuation;
Expand Down Expand Up @@ -440,7 +440,7 @@ fn reduce() -> Func {
fn apply_cont() -> Func {
let safe_uncons = safe_uncons();
let make_tail_continuation = make_tail_continuation();
let extend_rec = func!((env, var, result): 1 => {
let extend_rec = func!(extend_rec(env, var, result): 1 => {

let (binding_or_env, rest) = unhash2(env);
let (var_or_binding, _val_or_more_bindings) = unhash2(binding_or_env);
Expand All @@ -462,7 +462,7 @@ fn apply_cont() -> Func {
}
}
});
func!((result, env, cont, ctrl): 4 => {
func!(apply_cont(result, env, cont, ctrl): 4 => {
// Useful constants
let ret: Ctrl::Return;
let makethunk: Ctrl::MakeThunk;
Expand Down Expand Up @@ -790,7 +790,7 @@ fn apply_cont() -> Func {
}

fn make_thunk() -> Func {
func!((expr, env, cont, ctrl): 4 => {
func!(make_thunk(expr, env, cont, ctrl): 4 => {
let ret: Ctrl::Return;
match ctrl.tag {
Ctrl::MakeThunk => {
Expand Down
3 changes: 2 additions & 1 deletion src/lem/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,9 @@ macro_rules! block {

#[macro_export]
macro_rules! func {
(($( $in:ident ),*): $size:expr => $lem:tt) => {
($name:ident($( $in:ident ),*): $size:expr => $lem:tt) => {
$crate::lem::Func::new(
stringify!($name).into(),
vec![$($crate::var!($in)),*],
$size,
$crate::block!($lem),
Expand Down
32 changes: 22 additions & 10 deletions src/lem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub type AString = Arc<str>;
/// function body, which is a `Block`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Func {
name: String,
input_params: Vec<Var>,
output_size: usize,
body: Block,
Expand Down Expand Up @@ -266,8 +267,14 @@ pub enum Op {

impl Func {
/// Instantiates a `Func` with the appropriate transformations and checks
pub fn new(input_params: Vec<Var>, output_size: usize, body: Block) -> Result<Func> {
pub fn new(
name: String,
input_params: Vec<Var>,
output_size: usize,
body: Block,
) -> Result<Func> {
let func = Func {
name,
input_params,
output_size,
body,
Expand Down Expand Up @@ -501,7 +508,12 @@ impl Func {
ops.extend_from_slice(&self.body.ops);
let ctrl = self.body.ctrl.clone();
let body = Block { ops, ctrl };
Self::new(self.input_params.clone(), self.output_size, body)
Self::new(
self.name.clone(),
self.input_params.clone(),
self.output_size,
body,
)
}
}

Expand Down Expand Up @@ -707,7 +719,7 @@ mod tests {

#[test]
fn accepts_virtual_nested_match_tag() {
let lem = func!((expr_in, env_in, cont_in): 3 => {
let lem = func!(foo(expr_in, env_in, cont_in): 3 => {
match expr_in.tag {
Expr::Num => {
let cont_out_terminal: Cont::Terminal;
Expand Down Expand Up @@ -746,7 +758,7 @@ mod tests {

#[test]
fn resolves_conflicts_of_clashing_names_in_parallel_branches() {
let lem = func!((expr_in, env_in, _cont_in): 3 => {
let lem = func!(foo(expr_in, env_in, _cont_in): 3 => {
match expr_in.tag {
// This match is creating `cont_out_terminal` on two different
// branches, which, in theory, would cause troubles at allocation
Expand All @@ -770,7 +782,7 @@ mod tests {

#[test]
fn handles_non_ssa() {
let func = func!((expr_in, _env_in, _cont_in): 3 => {
let func = func!(foo(expr_in, _env_in, _cont_in): 3 => {
let x: Expr::Cons = hash2(expr_in, expr_in);
// The next line rewrites `x` and it should move on smoothly, matching
// the expected number of constraints accordingly
Expand All @@ -785,7 +797,7 @@ mod tests {

#[test]
fn test_simple_all_paths_delta() {
let lem = func!((expr_in, env_in, _cont_in): 3 => {
let lem = func!(foo(expr_in, env_in, _cont_in): 3 => {
let cont_out_terminal: Cont::Terminal;
return (expr_in, env_in, cont_out_terminal);
});
Expand All @@ -796,7 +808,7 @@ mod tests {

#[test]
fn test_match_all_paths_delta() {
let lem = func!((expr_in, env_in, _cont_in): 3 => {
let lem = func!(foo(expr_in, env_in, _cont_in): 3 => {
match expr_in.tag {
Expr::Num => {
let cont_out_terminal: Cont::Terminal;
Expand All @@ -815,7 +827,7 @@ mod tests {

#[test]
fn test_hash_slots() {
let lem = func!((expr_in, env_in, cont_in): 3 => {
let lem = func!(foo(expr_in, env_in, cont_in): 3 => {
let _x: Expr::Cons = hash2(expr_in, env_in);
let _y: Expr::Cons = hash3(expr_in, env_in, cont_in);
let _z: Expr::Cons = hash4(expr_in, env_in, cont_in, cont_in);
Expand Down Expand Up @@ -846,7 +858,7 @@ mod tests {

#[test]
fn test_unhash_slots() {
let lem = func!((expr_in, env_in, cont_in): 3 => {
let lem = func!(foo(expr_in, env_in, cont_in): 3 => {
let _x: Expr::Cons = hash2(expr_in, env_in);
let _y: Expr::Cons = hash3(expr_in, env_in, cont_in);
let _z: Expr::Cons = hash4(expr_in, env_in, cont_in, cont_in);
Expand Down Expand Up @@ -880,7 +892,7 @@ mod tests {

#[test]
fn test_unhash_nested_slots() {
let lem = func!((expr_in, env_in, cont_in): 3 => {
let lem = func!(foo(expr_in, env_in, cont_in): 3 => {
let _x: Expr::Cons = hash2(expr_in, env_in);
let _y: Expr::Cons = hash3(expr_in, env_in, cont_in);
let _z: Expr::Cons = hash4(expr_in, env_in, cont_in, cont_in);
Expand Down

0 comments on commit 8d2af27

Please sign in to comment.