-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(interpreter): use let else
#629
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
pub use crate::InstructionResult; | ||
|
||
macro_rules! check_staticcall { | ||
($interp:expr) => { | ||
if $interp.is_static { | ||
|
@@ -21,7 +19,7 @@ macro_rules! check { | |
macro_rules! gas { | ||
($interp:expr, $gas:expr) => { | ||
if crate::USE_GAS { | ||
if !$interp.gas.record_cost(($gas)) { | ||
if !$interp.gas.record_cost($gas) { | ||
$interp.instruction_result = InstructionResult::OutOfGas; | ||
return; | ||
} | ||
|
@@ -30,19 +28,19 @@ macro_rules! gas { | |
} | ||
|
||
macro_rules! refund { | ||
($interp:expr, $gas:expr) => {{ | ||
($interp:expr, $gas:expr) => { | ||
if crate::USE_GAS { | ||
$interp.gas.gas_refund($gas); | ||
$interp.gas.record_refund($gas); | ||
} | ||
}}; | ||
}; | ||
} | ||
|
||
macro_rules! gas_or_fail { | ||
($interp:expr, $gas:expr) => { | ||
if crate::USE_GAS { | ||
match $gas { | ||
Some(gas_used) => gas!($interp, gas_used), | ||
None => { | ||
Some(gas_used) if $interp.gas.record_cost(gas_used) => {} | ||
_ => { | ||
$interp.instruction_result = InstructionResult::OutOfGas; | ||
return; | ||
} | ||
|
@@ -52,11 +50,9 @@ macro_rules! gas_or_fail { | |
} | ||
|
||
macro_rules! memory_resize { | ||
($interp:expr, $offset:expr, $len:expr) => {{ | ||
let len: usize = $len; | ||
let offset: usize = $offset; | ||
($interp:expr, $offset:expr, $len:expr) => { | ||
if let Some(new_size) = | ||
crate::interpreter::memory::next_multiple_of_32(offset.saturating_add(len)) | ||
crate::interpreter::memory::next_multiple_of_32($offset.saturating_add($len)) | ||
{ | ||
#[cfg(feature = "memory_limit")] | ||
if new_size > ($interp.memory_limit as usize) { | ||
|
@@ -78,7 +74,7 @@ macro_rules! memory_resize { | |
$interp.instruction_result = InstructionResult::MemoryOOG; | ||
return; | ||
} | ||
}}; | ||
}; | ||
} | ||
|
||
macro_rules! pop_address { | ||
|
@@ -115,23 +111,23 @@ macro_rules! pop_address { | |
} | ||
|
||
macro_rules! pop { | ||
( $interp:expr, $x1:ident) => { | ||
($interp:expr, $x1:ident) => { | ||
if $interp.stack.len() < 1 { | ||
$interp.instruction_result = InstructionResult::StackUnderflow; | ||
return; | ||
} | ||
// Safety: Length is checked above. | ||
let $x1 = unsafe { $interp.stack.pop_unsafe() }; | ||
}; | ||
( $interp:expr, $x1:ident, $x2:ident) => { | ||
($interp:expr, $x1:ident, $x2:ident) => { | ||
if $interp.stack.len() < 2 { | ||
$interp.instruction_result = InstructionResult::StackUnderflow; | ||
return; | ||
} | ||
// Safety: Length is checked above. | ||
let ($x1, $x2) = unsafe { $interp.stack.pop2_unsafe() }; | ||
}; | ||
( $interp:expr, $x1:ident, $x2:ident, $x3:ident) => { | ||
($interp:expr, $x1:ident, $x2:ident, $x3:ident) => { | ||
if $interp.stack.len() < 3 { | ||
$interp.instruction_result = InstructionResult::StackUnderflow; | ||
return; | ||
|
@@ -140,7 +136,7 @@ macro_rules! pop { | |
let ($x1, $x2, $x3) = unsafe { $interp.stack.pop3_unsafe() }; | ||
}; | ||
|
||
( $interp:expr, $x1:ident, $x2:ident, $x3:ident, $x4:ident) => { | ||
($interp:expr, $x1:ident, $x2:ident, $x3:ident, $x4:ident) => { | ||
if $interp.stack.len() < 4 { | ||
$interp.instruction_result = InstructionResult::StackUnderflow; | ||
return; | ||
|
@@ -151,23 +147,23 @@ macro_rules! pop { | |
} | ||
|
||
macro_rules! pop_top { | ||
( $interp:expr, $x1:ident) => { | ||
($interp:expr, $x1:ident) => { | ||
if $interp.stack.len() < 1 { | ||
$interp.instruction_result = InstructionResult::StackUnderflow; | ||
return; | ||
} | ||
// Safety: Length is checked above. | ||
let $x1 = unsafe { $interp.stack.top_unsafe() }; | ||
}; | ||
( $interp:expr, $x1:ident, $x2:ident) => { | ||
($interp:expr, $x1:ident, $x2:ident) => { | ||
if $interp.stack.len() < 2 { | ||
$interp.instruction_result = InstructionResult::StackUnderflow; | ||
return; | ||
} | ||
// Safety: Length is checked above. | ||
let ($x1, $x2) = unsafe { $interp.stack.pop_top_unsafe() }; | ||
}; | ||
( $interp:expr, $x1:ident, $x2:ident, $x3:ident) => { | ||
($interp:expr, $x1:ident, $x2:ident, $x3:ident) => { | ||
if $interp.stack.len() < 3 { | ||
$interp.instruction_result = InstructionResult::StackUnderflow; | ||
return; | ||
|
@@ -178,59 +174,57 @@ macro_rules! pop_top { | |
} | ||
|
||
macro_rules! push_b256 { | ||
( $interp:expr, $( $x:expr ),* ) => ( | ||
$( | ||
match $interp.stack.push_b256($x) { | ||
Ok(()) => (), | ||
Err(e) => { | ||
$interp.instruction_result = e; | ||
return | ||
}, | ||
} | ||
)* | ||
) | ||
($interp:expr, $($x:expr),* $(,)?) => ($( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this now lets you push multiple values? or what is the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just allows trailing comma. Sometimes rustfmt will add it and then parsing fails because it is not expected in the macro. |
||
match $interp.stack.push_b256($x) { | ||
Ok(()) => {}, | ||
Err(e) => { | ||
$interp.instruction_result = e; | ||
return; | ||
}, | ||
} | ||
)*) | ||
} | ||
|
||
macro_rules! push { | ||
( $interp:expr, $( $x:expr ),* ) => ( | ||
$( | ||
match $interp.stack.push($x) { | ||
Ok(()) => (), | ||
Err(e) => { $interp.instruction_result = e; | ||
return | ||
} , | ||
} | ||
)* | ||
) | ||
($interp:expr, $($x:expr),* $(,)?) => ($( | ||
match $interp.stack.push($x) { | ||
Ok(()) => {}, | ||
Err(e) => { | ||
$interp.instruction_result = e; | ||
return; | ||
} | ||
} | ||
)*) | ||
} | ||
|
||
macro_rules! as_u64_saturated { | ||
( $v:expr ) => {{ | ||
if $v.as_limbs()[1] != 0 || $v.as_limbs()[2] != 0 || $v.as_limbs()[3] != 0 { | ||
u64::MAX | ||
($v:expr) => {{ | ||
let x: &[u64; 4] = $v.as_limbs(); | ||
if x[1] == 0 && x[2] == 0 && x[3] == 0 { | ||
x[0] | ||
} else { | ||
$v.as_limbs()[0] | ||
u64::MAX | ||
} | ||
}}; | ||
} | ||
|
||
macro_rules! as_usize_saturated { | ||
( $v:expr ) => {{ | ||
($v:expr) => { | ||
as_u64_saturated!($v) as usize | ||
}}; | ||
}; | ||
} | ||
|
||
macro_rules! as_usize_or_fail { | ||
( $interp:expr, $v:expr ) => {{ | ||
($interp:expr, $v:expr) => { | ||
as_usize_or_fail!($interp, $v, InstructionResult::InvalidOperandOOG) | ||
}}; | ||
}; | ||
|
||
( $interp:expr, $v:expr, $reason:expr ) => {{ | ||
if $v.as_limbs()[1] != 0 || $v.as_limbs()[2] != 0 || $v.as_limbs()[3] != 0 { | ||
($interp:expr, $v:expr, $reason:expr) => {{ | ||
let x = $v.as_limbs(); | ||
if x[1] != 0 || x[2] != 0 || x[3] != 0 { | ||
$interp.instruction_result = $reason; | ||
return; | ||
} | ||
|
||
$v.as_limbs()[0] as usize | ||
x[0] as usize | ||
}}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit any reason to not use the macro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would duplicate the assign/return on both branches, doesn't really matter since compiler probably optimizes that anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you revert this one, I slightly vary on the fn call side-effect inside if, and second reason is that
gas!
is used everywhere so it converges to one placeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understandable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fac35ad