Skip to content

Commit

Permalink
Rename jump_nz to is_zero. (#1931)
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Jan 26, 2023
1 parent ff12db2 commit 3b92682
Show file tree
Hide file tree
Showing 47 changed files with 216 additions and 218 deletions.
8 changes: 4 additions & 4 deletions corelib/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ impl NonZeroU128Drop of Drop::<NonZero::<u128>>;

#[panic_with('u128 is 0', u128_as_non_zero)]
fn u128_checked_as_non_zero(a: u128) -> Option::<NonZero::<u128>> implicits() nopanic {
match u128_jump_nz(a) {
JumpNzResult::Zero(()) => Option::<NonZero::<u128>>::None(()),
JumpNzResult::NonZero(x) => Option::<NonZero::<u128>>::Some(x),
match u128_is_zero(a) {
IsZeroResult::Zero(()) => Option::<NonZero::<u128>>::None(()),
IsZeroResult::NonZero(x) => Option::<NonZero::<u128>>::Some(x),
}
}

Expand Down Expand Up @@ -189,7 +189,7 @@ impl U128BitOr of BitOr::<u128> {
}
}

extern fn u128_jump_nz(a: u128) -> JumpNzResult::<u128> implicits() nopanic;
extern fn u128_is_zero(a: u128) -> IsZeroResult::<u128> implicits() nopanic;

#[derive(Copy, Drop)]
extern type u8;
Expand Down
6 changes: 3 additions & 3 deletions corelib/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn felt_neg(a: felt) -> felt {

extern type NonZero<T>;
// TODO(spapini): Add generic impls for NonZero for Copy, Drop.
enum JumpNzResult<T> {
enum IsZeroResult<T> {
Zero: (),
NonZero: NonZero::<T>,
}
Expand Down Expand Up @@ -147,7 +147,7 @@ impl PartialOrdFelt of PartialOrd::<felt> {
}
}

extern fn felt_jump_nz(a: felt) -> JumpNzResult::<felt> nopanic;
extern fn felt_is_zero(a: felt) -> IsZeroResult::<felt> nopanic;

// TODO(spapini): Constraint using Copy and Drop traits.
extern fn dup<T>(obj: T) -> (T, T) nopanic;
Expand Down Expand Up @@ -239,7 +239,7 @@ use integer::U128PartialEq;
use integer::U128BitAnd;
use integer::U128BitOr;
use integer::U128BitXor;
use integer::u128_jump_nz;
use integer::u128_is_zero;
use integer::u8;
use integer::u8_const;
use integer::u8_from_felt;
Expand Down
2 changes: 1 addition & 1 deletion crates/cairo-lang-lowering/src/lower/lower_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn lower_expr_if_eq(

// Emit the statement.
scope.push_finalized_statement(Statement::MatchExtern(StatementMatchExtern {
function: corelib::core_jump_nz_func(semantic_db),
function: corelib::core_felt_is_zero(semantic_db),
inputs: vec![condition_var],
arms: vec![
(corelib::jump_nz_zero_variant(ctx.db.upcast()), merged.blocks[0]),
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-lowering/src/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use itertools::{chain, zip_eq};
use num_traits::Zero;
use scope::BlockBuilder;
use semantic::corelib::{
core_felt_ty, core_jump_nz_func, core_nonzero_ty, get_core_function_id,
core_felt_is_zero, core_felt_ty, core_nonzero_ty, get_core_function_id,
jump_nz_nonzero_variant, jump_nz_zero_variant, unit_ty,
};
use semantic::items::enm::SemanticEnumEx;
Expand Down Expand Up @@ -681,7 +681,7 @@ fn lower_expr_match_felt(

// Emit the statement.
scope.push_finalized_statement(Statement::MatchExtern(StatementMatchExtern {
function: core_jump_nz_func(semantic_db),
function: core_felt_is_zero(semantic_db),
inputs: vec![expr_var],
arms,
}));
Expand Down
36 changes: 18 additions & 18 deletions crates/cairo-lang-lowering/src/test_data/if
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ blk2 (root):
Inputs: v0: core::felt
Initial refs:
Statements:
() <- match core::felt_jump_nz(v0) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v0) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
End:
Return(v3)
Expand All @@ -141,9 +141,9 @@ End:
blk2 (root):
Inputs: v0: core::felt
Statements:
() <- match core::felt_jump_nz(v0) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v0) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
End:
Return(v3)
Expand Down Expand Up @@ -192,9 +192,9 @@ blk2 (root):
Inputs: v0: core::felt
Initial refs:
Statements:
() <- match core::felt_jump_nz(v0) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v0) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
(v3: core::felt) <- 20u
End:
Expand All @@ -217,9 +217,9 @@ End:
blk2 (root):
Inputs: v0: core::felt
Statements:
() <- match core::felt_jump_nz(v0) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v0) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
(v3: core::felt) <- 20u
End:
Expand Down Expand Up @@ -274,9 +274,9 @@ Statements:
(v4: core::felt) <- core::FeltAdd::add(v0, v1)
(v5: core::felt) <- core::FeltSub::sub(v2, v3)
(v6: core::felt) <- core::FeltSub::sub(v4, v5)
() <- match core::felt_jump_nz(v6) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v6) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
End:
Return(v10)
Expand All @@ -302,9 +302,9 @@ Statements:
(v4: core::felt) <- core::felt_add(v0, v1)
(v5: core::felt) <- core::felt_sub(v2, v3)
(v6: core::felt) <- core::felt_sub(v4, v5)
() <- match core::felt_jump_nz(v6) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v6) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
End:
Return(v10)
12 changes: 6 additions & 6 deletions crates/cairo-lang-lowering/src/test_data/inline_diagnostics
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ blk2 (root):
Inputs: v0: core::felt
Initial refs:
Statements:
() <- match core::felt_jump_nz(v0) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v0) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
(v3: core::felt) <- 0u
End:
Expand All @@ -111,9 +111,9 @@ End:
blk2 (root):
Inputs: v0: core::felt
Statements:
() <- match core::felt_jump_nz(v0) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v0) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
(v3: core::felt) <- 0u
End:
Expand Down
16 changes: 8 additions & 8 deletions crates/cairo-lang-lowering/src/test_data/match
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ Inputs: v0: core::felt
Initial refs:
Statements:
(v1: core::felt) <- 7u
() <- match core::felt_jump_nz(v1) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v1) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
End:
Return(v5)
Expand All @@ -69,9 +69,9 @@ blk2 (root):
Inputs: v0: core::felt
Statements:
(v1: core::felt) <- 7u
() <- match core::felt_jump_nz(v1) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v1) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
End:
Return(v5)
Expand Down Expand Up @@ -180,7 +180,7 @@ test_function_lowering

