Skip to content
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
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/acir/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod intrinsics;

/// Test utility for converting [ACIR gen artifacts][crate::acir::ssa::Artifacts]
/// into the final [ACIR Program][Program] in order to use its parser and human-readable text format.
pub(crate) fn ssa_to_acir_program(src: &str) -> Program<FieldElement> {
fn ssa_to_acir_program(src: &str) -> Program<FieldElement> {
ssa_to_acir_program_with_debug_info(src).0
}

Expand Down
81 changes: 81 additions & 0 deletions compiler/noirc_evaluator/src/brillig/brillig_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod brillig_fn;
pub(crate) mod brillig_globals;
mod brillig_instructions;
pub(crate) mod constant_allocation;
#[cfg(test)]
mod tests;
mod variable_liveness;

Expand Down Expand Up @@ -91,3 +92,83 @@ pub(crate) fn gen_brillig_for(
// Generate the final bytecode
Ok(entry_point.finish())
}

#[cfg(test)]
mod entry_point {
use crate::{
assert_artifact_snapshot,
brillig::{
BrilligOptions, brillig_gen::gen_brillig_for, brillig_ir::artifact::BrilligParameter,
},
ssa::ssa_gen::Ssa,
};

#[test]
fn entry_point_setup_basic() {
let src = "
brillig(inline) fn main f0 {
b0(v0: u32, v1: u32):
v2 = add v0, v1
return v2
}
";
let ssa = Ssa::from_str(src).unwrap();
let options = BrilligOptions::default();
let brillig = ssa.to_brillig(&options);

let args = vec![BrilligParameter::SingleAddr(32), BrilligParameter::SingleAddr(32)];
let entry = gen_brillig_for(ssa.main(), args, &brillig, &options).unwrap();

// foo is a very simple function returning the addition of its inputs, which is done at line 18.
// The rest of the code is some overhead added to every brillig function for handling inputs, outputs, globals, ...
//
// Here is a breakdown of the code:
// Line 1-9: The function starts with the handling of the inputs via the 'calldata copy'
// The inputs are put in registers 1 and 2
// Line 10: Calling the part that deals with brillig globals. In this simple example, there are no globals so
// it just returns immediately.
// Line 11: Calling the check_max_stack_depth_procedure (Lines 24-29), via the call 24. Returning from
// this procedure is going to line 18 (right after the call 24 on line 17)
// Line 18: the actual body of the function, which is a single add instruction.
// Line 19-21: An overflow check on the addition, which will trap if the addition overflowed.
// Line 22: Moving the result of the addition into the return register (register 1)
// Line 23: Returning from the function body, which goes back to line 12
// Line 12-15: Handling the return data, moving the return value into the right place in memory and stopping execution.
assert_artifact_snapshot!(entry, @r"
fn main
0: @2 = const u32 1
1: @1 = const u32 32839
2: @0 = const u32 71
3: sp[3] = const u32 2
4: sp[4] = const u32 0
5: @68 = calldata copy [sp[4]; sp[3]]
6: @68 = cast @68 to u32
7: @69 = cast @69 to u32
8: sp[1] = @68
9: sp[2] = @69
10: call 16
11: call 17
12: @70 = sp[1]
13: sp[2] = const u32 70
14: sp[3] = const u32 1
15: stop &[sp[2]; sp[3]]
16: return
17: call 24
18: sp[3] = u32 add sp[1], sp[2]
19: sp[4] = u32 lt_eq sp[1], sp[3]
20: jump if sp[4] to 22
21: call 30
22: sp[1] = sp[3]
23: return
24: @4 = const u32 30791
25: @3 = u32 lt @0, @4
26: jump if @3 to 29
27: @1 = indirect const u64 15764276373176857197
28: trap &[@1; @2]
29: return
30: @1 = indirect const u64 14990209321349310352
31: trap &[@1; @2]
32: return
");
}
}
Loading
Loading