-
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
11 changed files
with
190 additions
and
3 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
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,44 @@ | ||
use cairo_lang_casm::builder::CasmBuilder; | ||
use cairo_lang_casm::casm_build_extend; | ||
use cairo_lang_sierra::extensions::range::IntRangeConcreteLibfunc; | ||
|
||
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: &IntRangeConcreteLibfunc, | ||
builder: CompiledInvocationBuilder<'_>, | ||
) -> Result<CompiledInvocation, InvocationError> { | ||
match libfunc { | ||
IntRangeConcreteLibfunc::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, | ||
const one = 1; | ||
let new_start = start + one; | ||
tempvar is_non_empty = end - start; | ||
jump NonEmpty if is_non_empty != 0; | ||
}; | ||
let non_empty_handle = get_non_fallthrough_statement_id(&builder); | ||
Ok(builder.build_from_casm_builder( | ||
casm_builder, | ||
[ | ||
("Fallthrough", &[], None), | ||
("NonEmpty", &[&[new_start, end], &[start]], Some(non_empty_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 @@ | ||
//! > int_range_pop_front libfunc | ||
|
||
//! > test_runner_name | ||
SmallE2ETestRunner | ||
|
||
//! > cairo | ||
use core::internal::OptionRev; | ||
// TODO(lior): Move to `range.cairo`. | ||
extern type IntRange<T>; | ||
extern fn int_range_pop_front<T>(range: IntRange<T>) -> OptionRev<(IntRange<T>, T)> nopanic; | ||
|
||
fn foo(v: IntRange<i16>) -> OptionRev<(IntRange<i16>, i16)> { | ||
int_range_pop_front(v) | ||
} | ||
|
||
//! > casm | ||
[fp + -3] = [ap + 0] + [fp + -4], ap++; | ||
jmp rel 11 if [ap + -1] != 0; | ||
[ap + 0] = 0, ap++; | ||
[ap + 0] = 0, ap++; | ||
[ap + 0] = 0, ap++; | ||
[ap + 0] = 0, ap++; | ||
ret; | ||
[ap + 0] = 1, ap++; | ||
[ap + 0] = [fp + -4] + 1, ap++; | ||
[ap + 0] = [fp + -3], ap++; | ||
[ap + 0] = [fp + -4], ap++; | ||
ret; | ||
|
||
//! > function_costs | ||
test::foo: OrderedHashMap({Const: 600}) | ||
|
||
//! > sierra_code | ||
type IntRange<i16> = IntRange<i16> [storable: true, drop: true, dup: true, zero_sized: false]; | ||
type i16 = i16 [storable: true, drop: true, dup: true, zero_sized: false]; | ||
type Tuple<IntRange<i16>, i16> = Struct<ut@Tuple, IntRange<i16>, 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 core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)> = Enum<ut@core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>, Unit, Tuple<IntRange<i16>, i16>> [storable: true, drop: true, dup: true, zero_sized: false]; | ||
|
||
libfunc int_range_pop_front<i16> = int_range_pop_front<i16>; | ||
libfunc branch_align = branch_align; | ||
libfunc struct_construct<Unit> = struct_construct<Unit>; | ||
libfunc enum_init<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>, 0> = enum_init<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>, 0>; | ||
libfunc store_temp<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>> = store_temp<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>>; | ||
libfunc struct_construct<Tuple<IntRange<i16>, i16>> = struct_construct<Tuple<IntRange<i16>, i16>>; | ||
libfunc enum_init<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>, 1> = enum_init<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>, 1>; | ||
|
||
int_range_pop_front<i16>([0]) { fallthrough() 6([1], [2]) }; // 0 | ||
branch_align() -> (); // 1 | ||
struct_construct<Unit>() -> ([3]); // 2 | ||
enum_init<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>, 0>([3]) -> ([4]); // 3 | ||
store_temp<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>>([4]) -> ([4]); // 4 | ||
return([4]); // 5 | ||
branch_align() -> (); // 6 | ||
struct_construct<Tuple<IntRange<i16>, i16>>([1], [2]) -> ([5]); // 7 | ||
enum_init<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>, 1>([5]) -> ([6]); // 8 | ||
store_temp<core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>>([6]) -> ([6]); // 9 | ||
return([6]); // 10 | ||
|
||
test::foo@0([0]: IntRange<i16>) -> (core::internal::OptionRev::<(test::IntRange::<core::integer::i16>, core::integer::i16)>); |