//! > function
fn foo() {
match felt_jump_nz(5) {};
match felt_is_zero(5) {};
}

//! > function_name
Expand All @@ -193,7 +193,7 @@ foo
//! > lowering_diagnostics
error: Unsupported match. Currently, matches require one arm per variant, in the order of variant definition.
--> lib.cairo:2:9
match felt_jump_nz(5) {};
match felt_is_zero(5) {};
^*************^

//! > lowering_structured
Expand Down
12 changes: 6 additions & 6 deletions crates/cairo-lang-lowering/src/test_data/tests
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ Statements:
(v3: core::felt) <- 1u
(v4: core::felt) <- 1u
(v5: core::felt) <- core::FeltSub::sub(v3, v4)
() <- match core::felt_jump_nz(v5) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v5) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
(v10: core::felt) <- 3u
(v11: core::felt) <- test::foo(v10)
Expand Down Expand Up @@ -170,9 +170,9 @@ Statements:
(v3: core::felt) <- 1u
(v4: core::felt) <- 1u
(v5: core::felt) <- core::felt_sub(v3, v4)
() <- match core::felt_jump_nz(v5) {
JumpNzResult::Zero => blk0,
JumpNzResult::NonZero => blk1,
() <- match core::felt_is_zero(v5) {
IsZeroResult::Zero => blk0,
IsZeroResult::NonZero => blk1,
}
(v10: core::felt) <- 3u
(v11: core::felt) <- test::foo(v10)
Expand Down
12 changes: 6 additions & 6 deletions crates/cairo-lang-semantic/src/corelib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,21 @@ pub fn true_variant(db: &dyn SemanticGroup) -> ConcreteVariant {
get_enum_concrete_variant(db, "bool", vec![], "True")
}

/// Generates a ConcreteVariant instance for `JumpNzResult::<felt>::Zero`.
/// Generates a ConcreteVariant instance for `IsZeroResult::<felt>::Zero`.
pub fn jump_nz_zero_variant(db: &dyn SemanticGroup) -> ConcreteVariant {
get_enum_concrete_variant(
db,
"JumpNzResult",
"IsZeroResult",
vec![GenericArgumentId::Type(core_felt_ty(db))],
"Zero",
)
}

/// Generates a ConcreteVariant instance for `JumpNzResult::<felt>::NonZero`.
/// Generates a ConcreteVariant instance for `IsZeroResult::<felt>::NonZero`.
pub fn jump_nz_nonzero_variant(db: &dyn SemanticGroup) -> ConcreteVariant {
get_enum_concrete_variant(
db,
"JumpNzResult",
"IsZeroResult",
vec![GenericArgumentId::Type(core_felt_ty(db))],
"NonZero",
)
Expand Down Expand Up @@ -343,8 +343,8 @@ fn get_core_function_impl_method(
})
}

