-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit-id:12946952
- Loading branch information
Showing
10 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use cairo_lang_casm::builder::CasmBuilder; | ||
use cairo_lang_casm::casm_build_extend; | ||
use cairo_lang_sierra::extensions::range::RangeConcreteLibfunc; | ||
|
||
use super::{CompiledInvocation, CompiledInvocationBuilder, InvocationError}; | ||
use crate::invocations::{add_input_variables, get_non_fallthrough_statement_id}; | ||
|
||
/// Builds instructions for `Range` operations. | ||
pub fn build( | ||
libfunc: &RangeConcreteLibfunc, | ||
builder: CompiledInvocationBuilder<'_>, | ||
) -> Result<CompiledInvocation, InvocationError> { | ||
match libfunc { | ||
RangeConcreteLibfunc::PopFront(_) => build_pop_front(builder), | ||
} | ||
} | ||
|
||
/// Libfunc for reducing `[a, b)` to `[a + 1, b)`. | ||
fn build_pop_front( | ||
builder: CompiledInvocationBuilder<'_>, | ||
) -> Result<CompiledInvocation, InvocationError> { | ||
let [start, end] = builder.try_get_refs::<1>()?[0].try_unpack()?; | ||
|
||
let mut casm_builder = CasmBuilder::default(); | ||
add_input_variables! {casm_builder, | ||
deref start; | ||
deref end; | ||
}; | ||
casm_build_extend! {casm_builder, | ||
tempvar is_non_empty = end - start; | ||
jump NonEmpty if is_non_empty != 0; | ||
jump Failure; | ||
NonEmpty: | ||
const one = 1; | ||
let new_start = start + one; | ||
}; | ||
let failure_handle = get_non_fallthrough_statement_id(&builder); | ||
Ok(builder.build_from_casm_builder( | ||
casm_builder, | ||
[ | ||
("Fallthrough", &[&[new_start, end], &[start]], None), | ||
("Failure", &[], Some(failure_handle)), | ||
], | ||
Default::default(), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//! > range_pop_front libfunc | ||
|
||
//! > test_runner_name | ||
SmallE2ETestRunner | ||
|
||
//! > cairo | ||
// TODO(lior): Move to `range.cairo`. | ||
extern type Range<T>; | ||
extern fn range_pop_front<T>(range: Range<T>) -> Option<(Range<T>, T)> nopanic; | ||
|
||
fn foo(v: Range<i16>) -> Option<(Range<i16>, i16)> { | ||
range_pop_front(v) | ||
} | ||
|
||
//! > casm | ||
[fp + -3] = [ap + 0] + [fp + -4], ap++; | ||
jmp rel 4 if [ap + -1] != 0; | ||
jmp rel 9; | ||
[ap + 0] = 0, ap++; | ||
[ap + 0] = [fp + -4] + 1, ap++; | ||
[ap + 0] = [fp + -3], ap++; | ||
[ap + 0] = [fp + -4], ap++; | ||
ret; | ||
[ap + 0] = 1, ap++; | ||
[ap + 0] = 0, ap++; | ||
[ap + 0] = 0, ap++; | ||
[ap + 0] = 0, ap++; | ||
ret; | ||
|
||
//! > function_costs | ||
test::foo: OrderedHashMap({Const: 700}) | ||
|
||
//! > sierra_code | ||
type Range<i16> = Range<i16> [storable: true, drop: true, dup: true, zero_sized: false]; | ||
type Unit = Struct<ut@Tuple> [storable: true, drop: true, dup: true, zero_sized: true]; | ||
type i16 = i16 [storable: true, drop: true, dup: true, zero_sized: false]; | ||
type Tuple<Range<i16>, i16> = Struct<ut@Tuple, Range<i16>, i16> [storable: true, drop: true, dup: true, zero_sized: false]; | ||
type core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)> = Enum<ut@core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>, Tuple<Range<i16>, i16>, Unit> [storable: true, drop: true, dup: true, zero_sized: false]; | ||
|
||
libfunc range_pop_front<i16> = range_pop_front<i16>; | ||
libfunc branch_align = branch_align; | ||
libfunc struct_construct<Tuple<Range<i16>, i16>> = struct_construct<Tuple<Range<i16>, i16>>; | ||
libfunc enum_init<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>, 0> = enum_init<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>, 0>; | ||
libfunc store_temp<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>> = store_temp<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>>; | ||
libfunc struct_construct<Unit> = struct_construct<Unit>; | ||
libfunc enum_init<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>, 1> = enum_init<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>, 1>; | ||
|
||
range_pop_front<i16>([0]) { fallthrough([1], [2]) 6() }; // 0 | ||
branch_align() -> (); // 1 | ||
struct_construct<Tuple<Range<i16>, i16>>([1], [2]) -> ([3]); // 2 | ||
enum_init<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>, 0>([3]) -> ([4]); // 3 | ||
store_temp<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>>([4]) -> ([4]); // 4 | ||
return([4]); // 5 | ||
branch_align() -> (); // 6 | ||
struct_construct<Unit>() -> ([5]); // 7 | ||
enum_init<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>, 1>([5]) -> ([6]); // 8 | ||
store_temp<core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>>([6]) -> ([6]); // 9 | ||
return([6]); // 10 | ||
|
||
test::foo@0([0]: Range<i16>) -> (core::option::Option::<(test::Range::<core::integer::i16>, core::integer::i16)>); |