Refactor SideEffects struct

This commit is contained in:
Harikrishnan Mulackal
2020-08-18 14:53:59 +02:00
parent 58bfe0b0d2
commit 3cbe65e4f3
24 changed files with 290 additions and 126 deletions
+50 -17
View File
@@ -18,6 +18,7 @@
#pragma once
#include <algorithm>
#include <set>
namespace solidity::yul
@@ -30,6 +31,20 @@ namespace solidity::yul
*/
struct SideEffects
{
/// Corresponds to the effect that a Yul-builtin has on a generic data location (storage, memory
/// and other blockchain state).
enum Effect
{
None,
Read,
Write
};
friend Effect operator+(Effect const& _a, Effect const& _b)
{
return static_cast<Effect>(std::max(static_cast<int>(_a), static_cast<int>(_b)));
}
/// If true, expressions in this code can be freely moved and copied without altering the
/// semantics.
/// At statement level, it means that functions containing this code can be
@@ -38,22 +53,34 @@ struct SideEffects
/// This means it cannot depend on storage or memory, cannot have any side-effects,
/// but it can depend on state that is constant across an EVM-call.
bool movable = true;
/// If true, the expressions in this code can be moved or copied (together with their arguments)
/// across control flow branches and instructions as long as these instructions' 'effects' do
/// not influence the 'effects' of the aforementioned expressions.
bool movableApartFromEffects = true;
/// If true, the code can be removed without changing the semantics.
bool sideEffectFree = true;
bool canBeRemoved = true;
/// If true, the code can be removed without changing the semantics as long as
/// the whole program does not contain the msize instruction.
bool sideEffectFreeIfNoMSize = true;
/// If false, storage is guaranteed to be unchanged by the code under all
/// circumstances.
bool invalidatesStorage = false;
/// If false, memory is guaranteed to be unchanged by the code under all
/// circumstances.
bool invalidatesMemory = false;
bool canBeRemovedIfNoMSize = true;
/// If false, the code calls a for-loop or a recursive function, and therefore potentially loops
/// infinitely. All builtins are set to true by default, even `invalid()`.
bool cannotLoop = true;
/// Can write, read or have no effect on the blockchain state, when the value of `otherState` is
/// `Write`, `Read` or `None` respectively.
Effect otherState = None;
/// Can write, read or have no effect on storage, when the value of `storage` is `Write`, `Read`
/// or `None` respectively. When the value is `Write`, the expression can invalidate storage,
/// potentially indirectly through external calls.
Effect storage = None;
/// Can write, read or have no effect on memory, when the value of `memory` is `Write`, `Read`
/// or `None` respectively. Note that, when the value is `Read`, the expression can have an
/// effect on `msize()`.
Effect memory = None;
/// @returns the worst-case side effects.
static SideEffects worst()
{
return SideEffects{false, false, false, true, true};
return SideEffects{false, false, false, false, false, Write, Write, Write};
}
/// @returns the combined side effects of two pieces of code.
@@ -61,10 +88,13 @@ struct SideEffects
{
return SideEffects{
movable && _other.movable,
sideEffectFree && _other.sideEffectFree,
sideEffectFreeIfNoMSize && _other.sideEffectFreeIfNoMSize,
invalidatesStorage || _other.invalidatesStorage,
invalidatesMemory || _other.invalidatesMemory
movableApartFromEffects && _other.movableApartFromEffects,
canBeRemoved && _other.canBeRemoved,
canBeRemovedIfNoMSize && _other.canBeRemovedIfNoMSize,
cannotLoop && _other.cannotLoop,
otherState + _other.otherState,
storage + _other.storage,
memory + _other.memory
};
}
@@ -79,10 +109,13 @@ struct SideEffects
{
return
movable == _other.movable &&
sideEffectFree == _other.sideEffectFree &&
sideEffectFreeIfNoMSize == _other.sideEffectFreeIfNoMSize &&
invalidatesStorage == _other.invalidatesStorage &&
invalidatesMemory == _other.invalidatesMemory;
movableApartFromEffects == _other.movableApartFromEffects &&
canBeRemoved == _other.canBeRemoved &&
canBeRemovedIfNoMSize == _other.canBeRemovedIfNoMSize &&
cannotLoop == _other.cannotLoop &&
otherState == _other.otherState &&
storage == _other.storage &&
memory == _other.memory;
}
};