pub fn core_jump_nz_func(db: &dyn SemanticGroup) -> FunctionId {
get_core_function_id(db, "felt_jump_nz".into(), vec![])
pub fn core_felt_is_zero(db: &dyn SemanticGroup) -> FunctionId {
get_core_function_id(db, "felt_is_zero".into(), vec![])
}

/// Given a core library function name and its generic arguments, returns [FunctionId].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn core_libfunc_ap_change<InfoProvider: InvocationApChangeInfoProvider>(
CoreConcreteLibfunc::Drop(_) | CoreConcreteLibfunc::Dup(_) => vec![ApChange::Known(0)],
CoreConcreteLibfunc::Felt(libfunc) => match libfunc {
FeltConcrete::BinaryOperation(_) | FeltConcrete::Const(_) => vec![ApChange::Known(0)],
FeltConcrete::JumpNotZero(_) => vec![ApChange::Known(0), ApChange::Known(0)],
FeltConcrete::IsZero(_) => vec![ApChange::Known(0), ApChange::Known(0)],
},
CoreConcreteLibfunc::FunctionCall(libfunc) => {
vec![ApChange::FunctionCall(libfunc.function.id.clone())]
Expand Down Expand Up @@ -142,7 +142,7 @@ pub fn core_libfunc_ap_change<InfoProvider: InvocationApChangeInfoProvider>(
Uint128Concrete::LessThanOrEqual(_) => vec![ApChange::Known(3), ApChange::Known(2)],
Uint128Concrete::FromFelt(_) => vec![ApChange::Known(1), ApChange::Known(6)],
Uint128Concrete::Const(_) | Uint128Concrete::ToFelt(_) => vec![ApChange::Known(0)],
Uint128Concrete::JumpNotZero(_) => vec![ApChange::Known(0), ApChange::Known(0)],
Uint128Concrete::IsZero(_) => vec![ApChange::Known(0), ApChange::Known(0)],
},
CoreConcreteLibfunc::Mem(libfunc) => match libfunc {
MemConcreteLibfunc::StoreTemp(libfunc) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-sierra-gas/src/core_libfunc_cost_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn u128_libfunc_cost<Ops: CostOperations>(
Uint128Concrete::FromFelt(_) => {
vec![ops.const_cost(2), ops.const_cost(11)]
}
Uint128Concrete::JumpNotZero(_) => {
Uint128Concrete::IsZero(_) => {
vec![ops.const_cost(1), ops.const_cost(1)]
}
Uint128Concrete::LessThan(_) => {
Expand All @@ -363,7 +363,7 @@ fn u128_libfunc_cost<Ops: CostOperations>(
fn felt_libfunc_cost<Ops: CostOperations>(ops: &Ops, libfunc: &FeltConcrete) -> Vec<Ops::CostType> {
match libfunc {
FeltConcrete::Const(_) | FeltConcrete::BinaryOperation(_) => vec![ops.const_cost(0)],
FeltConcrete::JumpNotZero(_) => {
FeltConcrete::IsZero(_) => {
vec![ops.const_cost(1), ops.const_cost(1)]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ foo
//! > sierra_gen_diagnostics

//! > sierra_code
felt_jump_nz([0]) { fallthrough() label0([1]) }
felt_is_zero([0]) { fallthrough() label0([1]) }
branch_align() -> ()
felt_const<1>() -> ([2])
PushValues([2]: felt) -> ([3])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ foo

//! > sierra_code
felt_const<10>() -> ([0])
felt_jump_nz([0]) { fallthrough() label0([1]) }
felt_is_zero([0]) { fallthrough() label0([1]) }
branch_align() -> ()
felt_const<3>() -> ([2])
PushValues([2]: felt) -> ([3])
Expand Down Expand Up @@ -70,7 +70,7 @@ foo
//! > sierra_code
felt_const<7>() -> ([0])
dup<felt>([0]) -> ([0], [1])
felt_jump_nz([1]) { fallthrough() label0([2]) }
felt_is_zero([1]) { fallthrough() label0([2]) }
branch_align() -> ()
PushValues([0]: felt) -> ([3])
jump() { label1() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn test_replacer() {
libfunc felt_mul_2 = felt_mul<2>;
libfunc felt_sub = felt_sub;
libfunc felt_dup = dup<felt>;
libfunc felt_jump_nz = felt_jump_nz;
libfunc felt_is_zero = felt_is_zero;
libfunc store_temp_felt = store_temp<felt>;
libfunc call_foo = function_call<user@foo>;
Expand Down Expand Up @@ -48,7 +48,7 @@ fn test_replacer() {
libfunc [2] = felt_mul<2>;
libfunc [3] = felt_sub;
libfunc [4] = dup<[0]>;
libfunc [5] = felt_jump_nz;
libfunc [5] = felt_is_zero;
libfunc [6] = store_temp<[0]>;
libfunc [7] = function_call<user@[1]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ foo
//! > sierra_code
label0:
dup<felt>([0]) -> ([0], [1])
felt_jump_nz([1]) { fallthrough() label1([2]) }
felt_is_zero([1]) { fallthrough() label1([2]) }
branch_align() -> ()
dup<felt>([0]) -> ([0], [3])
struct_construct<Tuple<felt, felt>>([3], [0]) -> ([4])
Expand Down

0 comments on commit 3b92682

Please sign in to comment.