Skip to content

Commit

Permalink
all revert operations created
Browse files Browse the repository at this point in the history
  • Loading branch information
g-r-a-n-t committed Jul 27, 2021
1 parent 8133e19 commit 608cbc4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 47 deletions.
32 changes: 20 additions & 12 deletions crates/yulgen/src/mappers/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn expr(context: &mut Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {

fn revert(context: &mut Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
if let fe::FuncStmt::Revert { error } = &stmt.kind {
return if let Some(error_expr) = error {
if let Some(error_expr) = error {
let error_attributes = context
.analysis
.get_expression(error_expr)
Expand All @@ -132,21 +132,20 @@ fn revert(context: &mut Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
if let Type::Struct(_struct) = &error_attributes.typ {
context.revert_errors.insert(_struct.clone());

let revert_data = expressions::expr(context, error_expr);
let revert_fn = names::revert_name(&_struct.name, &AbiType::from(_struct));

statement! {
([revert_fn]([revert_data]))
}
revert_operations::revert(
&_struct.name,
&AbiType::from(_struct),
expressions::expr(context, error_expr)
)
} else {
panic!("trying to revert with non-struct expression")
}
} else {
statement! { revert(0, 0) }
};
}
} else {
unreachable!()
}

unreachable!()
}

fn emit(context: &mut Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {
Expand Down Expand Up @@ -180,13 +179,22 @@ fn assert(context: &mut Context, stmt: &Node<fe::FuncStmt>) -> yul::Statement {

if let Type::String(string) = &msg_attributes.typ {
context.assert_strings.insert(string.to_owned());
revert_operations::error_revert(&AbiType::from(string), test, msg)

statement! {
if (iszero([test])) {
[revert_operations::error_revert(&AbiType::from(string), msg)]
}
}
} else {
unreachable!()
}
}
None => {
statement! { if (iszero([test])) { (revert_with_Panic_uint256([literal_expression! {(PANIC_FAILED_ASSERTION)}])) } }
statement! {
if (iszero([test])) {
[revert_operations::panic_revert(PANIC_FAILED_ASSERTION)]
}
}
}
}
} else {
Expand Down
11 changes: 1 addition & 10 deletions crates/yulgen/src/names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,8 @@ pub fn var_name(name: &str) -> yul::Identifier {
identifier! { (format!("${}", name)) }
}

/// Generate a revert function name for the name `Error` and a given set of types
pub fn error_revert_name(typ: &AbiType) -> yul::Identifier {
revert_name("Error", typ)
}

pub fn panic_revert_name(typ: &AbiType) -> yul::Identifier {
revert_name("Panic", typ)
}

/// Generates a revert function name for a given name and types
pub fn revert_name(name: &str, typ: &AbiType) -> yul::Identifier {
pub fn revert(name: &str, typ: &AbiType) -> yul::Identifier {
let name = format!("revert_with_{}_{}", name, abi_names::typ(typ));

identifier! { (name) }
Expand Down
18 changes: 9 additions & 9 deletions crates/yulgen/src/operations/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use crate::types::AbiType;
use crate::names;
use yultsur::*;

pub fn error_revert(typ: &AbiType, test: yul::Expression, msg: yul::Expression) -> yul::Statement {
let func_name = names::error_revert_name(typ);
// this is more of an assert operation
pub fn error_revert(typ: &AbiType, msg: yul::Expression) -> yul::Statement {
revert("Error", typ, msg)
}

statement! {
if (iszero([test])) {
([func_name]([msg]))
}
}
pub fn panic_revert(val: usize) -> yul::Statement {
revert("Panic", &AbiType::Uint { size: 32 }, literal_expression! { (val) })
}

pub fn revert(name: &str, types: &[AbiType]) -> yul::Statement {
unimplemented!()
pub fn revert(name: &str, typ: &AbiType, val: yul::Expression) -> yul::Statement {
let func_name = names::revert(name, typ);
statement! { [func_name]([val]) }
}
5 changes: 2 additions & 3 deletions crates/yulgen/src/runtime/functions/abi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::constants::PANIC_INVALID_ABI_DATA;
use crate::names::abi as abi_names;
use crate::operations::abi as abi_operations;
use crate::operations::revert as revert_operations;
use crate::operations::abi::EncodingSize;
use crate::types::{AbiDecodeLocation, AbiType};
use crate::utils::ceil_32;
Expand Down Expand Up @@ -447,9 +448,7 @@ pub fn is_right_padded() -> yul::Statement {
}

fn revert_with_invalid_abi_data() -> yul::Statement {
statement!(revert_with_Panic_uint256([
literal_expression! { (PANIC_INVALID_ABI_DATA) }
]))
revert_operations::panic_revert(PANIC_INVALID_ABI_DATA)
}

/// Reverts if the value is not left padded with the given number of bits.
Expand Down
9 changes: 3 additions & 6 deletions crates/yulgen/src/runtime/functions/math.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::constants::{numeric_min_max, PANIC_DIV_OR_MOD_BY_ZERO, PANIC_OVER_OR_UNDERFLOW};
use crate::names;
use crate::operations::revert as revert_operations;
use fe_analyzer::namespace::types::Integer;
use yultsur::*;

Expand Down Expand Up @@ -110,15 +111,11 @@ pub fn all() -> Vec<yul::Statement> {
}

fn revert_with_over_or_under_flow() -> yul::Statement {
statement!(revert_with_Panic_uint256([
literal_expression! {(PANIC_OVER_OR_UNDERFLOW)}
]))
revert_operations::panic_revert(PANIC_OVER_OR_UNDERFLOW)
}

fn revert_with_div_or_mod_by_zero() -> yul::Statement {
statement!(revert_with_Panic_uint256([
literal_expression! {(PANIC_DIV_OR_MOD_BY_ZERO)}
]))
revert_operations::panic_revert(PANIC_DIV_OR_MOD_BY_ZERO)
}

fn checked_mod_unsigned() -> yul::Statement {
Expand Down
7 changes: 2 additions & 5 deletions crates/yulgen/src/runtime/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ pub mod structs;

/// Returns all functions that should be available during runtime.
pub fn std() -> Vec<yul::Statement> {
let mut functions = [
[
contracts::all(),
abi::all(),
data::all(),
math::all(),
revert::all(),
]
.concat();
functions.sort();
functions.dedup();
functions
.concat()
}
4 changes: 2 additions & 2 deletions crates/yulgen/src/runtime/functions/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn panic_revert() -> yul::Statement {

/// Generate a YUL function to revert with data
pub fn revert(name: &str, typ: &AbiType) -> yul::Statement {
let function_name = names::revert_name(name, typ);
let func_name = names::revert(name, typ);
// the selector parens around a tuple are removed for the selector preimage
// e.g. we use `MyError(bool, address)` instead of `MyError(bool, address)`
let selector = {
Expand All @@ -35,7 +35,7 @@ pub fn revert(name: &str, typ: &AbiType) -> yul::Statement {
let encoding_size = abi_operations::encoding_size(&[typ.to_owned()], vec![expression! { val }]);

function_definition! {
function [function_name](val) {
function [func_name](val) {
(let ptr := alloc_mstoren([selector], 4))
(pop([encode_val]))
(revert(ptr, (add(4, [encoding_size]))))
Expand Down

0 comments on commit 608cbc4

Please sign in to comment.