Skip to content

Commit

Permalink
refactor how tracing is popped off to be in one location in uplc_gen
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroProofs committed Jan 3, 2024
1 parent a32a864 commit dd1359b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 62 deletions.
68 changes: 14 additions & 54 deletions crates/aiken-lang/src/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::{
use self::{
air::Air,
builder::{
cast_validator_args, constants_ir, convert_type_to_data, extract_constant,
air_holds_msg, cast_validator_args, constants_ir, convert_type_to_data, extract_constant,
lookup_data_type_by_tipo, modify_cyclic_calls, modify_self_calls, rearrange_list_clauses,
AssignmentProperties, ClauseProperties, CodeGenSpecialFuncs, CycleFunctionNames,
DataTypeKey, FunctionAccessKey, HoistableFunction, Variant,
Expand Down Expand Up @@ -3680,6 +3680,16 @@ impl<'a> CodeGenerator<'a> {

fn gen_uplc(&mut self, ir: Air, arg_stack: &mut Vec<Term<Name>>) {
// Going to mark the changes made to code gen after air tree implementation
let error_term = if self.tracing && air_holds_msg(&ir) {
// In the case of an air that holds a msg and tracing is active
// we pop the msg off the stack first
let msg = arg_stack.pop().unwrap();

Term::Error.delayed_trace(msg)
} else {
Term::Error
};

match ir {
Air::Int { value } => {
arg_stack.push(Term::integer(value.parse().unwrap()));
Expand Down Expand Up @@ -3966,13 +3976,6 @@ impl<'a> CodeGenerator<'a> {
} => {
let value = arg_stack.pop().unwrap();

let error_term = if self.tracing && is_expect {
let msg = arg_stack.pop().unwrap();
Term::Error.delayed_trace(msg)
} else {
Term::Error
};

let mut term = arg_stack.pop().unwrap();

let list_id = self.id_gen.next();
Expand Down Expand Up @@ -4495,13 +4498,6 @@ impl<'a> CodeGenerator<'a> {
Air::AssertConstr { constr_index } => {
let constr = arg_stack.pop().unwrap();

let trace_term = if self.tracing {
let msg = arg_stack.pop().unwrap();
Term::Error.delayed_trace(msg)
} else {
Term::Error
};

let mut term = arg_stack.pop().unwrap();

term = Term::equals_integer()
Expand All @@ -4513,27 +4509,19 @@ impl<'a> CodeGenerator<'a> {
)
.apply(constr),
)
.delayed_if_then_else(term, trace_term);
.delayed_if_then_else(term, error_term);

arg_stack.push(term);
}
Air::AssertBool { is_true } => {
let value = arg_stack.pop().unwrap();

let trace_term = if self.tracing {
let msg = arg_stack.pop().unwrap();

Term::Error.delayed_trace(msg)
} else {
Term::Error
};

let mut term = arg_stack.pop().unwrap();

if is_true {
term = value.delayed_if_then_else(term, trace_term)
term = value.delayed_if_then_else(term, error_term)
} else {
term = value.delayed_if_then_else(trace_term, term)
term = value.delayed_if_then_else(error_term, term)
}
arg_stack.push(term);
}
Expand Down Expand Up @@ -4911,13 +4899,6 @@ impl<'a> CodeGenerator<'a> {

let value = arg_stack.pop().unwrap();

let error_term = if self.tracing && is_expect {
let msg = arg_stack.pop().unwrap();
Term::Error.delayed_trace(msg)
} else {
Term::Error
};

let mut term = arg_stack.pop().unwrap();
let list_id = self.id_gen.next();

Expand Down Expand Up @@ -4972,13 +4953,6 @@ impl<'a> CodeGenerator<'a> {
Air::FieldsEmpty => {
let value = arg_stack.pop().unwrap();

let error_term = if self.tracing {
let msg = arg_stack.pop().unwrap();
Term::Error.delayed_trace(msg)
} else {
Term::Error
};

let mut term = arg_stack.pop().unwrap();

term = Term::var(
Expand All @@ -4993,13 +4967,6 @@ impl<'a> CodeGenerator<'a> {
Air::ListEmpty => {
let value = arg_stack.pop().unwrap();

let error_term = if self.tracing {
let msg = arg_stack.pop().unwrap();
Term::Error.delayed_trace(msg)
} else {
Term::Error
};

let mut term = arg_stack.pop().unwrap();

term = value.delayed_choose_list(term, error_term);
Expand Down Expand Up @@ -5190,13 +5157,6 @@ impl<'a> CodeGenerator<'a> {
let inner_types = tipo.get_inner_types();
let value = arg_stack.pop().unwrap();

let error_term = if self.tracing && is_expect && !tipo.is_2_tuple() {
let msg = arg_stack.pop().unwrap();
Term::Error.delayed_trace(msg)
} else {
Term::Error
};

let mut term = arg_stack.pop().unwrap();
let list_id = self.id_gen.next();

Expand Down
18 changes: 17 additions & 1 deletion crates/aiken-lang/src/gen_uplc/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use crate::{
tipo::Type,
};

use super::tree::{AirExpression, AirStatement, AirTree, TreePath};
use super::{
air::Air,
tree::{AirExpression, AirStatement, AirTree, TreePath},
};

pub type Variant = String;

Expand Down Expand Up @@ -1883,3 +1886,16 @@ pub fn get_src_code_by_span(
.expect("Out of bounds span")
.to_string()
}

pub fn air_holds_msg(air: &Air) -> bool {
match air {
Air::AssertConstr { .. } | Air::AssertBool { .. } | Air::FieldsEmpty | Air::ListEmpty => {
true
}
Air::FieldsExpose { is_expect, .. }
| Air::ListAccessor { is_expect, .. }
| Air::TupleAccessor { is_expect, .. } => *is_expect,

_ => false,
}
}
22 changes: 15 additions & 7 deletions crates/aiken-lang/src/gen_uplc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,17 +959,19 @@ impl AirTree {
air_vec.push(Air::AssertConstr {
constr_index: *constr_index,
});
constr.create_air_vec(air_vec);
// msg is first so we can pop it off first in uplc_gen
// if traces are on
msg.create_air_vec(air_vec);
constr.create_air_vec(air_vec);
}
AirStatement::AssertBool {
is_true,
value,
msg,
} => {
air_vec.push(Air::AssertBool { is_true: *is_true });
value.create_air_vec(air_vec);
msg.create_air_vec(air_vec);
value.create_air_vec(air_vec);
}
AirStatement::ClauseGuard {
subject_name,
Expand Down Expand Up @@ -1016,10 +1018,12 @@ impl AirTree {
indices: indices.clone(),
is_expect: msg.is_some(),
});
record.create_air_vec(air_vec);

msg.iter().for_each(|msg| {
msg.create_air_vec(air_vec);
});

record.create_air_vec(air_vec);
}
AirStatement::ListAccessor {
tipo,
Expand All @@ -1034,10 +1038,12 @@ impl AirTree {
tail: *tail,
is_expect: msg.is_some(),
});
list.create_air_vec(air_vec);

msg.iter().for_each(|msg| {
msg.create_air_vec(air_vec);
});

list.create_air_vec(air_vec);
}
AirStatement::ListExpose {
tipo,
Expand All @@ -1061,23 +1067,25 @@ impl AirTree {
tipo: tipo.clone(),
is_expect: msg.is_some(),
});
tuple.create_air_vec(air_vec);

msg.iter().for_each(|msg| {
msg.create_air_vec(air_vec);
});

tuple.create_air_vec(air_vec);
}
AirStatement::NoOp => {
air_vec.push(Air::NoOp);
}
AirStatement::FieldsEmpty { constr, msg } => {
air_vec.push(Air::FieldsEmpty);
constr.create_air_vec(air_vec);
msg.create_air_vec(air_vec);
constr.create_air_vec(air_vec);
}
AirStatement::ListEmpty { list, msg } => {
air_vec.push(Air::ListEmpty);
list.create_air_vec(air_vec);
msg.create_air_vec(air_vec);
list.create_air_vec(air_vec);
}
};
exp.create_air_vec(air_vec);
Expand Down

0 comments on commit dd1359b

Please sign in to comment.