mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge remote-tracking branch 'origin/develop' into develop_060
This commit is contained in:
commit
46ac16d25c
@ -568,7 +568,7 @@ jobs:
|
||||
environment:
|
||||
EVM: constantinople
|
||||
OPTIMIZE: 0
|
||||
flags: --no-smt
|
||||
SOLTEST_FLAGS: --no-smt
|
||||
ASAN_OPTIONS: check_initialization_order=true:detect_stack_use_after_return=true:strict_init_order=true:strict_string_checks=true:detect_invalid_pointer_pairs=2
|
||||
|
||||
t_ems_solcjs:
|
||||
|
@ -12,6 +12,7 @@
|
||||
# EVM=version_string Specifies EVM version to compile for (such as homestead, etc)
|
||||
# OPTIMIZE=1 Enables backend optimizer
|
||||
# ABI_ENCODER_V2=1 Enables ABI encoder version 2
|
||||
# SOLTEST_FLAGS=<flags> Appends <flags> to default SOLTEST_ARGS
|
||||
#
|
||||
# ------------------------------------------------------------------------------
|
||||
# This file is part of solidity.
|
||||
@ -54,7 +55,7 @@ get_logfile_basename() {
|
||||
}
|
||||
|
||||
BOOST_TEST_ARGS="--color_output=no --show_progress=yes --logger=JUNIT,error,test_results/`get_logfile_basename`.xml"
|
||||
SOLTEST_ARGS="--evm-version=$EVM --evmonepath /usr/lib/libevmone.so $flags"
|
||||
SOLTEST_ARGS="--evm-version=$EVM --evmonepath /usr/lib/libevmone.so $SOLTEST_FLAGS"
|
||||
test "${OPTIMIZE}" = "1" && SOLTEST_ARGS="${SOLTEST_ARGS} --optimize"
|
||||
test "${ABI_ENCODER_V2}" = "1" && SOLTEST_ARGS="${SOLTEST_ARGS} --abiencoderv2 --optimize-yul"
|
||||
|
||||
|
@ -28,6 +28,7 @@ Language Features:
|
||||
|
||||
Compiler Features:
|
||||
* Code Generator: Use SELFBALANCE for ``address(this).balance`` if using Istanbul EVM
|
||||
* SMTChecker: Support assignments to multi-dimensional arrays and mappings.
|
||||
|
||||
|
||||
Bugfixes:
|
||||
@ -50,6 +51,7 @@ Compiler Features:
|
||||
* SMTChecker: Add loop support to the CHC engine.
|
||||
* Yul Optimizer: Take side-effect-freeness of user-defined functions into account.
|
||||
* Yul Optimizer: Remove redundant mload/sload operations.
|
||||
* Yul Optimizer: Use the fact that branch conditions have certain value inside the branch.
|
||||
|
||||
|
||||
Bugfixes:
|
||||
|
@ -783,78 +783,79 @@ void SMTEncoder::arrayAssignment()
|
||||
|
||||
void SMTEncoder::arrayIndexAssignment(Expression const& _expr, smt::Expression const& _rightHandSide)
|
||||
{
|
||||
auto const& indexAccess = dynamic_cast<IndexAccess const&>(_expr);
|
||||
if (auto const& id = dynamic_cast<Identifier const*>(&indexAccess.baseExpression()))
|
||||
auto toStore = _rightHandSide;
|
||||
auto indexAccess = dynamic_cast<IndexAccess const*>(&_expr);
|
||||
solAssert(indexAccess, "");
|
||||
while (true)
|
||||
{
|
||||
auto varDecl = identifierToVariable(*id);
|
||||
solAssert(varDecl, "");
|
||||
|
||||
if (varDecl->hasReferenceOrMappingType())
|
||||
m_context.resetVariables([&](VariableDeclaration const& _var) {
|
||||
if (_var == *varDecl)
|
||||
return false;
|
||||
|
||||
// If both are state variables no need to clear knowledge.
|
||||
if (_var.isStateVariable() && varDecl->isStateVariable())
|
||||
return false;
|
||||
|
||||
TypePointer prefix = _var.type();
|
||||
TypePointer originalType = typeWithoutPointer(varDecl->type());
|
||||
while (
|
||||
prefix->category() == Type::Category::Mapping ||
|
||||
prefix->category() == Type::Category::Array
|
||||
)
|
||||
{
|
||||
if (*originalType == *typeWithoutPointer(prefix))
|
||||
return true;
|
||||
if (prefix->category() == Type::Category::Mapping)
|
||||
{
|
||||
auto mapPrefix = dynamic_cast<MappingType const*>(prefix);
|
||||
solAssert(mapPrefix, "");
|
||||
prefix = mapPrefix->valueType();
|
||||
}
|
||||
else
|
||||
{
|
||||
auto arrayPrefix = dynamic_cast<ArrayType const*>(prefix);
|
||||
solAssert(arrayPrefix, "");
|
||||
prefix = arrayPrefix->baseType();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
smt::Expression store = smt::Expression::store(
|
||||
m_context.variable(*varDecl)->currentValue(),
|
||||
expr(*indexAccess.indexExpression()),
|
||||
_rightHandSide
|
||||
);
|
||||
m_context.addAssertion(m_context.newValue(*varDecl) == store);
|
||||
// Update the SMT select value after the assignment,
|
||||
// necessary for sound models.
|
||||
defineExpr(indexAccess, smt::Expression::select(
|
||||
m_context.variable(*varDecl)->currentValue(),
|
||||
expr(*indexAccess.indexExpression())
|
||||
));
|
||||
}
|
||||
else if (dynamic_cast<IndexAccess const*>(&indexAccess.baseExpression()))
|
||||
{
|
||||
auto identifier = dynamic_cast<Identifier const*>(leftmostBase(indexAccess));
|
||||
if (identifier)
|
||||
if (auto const& id = dynamic_cast<Identifier const*>(&indexAccess->baseExpression()))
|
||||
{
|
||||
auto varDecl = identifierToVariable(*identifier);
|
||||
m_context.newValue(*varDecl);
|
||||
}
|
||||
auto varDecl = identifierToVariable(*id);
|
||||
solAssert(varDecl, "");
|
||||
|
||||
m_errorReporter.warning(
|
||||
indexAccess.location(),
|
||||
"Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays."
|
||||
);
|
||||
if (varDecl->hasReferenceOrMappingType())
|
||||
m_context.resetVariables([&](VariableDeclaration const& _var) {
|
||||
if (_var == *varDecl)
|
||||
return false;
|
||||
|
||||
// If both are state variables no need to clear knowledge.
|
||||
if (_var.isStateVariable() && varDecl->isStateVariable())
|
||||
return false;
|
||||
|
||||
TypePointer prefix = _var.type();
|
||||
TypePointer originalType = typeWithoutPointer(varDecl->type());
|
||||
while (
|
||||
prefix->category() == Type::Category::Mapping ||
|
||||
prefix->category() == Type::Category::Array
|
||||
)
|
||||
{
|
||||
if (*originalType == *typeWithoutPointer(prefix))
|
||||
return true;
|
||||
if (prefix->category() == Type::Category::Mapping)
|
||||
{
|
||||
auto mapPrefix = dynamic_cast<MappingType const*>(prefix);
|
||||
solAssert(mapPrefix, "");
|
||||
prefix = mapPrefix->valueType();
|
||||
}
|
||||
else
|
||||
{
|
||||
auto arrayPrefix = dynamic_cast<ArrayType const*>(prefix);
|
||||
solAssert(arrayPrefix, "");
|
||||
prefix = arrayPrefix->baseType();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
smt::Expression store = smt::Expression::store(
|
||||
m_context.variable(*varDecl)->currentValue(),
|
||||
expr(*indexAccess->indexExpression()),
|
||||
toStore
|
||||
);
|
||||
m_context.addAssertion(m_context.newValue(*varDecl) == store);
|
||||
// Update the SMT select value after the assignment,
|
||||
// necessary for sound models.
|
||||
defineExpr(*indexAccess, smt::Expression::select(
|
||||
m_context.variable(*varDecl)->currentValue(),
|
||||
expr(*indexAccess->indexExpression())
|
||||
));
|
||||
|
||||
break;
|
||||
}
|
||||
else if (auto base = dynamic_cast<IndexAccess const*>(&indexAccess->baseExpression()))
|
||||
{
|
||||
toStore = smt::Expression::store(expr(*base), expr(*indexAccess->indexExpression()), toStore);
|
||||
indexAccess = base;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_errorReporter.warning(
|
||||
_expr.location(),
|
||||
"Assertion checker does not yet implement this expression."
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
m_errorReporter.warning(
|
||||
_expr.location(),
|
||||
"Assertion checker does not yet implement this expression."
|
||||
);
|
||||
}
|
||||
|
||||
void SMTEncoder::defineGlobalVariable(string const& _name, Expression const& _expr, bool _increaseIndex)
|
||||
|
@ -66,6 +66,10 @@ add_library(yul
|
||||
optimiser/CallGraphGenerator.h
|
||||
optimiser/CommonSubexpressionEliminator.cpp
|
||||
optimiser/CommonSubexpressionEliminator.h
|
||||
optimiser/ConditionalSimplifier.cpp
|
||||
optimiser/ConditionalSimplifier.h
|
||||
optimiser/ConditionalUnsimplifier.cpp
|
||||
optimiser/ConditionalUnsimplifier.h
|
||||
optimiser/ControlFlowSimplifier.cpp
|
||||
optimiser/ControlFlowSimplifier.h
|
||||
optimiser/DataFlowAnalyzer.cpp
|
||||
|
@ -48,7 +48,7 @@ namespace
|
||||
{
|
||||
static string const polyfill{R"({
|
||||
function or_bool(a, b, c, d) -> r {
|
||||
r := i64.ne(0, i64.or(i64.or(a, b), i64.or(c, d)))
|
||||
r := i64.or(i64.or(a, b), i64.or(c, d))
|
||||
}
|
||||
// returns a + y + c plus carry value on 64 bit values.
|
||||
// c should be at most 1
|
||||
@ -205,9 +205,7 @@ function cmp(a, b) -> r {
|
||||
switch i64.lt_u(a, b)
|
||||
case 1 { r := 0xffffffffffffffff }
|
||||
default {
|
||||
switch i64.gt_u(a, b)
|
||||
case 1 { r := 1 }
|
||||
default { r := 0 }
|
||||
r := i64.ne(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
@ -218,10 +216,7 @@ function lt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
|
||||
case 0 {
|
||||
switch cmp(x3, y3)
|
||||
case 0 {
|
||||
switch cmp(x4, y4)
|
||||
case 0 { z4 := 0 }
|
||||
case 1 { z4 := 0 }
|
||||
default { z4 := 1 }
|
||||
z4 := i64.lt_u(x4, y4)
|
||||
}
|
||||
case 1 { z4 := 0 }
|
||||
default { z4 := 1 }
|
||||
@ -245,13 +240,78 @@ function sgt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
|
||||
z1, z2, z3, z4 := slt(y1, y2, y3, y4, x1, x2, x3, x4)
|
||||
}
|
||||
|
||||
function shl(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
|
||||
// TODO implement
|
||||
unreachable()
|
||||
function shl_single(a, amount) -> x, y {
|
||||
// amount < 64
|
||||
x := i64.shr_u(a, i64.sub(64, amount))
|
||||
y := i64.shl(a, amount)
|
||||
}
|
||||
|
||||
function shl(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
|
||||
if i64.and(i64.eqz(x1), i64.eqz(x2)) {
|
||||
if i64.eqz(x3) {
|
||||
if i64.lt_u(x4, 256) {
|
||||
if i64.ge_u(x4, 128) {
|
||||
y1 := y3
|
||||
y2 := y4
|
||||
y3 := 0
|
||||
y4 := 0
|
||||
x4 := i64.sub(x4, 128)
|
||||
}
|
||||
if i64.ge_u(x4, 64) {
|
||||
y1 := y2
|
||||
y2 := y3
|
||||
y3 := y4
|
||||
y4 := 0
|
||||
x4 := i64.sub(x4, 64)
|
||||
}
|
||||
let t, r
|
||||
t, z4 := shl_single(y4, x4)
|
||||
r, z3 := shl_single(y3, x4)
|
||||
z3 := i64.or(z3, t)
|
||||
t, z2 := shl_single(y2, x4)
|
||||
z2 := i64.or(z2, r)
|
||||
r, z1 := shl_single(y1, x4)
|
||||
z1 := i64.or(z1, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function shr_single(a, amount) -> x, y {
|
||||
// amount < 64
|
||||
y := i64.shl(a, i64.sub(64, amount))
|
||||
x := i64.shr_u(a, amount)
|
||||
}
|
||||
|
||||
function shr(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
|
||||
// TODO implement
|
||||
unreachable()
|
||||
if i64.and(i64.eqz(x1), i64.eqz(x2)) {
|
||||
if i64.eqz(x3) {
|
||||
if i64.lt_u(x4, 256) {
|
||||
if i64.ge_u(x4, 128) {
|
||||
y4 := y2
|
||||
y3 := y1
|
||||
y2 := 0
|
||||
y1 := 0
|
||||
x4 := i64.sub(x4, 128)
|
||||
}
|
||||
if i64.ge_u(x4, 64) {
|
||||
y4 := y3
|
||||
y3 := y2
|
||||
y2 := y1
|
||||
y1 := 0
|
||||
x4 := i64.sub(x4, 64)
|
||||
}
|
||||
let t
|
||||
z4, t := shr_single(y4, x4)
|
||||
z3, t := shr_single(y3, x4)
|
||||
z4 := i64.or(z4, t)
|
||||
z2, t := shr_single(y2, x4)
|
||||
z3 := i64.or(z3, t)
|
||||
z1, t := shr_single(y1, x4)
|
||||
z2 := i64.or(z2, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function sar(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
|
||||
// TODO implement
|
||||
@ -291,45 +351,36 @@ function keccak256(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {
|
||||
}
|
||||
|
||||
function address() -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4 := save_temp_mem_32()
|
||||
eth.getAddress(0)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 0)
|
||||
restore_temp_mem_32(t1, t2, t3, t4)
|
||||
z1, z2, z3, z4 := mload_internal(0)
|
||||
}
|
||||
function balance(x1, x2, x3, x4) -> z1, z2, z3, z4 {
|
||||
// TODO implement
|
||||
unreachable()
|
||||
}
|
||||
function origin() -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4 := save_temp_mem_32()
|
||||
eth.getTxOrigin(0)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 0)
|
||||
restore_temp_mem_32(t1, t2, t3, t4)
|
||||
z1, z2, z3, z4 := mload_internal(0)
|
||||
}
|
||||
function caller() -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4 := save_temp_mem_32()
|
||||
eth.getCaller(0)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 0)
|
||||
restore_temp_mem_32(t1, t2, t3, t4)
|
||||
z1, z2, z3, z4 := mload_internal(0)
|
||||
}
|
||||
function callvalue() -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4 := save_temp_mem_32()
|
||||
eth.getCallValue(0)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 0)
|
||||
restore_temp_mem_32(t1, t2, t3, t4)
|
||||
z1, z2, z3, z4 := mload_internal(0)
|
||||
}
|
||||
function calldataload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4 := save_temp_mem_32()
|
||||
eth.callDataCopy(0, u256_to_i32(x1, x2, x3, x4), 32)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 0)
|
||||
restore_temp_mem_32(t1, t2, t3, t4)
|
||||
z1, z2, z3, z4 := mload_internal(0)
|
||||
}
|
||||
function calldatasize() -> z1, z2, z3, z4 {
|
||||
z4 := eth.getCallDataSize()
|
||||
}
|
||||
function calldatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
|
||||
eth.callDataCopy(
|
||||
u256_to_i32ptr(x1, x2, x3, x4),
|
||||
// scratch - TODO: overflow check
|
||||
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
|
||||
u256_to_i32(y1, y2, y3, y4),
|
||||
u256_to_i32(z1, z2, z3, z4)
|
||||
)
|
||||
@ -337,14 +388,13 @@ function calldatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
|
||||
|
||||
// Needed?
|
||||
function codesize() -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4 := save_temp_mem_32()
|
||||
eth.getCodeSize(0)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 0)
|
||||
restore_temp_mem_32(t1, t2, t3, t4)
|
||||
z1, z2, z3, z4 := mload_internal(0)
|
||||
}
|
||||
function codecopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
|
||||
eth.codeCopy(
|
||||
u256_to_i32ptr(x1, x2, x3, x4),
|
||||
// scratch - TODO: overflow check
|
||||
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
|
||||
u256_to_i32(y1, y2, y3, y4),
|
||||
u256_to_i32(z1, z2, z3, z4)
|
||||
)
|
||||
@ -355,10 +405,8 @@ function datacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
|
||||
}
|
||||
|
||||
function gasprice() -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4 := save_temp_mem_32()
|
||||
eth.getTxGasPrice(0)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 0)
|
||||
restore_temp_mem_32(t1, t2, t3, t4)
|
||||
z1, z2, z3, z4 := mload_internal(0)
|
||||
}
|
||||
function extcodesize(x1, x2, x3, x4) -> z1, z2, z3, z4 {
|
||||
// TODO implement
|
||||
@ -378,7 +426,8 @@ function returndatasize() -> z1, z2, z3, z4 {
|
||||
}
|
||||
function returndatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
|
||||
eth.returnDataCopy(
|
||||
u256_to_i32ptr(x1, x2, x3, x4),
|
||||
// scratch - TODO: overflow check
|
||||
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
|
||||
u256_to_i32(y1, y2, y3, y4),
|
||||
u256_to_i32(z1, z2, z3, z4)
|
||||
)
|
||||
@ -399,10 +448,8 @@ function number() -> z1, z2, z3, z4 {
|
||||
z4 := eth.getBlockNumber()
|
||||
}
|
||||
function difficulty() -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4 := save_temp_mem_32()
|
||||
eth.getBlockDifficulty(0)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 0)
|
||||
restore_temp_mem_32(t1, t2, t3, t4)
|
||||
z1, z2, z3, z4 := mload_internal(0)
|
||||
}
|
||||
function gaslimit() -> z1, z2, z3, z4 {
|
||||
z4 := eth.getBlockGasLimit()
|
||||
@ -463,6 +510,12 @@ function restore_temp_mem_64(t1, t2, t3, t4, t5, t6, t7, t8) {
|
||||
}
|
||||
function mload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
|
||||
let pos := u256_to_i32ptr(x1, x2, x3, x4)
|
||||
// Make room for the scratch space
|
||||
// TODO do we need to check for overflow?
|
||||
pos := i64.add(pos, 64)
|
||||
z1, z2, z3, z4 := mload_internal(pos)
|
||||
}
|
||||
function mload_internal(pos) -> z1, z2, z3, z4 {
|
||||
z1 := endian_swap(i64.load(pos))
|
||||
z2 := endian_swap(i64.load(i64.add(pos, 8)))
|
||||
z3 := endian_swap(i64.load(i64.add(pos, 16)))
|
||||
@ -470,10 +523,16 @@ function mload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
|
||||
}
|
||||
function mstore(x1, x2, x3, x4, y1, y2, y3, y4) {
|
||||
let pos := u256_to_i32ptr(x1, x2, x3, x4)
|
||||
i64.store(pos, endian_swap(x1))
|
||||
i64.store(i64.add(pos, 8), endian_swap(x2))
|
||||
i64.store(i64.add(pos, 16), endian_swap(x3))
|
||||
i64.store(i64.add(pos, 24), endian_swap(x4))
|
||||
// Make room for the scratch space
|
||||
// TODO do we need to check for overflow?
|
||||
pos := i64.add(pos, 64)
|
||||
mstore_internal(pos, y1, y2, y3, y4)
|
||||
}
|
||||
function mstore_internal(pos, y1, y2, y3, y4) {
|
||||
i64.store(pos, endian_swap(y1))
|
||||
i64.store(i64.add(pos, 8), endian_swap(y2))
|
||||
i64.store(i64.add(pos, 16), endian_swap(y3))
|
||||
i64.store(i64.add(pos, 24), endian_swap(y4))
|
||||
}
|
||||
function mstore8(x1, x2, x3, x4, y1, y2, y3, y4) {
|
||||
// TODO implement
|
||||
@ -485,19 +544,15 @@ function msize() -> z1, z2, z3, z4 {
|
||||
unreachable()
|
||||
}
|
||||
function sload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
|
||||
let t1, t2, t3, t4, t5, t6, t7, t8 := save_temp_mem_64()
|
||||
mstore(0, 0, 0, 0, x1, x2, x3, x4)
|
||||
eth.storageLoad(0, 16)
|
||||
z1, z2, z3, z4 := mload(0, 0, 0, 16)
|
||||
restore_temp_mem_64(t1, t2, t3, t4, t5, t6, t7, t8)
|
||||
mstore_internal(0, x1, x2, x3, x4)
|
||||
eth.storageLoad(0, 32)
|
||||
z1, z2, z3, z4 := mload_internal(32)
|
||||
}
|
||||
|
||||
function sstore(x1, x2, x3, x4, y1, y2, y3, y4) {
|
||||
let t1, t2, t3, t4, t5, t6, t7, t8 := save_temp_mem_64()
|
||||
mstore(0, 0, 0, 0, x1, x2, x3, x4)
|
||||
mstore(0, 0, 0, 32, y1, y2, y3, y4)
|
||||
mstore_internal(0, x1, x2, x3, x4)
|
||||
mstore_internal(32, y1, y2, y3, y4)
|
||||
eth.storageStore(0, 32)
|
||||
restore_temp_mem_64(t1, t2, t3, t4, t5, t6, t7, t8)
|
||||
}
|
||||
|
||||
// Needed?
|
||||
@ -615,13 +670,15 @@ function selfdestruct(a1, a2, a3, a4) {
|
||||
|
||||
function return(x1, x2, x3, x4, y1, y2, y3, y4) {
|
||||
eth.finish(
|
||||
u256_to_i32ptr(x1, x2, x3, x4),
|
||||
// scratch - TODO: overflow check
|
||||
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
|
||||
u256_to_i32(y1, y2, y3, y4)
|
||||
)
|
||||
}
|
||||
function revert(x1, x2, x3, x4, y1, y2, y3, y4) {
|
||||
eth.revert(
|
||||
u256_to_i32ptr(x1, x2, x3, x4),
|
||||
// scratch - TODO: overflow check
|
||||
i64.add(u256_to_i32ptr(x1, x2, x3, x4), 64),
|
||||
u256_to_i32(y1, y2, y3, y4)
|
||||
)
|
||||
}
|
||||
@ -653,6 +710,7 @@ Object EVMToEWasmTranslator::run(Object const& _object)
|
||||
ast.statements.emplace_back(ASTCopier{}.translate(st));
|
||||
|
||||
Object ret;
|
||||
ret.name = _object.name;
|
||||
ret.code = make_shared<Block>(move(ast));
|
||||
ret.analysisInfo = make_shared<AsmAnalysisInfo>();
|
||||
|
||||
|
@ -121,6 +121,8 @@ wasm::Expression EWasmCodeTransform::operator()(FunctionalInstruction const& _f)
|
||||
|
||||
wasm::Expression EWasmCodeTransform::operator()(FunctionCall const& _call)
|
||||
{
|
||||
bool typeConversionNeeded = false;
|
||||
|
||||
if (BuiltinFunction const* builtin = m_dialect.builtin(_call.functionName.name))
|
||||
{
|
||||
if (_call.functionName.name.str().substr(0, 4) == "eth.")
|
||||
@ -140,6 +142,7 @@ wasm::Expression EWasmCodeTransform::operator()(FunctionCall const& _call)
|
||||
imp.paramTypes.emplace_back(param.str());
|
||||
m_functionsToImport[builtin->name] = std::move(imp);
|
||||
}
|
||||
typeConversionNeeded = true;
|
||||
}
|
||||
else if (builtin->literalArguments)
|
||||
{
|
||||
@ -149,14 +152,32 @@ wasm::Expression EWasmCodeTransform::operator()(FunctionCall const& _call)
|
||||
return wasm::BuiltinCall{_call.functionName.name.str(), std::move(literals)};
|
||||
}
|
||||
else
|
||||
return wasm::BuiltinCall{_call.functionName.name.str(), visit(_call.arguments)};
|
||||
{
|
||||
wasm::BuiltinCall call{
|
||||
_call.functionName.name.str(),
|
||||
injectTypeConversionIfNeeded(visit(_call.arguments), builtin->parameters)
|
||||
};
|
||||
if (!builtin->returns.empty() && !builtin->returns.front().empty() && builtin->returns.front() != "i64"_yulstring)
|
||||
{
|
||||
yulAssert(builtin->returns.front() == "i32"_yulstring, "Invalid type " + builtin->returns.front().str());
|
||||
call = wasm::BuiltinCall{"i64.extend_i32_u", make_vector<wasm::Expression>(std::move(call))};
|
||||
}
|
||||
return {std::move(call)};
|
||||
}
|
||||
}
|
||||
|
||||
// If this function returns multiple values, then the first one will
|
||||
// be returned in the expression itself and the others in global variables.
|
||||
// The values have to be used right away in an assignment or variable declaration,
|
||||
// so it is handled there.
|
||||
return wasm::FunctionCall{_call.functionName.name.str(), visit(_call.arguments)};
|
||||
|
||||
wasm::FunctionCall funCall{_call.functionName.name.str(), visit(_call.arguments)};
|
||||
if (typeConversionNeeded)
|
||||
// Inject type conversion if needed on the fly. This is just a temporary measure
|
||||
// and can be removed once we have proper types in Yul.
|
||||
return injectTypeConversionIfNeeded(std::move(funCall));
|
||||
else
|
||||
return {std::move(funCall)};
|
||||
}
|
||||
|
||||
wasm::Expression EWasmCodeTransform::operator()(Identifier const& _identifier)
|
||||
@ -173,7 +194,16 @@ wasm::Expression EWasmCodeTransform::operator()(Literal const& _literal)
|
||||
|
||||
wasm::Expression EWasmCodeTransform::operator()(If const& _if)
|
||||
{
|
||||
return wasm::If{visit(*_if.condition), visit(_if.body.statements), {}};
|
||||
// TODO converting i64 to i32 might not always be needed.
|
||||
|
||||
vector<wasm::Expression> args;
|
||||
args.emplace_back(visitReturnByValue(*_if.condition));
|
||||
args.emplace_back(wasm::Literal{0});
|
||||
return wasm::If{
|
||||
make_unique<wasm::Expression>(wasm::BuiltinCall{"i64.ne", std::move(args)}),
|
||||
visit(_if.body.statements),
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
||||
wasm::Expression EWasmCodeTransform::operator()(Switch const& _switch)
|
||||
@ -323,6 +353,40 @@ wasm::FunctionDefinition EWasmCodeTransform::translateFunction(yul::FunctionDefi
|
||||
return fun;
|
||||
}
|
||||
|
||||
wasm::Expression EWasmCodeTransform::injectTypeConversionIfNeeded(wasm::FunctionCall _call) const
|
||||
{
|
||||
wasm::FunctionImport const& import = m_functionsToImport.at(YulString{_call.functionName});
|
||||
for (size_t i = 0; i < _call.arguments.size(); ++i)
|
||||
if (import.paramTypes.at(i) == "i32")
|
||||
_call.arguments[i] = wasm::BuiltinCall{"i32.wrap_i64", make_vector<wasm::Expression>(std::move(_call.arguments[i]))};
|
||||
else
|
||||
yulAssert(import.paramTypes.at(i) == "i64", "Unknown type " + import.paramTypes.at(i));
|
||||
|
||||
if (import.returnType && *import.returnType != "i64")
|
||||
{
|
||||
yulAssert(*import.returnType == "i32", "Invalid type " + *import.returnType);
|
||||
return wasm::BuiltinCall{"i64.extend_i32_u", make_vector<wasm::Expression>(std::move(_call))};
|
||||
}
|
||||
return {std::move(_call)};
|
||||
}
|
||||
|
||||
vector<wasm::Expression> EWasmCodeTransform::injectTypeConversionIfNeeded(
|
||||
vector<wasm::Expression> _arguments,
|
||||
vector<Type> const& _parameterTypes
|
||||
) const
|
||||
{
|
||||
for (size_t i = 0; i < _arguments.size(); ++i)
|
||||
if (_parameterTypes.at(i) == "i32"_yulstring)
|
||||
_arguments[i] = wasm::BuiltinCall{"i32.wrap_i64", make_vector<wasm::Expression>(std::move(_arguments[i]))};
|
||||
else
|
||||
yulAssert(
|
||||
_parameterTypes.at(i).empty() || _parameterTypes.at(i) == "i64"_yulstring,
|
||||
"Unknown type " + _parameterTypes.at(i).str()
|
||||
);
|
||||
|
||||
return _arguments;
|
||||
}
|
||||
|
||||
string EWasmCodeTransform::newLabel()
|
||||
{
|
||||
return m_nameDispenser.newName("label_"_yulstring).str();
|
||||
|
@ -80,6 +80,12 @@ private:
|
||||
|
||||
wasm::FunctionDefinition translateFunction(yul::FunctionDefinition const& _funDef);
|
||||
|
||||
wasm::Expression injectTypeConversionIfNeeded(wasm::FunctionCall _call) const;
|
||||
std::vector<wasm::Expression> injectTypeConversionIfNeeded(
|
||||
std::vector<wasm::Expression> _arguments,
|
||||
std::vector<yul::Type> const& _parameterTypes
|
||||
) const;
|
||||
|
||||
std::string newLabel();
|
||||
/// Makes sure that there are at least @a _amount global variables.
|
||||
void allocateGlobals(size_t _amount);
|
||||
|
@ -74,12 +74,12 @@ string EWasmToText::operator()(wasm::StringLiteral const& _literal)
|
||||
|
||||
string EWasmToText::operator()(wasm::LocalVariable const& _identifier)
|
||||
{
|
||||
return "(get_local $" + _identifier.name + ")";
|
||||
return "(local.get $" + _identifier.name + ")";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::GlobalVariable const& _identifier)
|
||||
{
|
||||
return "(get_global $" + _identifier.name + ")";
|
||||
return "(global.get $" + _identifier.name + ")";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::BuiltinCall const& _builtinCall)
|
||||
@ -96,12 +96,12 @@ string EWasmToText::operator()(wasm::FunctionCall const& _functionCall)
|
||||
|
||||
string EWasmToText::operator()(wasm::LocalAssignment const& _assignment)
|
||||
{
|
||||
return "(set_local $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
|
||||
return "(local.set $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::GlobalAssignment const& _assignment)
|
||||
{
|
||||
return "(set_global $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
|
||||
return "(global.set $" + _assignment.variableName + " " + visit(*_assignment.value) + ")\n";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::If const& _if)
|
||||
|
@ -46,12 +46,22 @@ WasmDialect::WasmDialect():
|
||||
})
|
||||
addFunction(name, 2, 1);
|
||||
|
||||
m_functions["i64.lt_u"_yulstring].returns.front() = "i32"_yulstring;
|
||||
m_functions["i64.gt_u"_yulstring].returns.front() = "i32"_yulstring;
|
||||
m_functions["i64.le_u"_yulstring].returns.front() = "i32"_yulstring;
|
||||
m_functions["i64.ge_u"_yulstring].returns.front() = "i32"_yulstring;
|
||||
m_functions["i64.eq"_yulstring].returns.front() = "i32"_yulstring;
|
||||
m_functions["i64.ne"_yulstring].returns.front() = "i32"_yulstring;
|
||||
|
||||
addFunction("i64.eqz", 1, 1);
|
||||
m_functions["i64.eqz"_yulstring].returns.front() = "i32"_yulstring;
|
||||
|
||||
addFunction("i64.store", 2, 0, false);
|
||||
m_functions["i64.store"_yulstring].parameters.front() = "i32"_yulstring;
|
||||
m_functions["i64.store"_yulstring].sideEffects.invalidatesStorage = false;
|
||||
|
||||
addFunction("i64.load", 1, 1, false);
|
||||
m_functions["i64.load"_yulstring].parameters.front() = "i32"_yulstring;
|
||||
m_functions["i64.load"_yulstring].sideEffects.invalidatesStorage = false;
|
||||
m_functions["i64.load"_yulstring].sideEffects.invalidatesMemory = false;
|
||||
m_functions["i64.load"_yulstring].sideEffects.sideEffectFree = true;
|
||||
@ -63,8 +73,8 @@ WasmDialect::WasmDialect():
|
||||
m_functions["unreachable"_yulstring].sideEffects.invalidatesStorage = false;
|
||||
m_functions["unreachable"_yulstring].sideEffects.invalidatesMemory = false;
|
||||
|
||||
addFunction("datasize", 1, 4, true, true);
|
||||
addFunction("dataoffset", 1, 4, true, true);
|
||||
addFunction("datasize", 1, 1, true, true);
|
||||
addFunction("dataoffset", 1, 1, true, true);
|
||||
|
||||
addEthereumExternals();
|
||||
}
|
||||
|
@ -87,6 +87,30 @@ void WordSizeTransform::operator()(Block& _block)
|
||||
if (_s.type() == typeid(VariableDeclaration))
|
||||
{
|
||||
VariableDeclaration& varDecl = boost::get<VariableDeclaration>(_s);
|
||||
|
||||
// Special handling for datasize and dataoffset - they will only need one variable.
|
||||
if (varDecl.value && varDecl.value->type() == typeid(FunctionCall))
|
||||
if (BuiltinFunction const* f = m_inputDialect.builtin(boost::get<FunctionCall>(*varDecl.value).functionName.name))
|
||||
if (f->literalArguments)
|
||||
{
|
||||
yulAssert(f->name == "datasize"_yulstring || f->name == "dataoffset"_yulstring, "");
|
||||
yulAssert(varDecl.variables.size() == 1, "");
|
||||
auto newLhs = generateU64IdentifierNames(varDecl.variables[0].name);
|
||||
vector<Statement> ret;
|
||||
for (int i = 0; i < 3; i++)
|
||||
ret.push_back(VariableDeclaration{
|
||||
varDecl.location,
|
||||
{TypedName{varDecl.location, newLhs[i], "u64"_yulstring}},
|
||||
make_unique<Expression>(Literal{locationOf(*varDecl.value), LiteralKind::Number, "0"_yulstring, "u64"_yulstring})
|
||||
});
|
||||
ret.push_back(VariableDeclaration{
|
||||
varDecl.location,
|
||||
{TypedName{varDecl.location, newLhs[3], "u64"_yulstring}},
|
||||
std::move(varDecl.value)
|
||||
});
|
||||
return {std::move(ret)};
|
||||
}
|
||||
|
||||
if (
|
||||
!varDecl.value ||
|
||||
varDecl.value->type() == typeid(FunctionalInstruction) ||
|
||||
@ -123,6 +147,30 @@ void WordSizeTransform::operator()(Block& _block)
|
||||
{
|
||||
Assignment& assignment = boost::get<Assignment>(_s);
|
||||
yulAssert(assignment.value, "");
|
||||
|
||||
// Special handling for datasize and dataoffset - they will only need one variable.
|
||||
if (assignment.value->type() == typeid(FunctionCall))
|
||||
if (BuiltinFunction const* f = m_inputDialect.builtin(boost::get<FunctionCall>(*assignment.value).functionName.name))
|
||||
if (f->literalArguments)
|
||||
{
|
||||
yulAssert(f->name == "datasize"_yulstring || f->name == "dataoffset"_yulstring, "");
|
||||
yulAssert(assignment.variableNames.size() == 1, "");
|
||||
auto newLhs = generateU64IdentifierNames(assignment.variableNames[0].name);
|
||||
vector<Statement> ret;
|
||||
for (int i = 0; i < 3; i++)
|
||||
ret.push_back(Assignment{
|
||||
assignment.location,
|
||||
{Identifier{assignment.location, newLhs[i]}},
|
||||
make_unique<Expression>(Literal{locationOf(*assignment.value), LiteralKind::Number, "0"_yulstring, "u64"_yulstring})
|
||||
});
|
||||
ret.push_back(Assignment{
|
||||
assignment.location,
|
||||
{Identifier{assignment.location, newLhs[3]}},
|
||||
std::move(assignment.value)
|
||||
});
|
||||
return {std::move(ret)};
|
||||
}
|
||||
|
||||
if (
|
||||
assignment.value->type() == typeid(FunctionalInstruction) ||
|
||||
assignment.value->type() == typeid(FunctionCall)
|
||||
|
92
libyul/optimiser/ConditionalSimplifier.cpp
Normal file
92
libyul/optimiser/ConditionalSimplifier.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <libyul/optimiser/ConditionalSimplifier.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/AsmData.h>
|
||||
#include <libyul/Utilities.h>
|
||||
#include <libyul/optimiser/NameCollector.h>
|
||||
#include <libdevcore/CommonData.h>
|
||||
#include <libdevcore/Visitor.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
void ConditionalSimplifier::operator()(Switch& _switch)
|
||||
{
|
||||
visit(*_switch.expression);
|
||||
if (_switch.expression->type() != typeid(Identifier))
|
||||
{
|
||||
ASTModifier::operator()(_switch);
|
||||
return;
|
||||
}
|
||||
YulString expr = boost::get<Identifier>(*_switch.expression).name;
|
||||
for (auto& _case: _switch.cases)
|
||||
{
|
||||
if (_case.value)
|
||||
{
|
||||
(*this)(*_case.value);
|
||||
_case.body.statements.insert(_case.body.statements.begin(),
|
||||
Assignment{
|
||||
_case.body.location,
|
||||
{Identifier{_case.body.location, expr}},
|
||||
make_unique<Expression>(*_case.value)
|
||||
}
|
||||
);
|
||||
}
|
||||
(*this)(_case.body);
|
||||
}
|
||||
}
|
||||
|
||||
void ConditionalSimplifier::operator()(Block& _block)
|
||||
{
|
||||
iterateReplacing(
|
||||
_block.statements,
|
||||
[&](Statement& _s) -> std::optional<vector<Statement>>
|
||||
{
|
||||
visit(_s);
|
||||
if (_s.type() == typeid(If))
|
||||
{
|
||||
If& _if = boost::get<If>(_s);
|
||||
if (
|
||||
_if.condition->type() == typeid(Identifier) &&
|
||||
!_if.body.statements.empty() &&
|
||||
TerminationFinder(m_dialect).controlFlowKind(_if.body.statements.back()) !=
|
||||
TerminationFinder::ControlFlow::FlowOut
|
||||
)
|
||||
{
|
||||
YulString condition = boost::get<Identifier>(*_if.condition).name;
|
||||
langutil::SourceLocation location = _if.location;
|
||||
return make_vector<Statement>(
|
||||
std::move(_s),
|
||||
Assignment{
|
||||
location,
|
||||
{Identifier{location, condition}},
|
||||
make_unique<Expression>(Literal{
|
||||
location,
|
||||
LiteralKind::Number,
|
||||
"0"_yulstring,
|
||||
{}
|
||||
})
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
);
|
||||
}
|
71
libyul/optimiser/ConditionalSimplifier.h
Normal file
71
libyul/optimiser/ConditionalSimplifier.h
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <libyul/optimiser/OptimiserStep.h>
|
||||
#include <libyul/Dialect.h>
|
||||
#include <libdevcore/Common.h>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
|
||||
/**
|
||||
* Conditional simplifier.
|
||||
*
|
||||
* Inserts assignments to condition variables if the value can be determined
|
||||
* from the control-flow.
|
||||
*
|
||||
* Destroys SSA form.
|
||||
*
|
||||
* Currently, this tool is very limited, mostly because we do not yet have support
|
||||
* for boolean types. Since conditions only check for expressions being nonzero,
|
||||
* we cannot assign a specific value.
|
||||
*
|
||||
* Current features:
|
||||
* - switch cases: insert "<condition> := <caseLabel>"
|
||||
* - after if statement with terminating control-flow, insert "<condition> := 0"
|
||||
*
|
||||
* Future features:
|
||||
* - allow replacements by "1"
|
||||
* - take termination of user-defined functions into account
|
||||
*
|
||||
* Works best with SSA form and if dead code removal has run before.
|
||||
*
|
||||
* Prerequisite: Disambiguator.
|
||||
*/
|
||||
class ConditionalSimplifier: public ASTModifier
|
||||
{
|
||||
public:
|
||||
static constexpr char const* name{"ConditionalSimplifier"};
|
||||
static void run(OptimiserStepContext& _context, Block& _ast)
|
||||
{
|
||||
ConditionalSimplifier{_context.dialect}(_ast);
|
||||
}
|
||||
|
||||
using ASTModifier::operator();
|
||||
void operator()(Switch& _switch) override;
|
||||
void operator()(Block& _block) override;
|
||||
|
||||
private:
|
||||
explicit ConditionalSimplifier(Dialect const& _dialect):
|
||||
m_dialect(_dialect)
|
||||
{}
|
||||
Dialect const& m_dialect;
|
||||
};
|
||||
|
||||
}
|
98
libyul/optimiser/ConditionalUnsimplifier.cpp
Normal file
98
libyul/optimiser/ConditionalUnsimplifier.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <libyul/optimiser/ConditionalUnsimplifier.h>
|
||||
#include <libyul/optimiser/Semantics.h>
|
||||
#include <libyul/AsmData.h>
|
||||
#include <libyul/Utilities.h>
|
||||
#include <libyul/optimiser/NameCollector.h>
|
||||
#include <libdevcore/CommonData.h>
|
||||
#include <libdevcore/Visitor.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
|
||||
void ConditionalUnsimplifier::operator()(Switch& _switch)
|
||||
{
|
||||
visit(*_switch.expression);
|
||||
if (_switch.expression->type() != typeid(Identifier))
|
||||
{
|
||||
ASTModifier::operator()(_switch);
|
||||
return;
|
||||
}
|
||||
YulString expr = boost::get<Identifier>(*_switch.expression).name;
|
||||
for (auto& _case: _switch.cases)
|
||||
{
|
||||
if (_case.value)
|
||||
{
|
||||
(*this)(*_case.value);
|
||||
if (
|
||||
!_case.body.statements.empty() &&
|
||||
_case.body.statements.front().type() == typeid(Assignment)
|
||||
)
|
||||
{
|
||||
Assignment const& assignment = boost::get<Assignment>(_case.body.statements.front());
|
||||
if (
|
||||
assignment.variableNames.size() == 1 &&
|
||||
assignment.variableNames.front().name == expr &&
|
||||
assignment.value->type() == typeid(Literal) &&
|
||||
valueOfLiteral(boost::get<Literal>(*assignment.value)) == valueOfLiteral(*_case.value)
|
||||
)
|
||||
_case.body.statements.erase(_case.body.statements.begin());
|
||||
}
|
||||
}
|
||||
(*this)(_case.body);
|
||||
}
|
||||
}
|
||||
|
||||
void ConditionalUnsimplifier::operator()(Block& _block)
|
||||
{
|
||||
walkVector(_block.statements);
|
||||
iterateReplacingWindow<2>(
|
||||
_block.statements,
|
||||
[&](Statement& _stmt1, Statement& _stmt2) -> std::optional<vector<Statement>>
|
||||
{
|
||||
if (_stmt1.type() == typeid(If))
|
||||
{
|
||||
If& _if = boost::get<If>(_stmt1);
|
||||
if (
|
||||
_if.condition->type() == typeid(Identifier) &&
|
||||
!_if.body.statements.empty()
|
||||
)
|
||||
{
|
||||
YulString condition = boost::get<Identifier>(*_if.condition).name;
|
||||
if (
|
||||
_stmt2.type() == typeid(Assignment) &&
|
||||
TerminationFinder(m_dialect).controlFlowKind(_if.body.statements.back()) !=
|
||||
TerminationFinder::ControlFlow::FlowOut
|
||||
)
|
||||
{
|
||||
Assignment const& assignment = boost::get<Assignment>(_stmt2);
|
||||
if (
|
||||
assignment.variableNames.size() == 1 &&
|
||||
assignment.variableNames.front().name == condition &&
|
||||
assignment.value->type() == typeid(Literal) &&
|
||||
valueOfLiteral(boost::get<Literal>(*assignment.value)) == 0
|
||||
)
|
||||
return {make_vector<Statement>(std::move(_stmt1))};
|
||||
}
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
);
|
||||
}
|
51
libyul/optimiser/ConditionalUnsimplifier.h
Normal file
51
libyul/optimiser/ConditionalUnsimplifier.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <libyul/optimiser/OptimiserStep.h>
|
||||
#include <libyul/Dialect.h>
|
||||
#include <libdevcore/Common.h>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
|
||||
/**
|
||||
* Reverse of conditional simplifier.
|
||||
*
|
||||
*/
|
||||
class ConditionalUnsimplifier: public ASTModifier
|
||||
{
|
||||
public:
|
||||
static constexpr char const* name{"ConditionalUnsimplifier"};
|
||||
static void run(OptimiserStepContext& _context, Block& _ast)
|
||||
{
|
||||
ConditionalUnsimplifier{_context.dialect}(_ast);
|
||||
}
|
||||
|
||||
using ASTModifier::operator();
|
||||
void operator()(Switch& _switch) override;
|
||||
void operator()(Block& _block) override;
|
||||
|
||||
private:
|
||||
explicit ConditionalUnsimplifier(Dialect const& _dialect):
|
||||
m_dialect(_dialect)
|
||||
{}
|
||||
Dialect const& m_dialect;
|
||||
};
|
||||
|
||||
}
|
@ -31,7 +31,11 @@ void ForLoopConditionIntoBody::run(OptimiserStepContext& _context, Block& _ast)
|
||||
|
||||
void ForLoopConditionIntoBody::operator()(ForLoop& _forLoop)
|
||||
{
|
||||
if (m_dialect.booleanNegationFunction() && _forLoop.condition->type() != typeid(Literal))
|
||||
if (
|
||||
m_dialect.booleanNegationFunction() &&
|
||||
_forLoop.condition->type() != typeid(Literal) &&
|
||||
_forLoop.condition->type() != typeid(Identifier)
|
||||
)
|
||||
{
|
||||
langutil::SourceLocation const loc = locationOf(*_forLoop.condition);
|
||||
|
||||
|
@ -25,6 +25,8 @@
|
||||
#include <libyul/optimiser/BlockFlattener.h>
|
||||
#include <libyul/optimiser/CallGraphGenerator.h>
|
||||
#include <libyul/optimiser/ControlFlowSimplifier.h>
|
||||
#include <libyul/optimiser/ConditionalSimplifier.h>
|
||||
#include <libyul/optimiser/ConditionalUnsimplifier.h>
|
||||
#include <libyul/optimiser/DeadCodeEliminator.h>
|
||||
#include <libyul/optimiser/FunctionGrouper.h>
|
||||
#include <libyul/optimiser/FunctionHoister.h>
|
||||
@ -36,6 +38,7 @@
|
||||
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
|
||||
#include <libyul/optimiser/ForLoopConditionOutOfBody.h>
|
||||
#include <libyul/optimiser/ForLoopInitRewriter.h>
|
||||
#include <libyul/optimiser/ForLoopConditionIntoBody.h>
|
||||
#include <libyul/optimiser/Rematerialiser.h>
|
||||
#include <libyul/optimiser/UnusedPruner.h>
|
||||
#include <libyul/optimiser/ExpressionSimplifier.h>
|
||||
@ -98,6 +101,7 @@ void OptimiserSuite::run(
|
||||
BlockFlattener::name,
|
||||
ControlFlowSimplifier::name,
|
||||
LiteralRematerialiser::name,
|
||||
ConditionalUnsimplifier::name,
|
||||
StructuralSimplifier::name,
|
||||
ControlFlowSimplifier::name,
|
||||
ForLoopConditionIntoBody::name,
|
||||
@ -130,8 +134,13 @@ void OptimiserSuite::run(
|
||||
}
|
||||
|
||||
{
|
||||
// still in SSA, perform structural simplification
|
||||
// perform structural simplification
|
||||
suite.runSequence({
|
||||
CommonSubexpressionEliminator::name,
|
||||
ConditionalSimplifier::name,
|
||||
LiteralRematerialiser::name,
|
||||
ConditionalUnsimplifier::name,
|
||||
StructuralSimplifier::name,
|
||||
LiteralRematerialiser::name,
|
||||
ForLoopConditionOutOfBody::name,
|
||||
ControlFlowSimplifier::name,
|
||||
@ -200,6 +209,10 @@ void OptimiserSuite::run(
|
||||
{
|
||||
// SSA plus simplify
|
||||
suite.runSequence({
|
||||
ConditionalSimplifier::name,
|
||||
LiteralRematerialiser::name,
|
||||
ConditionalUnsimplifier::name,
|
||||
CommonSubexpressionEliminator::name,
|
||||
SSATransform::name,
|
||||
RedundantAssignEliminator::name,
|
||||
RedundantAssignEliminator::name,
|
||||
@ -315,6 +328,8 @@ map<string, unique_ptr<OptimiserStep>> const& OptimiserSuite::allSteps()
|
||||
instance = optimiserStepCollection<
|
||||
BlockFlattener,
|
||||
CommonSubexpressionEliminator,
|
||||
ConditionalSimplifier,
|
||||
ConditionalUnsimplifier,
|
||||
ControlFlowSimplifier,
|
||||
DeadCodeEliminator,
|
||||
EquivalentFunctionCombiner,
|
||||
|
@ -5,22 +5,22 @@
|
||||
|
||||
(func $main
|
||||
(local $_1 i64)
|
||||
(local $pos i64)
|
||||
(local $_2 i64)
|
||||
(local $_3 i64)
|
||||
(local $hi i64)
|
||||
(local $y i64)
|
||||
(local $hi_1 i64)
|
||||
(set_local $_1 (i64.const 0))
|
||||
(set_local $pos (call $u256_to_i32 (get_local $_1) (get_local $_1) (get_local $_1) (i64.const 64)))
|
||||
(set_local $_2 (i64.const 65280))
|
||||
(set_local $hi (i64.shl (i64.or (i64.shl (i64.or (i64.and (i64.shl (get_local $_1) (i64.const 8)) (get_local $_2)) (i64.and (i64.shr_u (get_local $_1) (i64.const 8)) (i64.const 255))) (i64.const 16)) (call $endian_swap_16 (i64.shr_u (get_local $_1) (i64.const 16)))) (i64.const 32)))
|
||||
(set_local $y (i64.or (get_local $hi) (call $endian_swap_32 (i64.shr_u (get_local $_1) (i64.const 32)))))
|
||||
(i64.store (get_local $pos) (get_local $y))
|
||||
(i64.store (i64.add (get_local $pos) (i64.const 8)) (get_local $y))
|
||||
(i64.store (i64.add (get_local $pos) (i64.const 16)) (get_local $y))
|
||||
(set_local $hi_1 (i64.shl (i64.or (i64.shl (i64.or (i64.and (i64.shl (i64.const 64) (i64.const 8)) (get_local $_2)) (i64.and (i64.shr_u (i64.const 64) (i64.const 8)) (i64.const 255))) (i64.const 16)) (call $endian_swap_16 (i64.shr_u (i64.const 64) (i64.const 16)))) (i64.const 32)))
|
||||
(i64.store (i64.add (get_local $pos) (i64.const 24)) (i64.or (get_local $hi_1) (call $endian_swap_32 (i64.shr_u (i64.const 64) (i64.const 32)))))
|
||||
(call $eth.revert (call $u256_to_i32 (get_local $_1) (get_local $_1) (get_local $_1) (get_local $_1)) (call $u256_to_i32 (get_local $_1) (get_local $_1) (get_local $_1) (get_local $_1)))
|
||||
(local.set $_1 (i64.const 0))
|
||||
(local.set $_2 (i64.add (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 64)) (i64.const 64)))
|
||||
(local.set $_3 (i64.const 65280))
|
||||
(local.set $hi (i64.shl (i64.or (i64.shl (i64.or (i64.and (i64.shl (local.get $_1) (i64.const 8)) (local.get $_3)) (i64.and (i64.shr_u (local.get $_1) (i64.const 8)) (i64.const 255))) (i64.const 16)) (call $endian_swap_16 (i64.shr_u (local.get $_1) (i64.const 16)))) (i64.const 32)))
|
||||
(local.set $y (i64.or (local.get $hi) (call $endian_swap_32 (i64.shr_u (local.get $_1) (i64.const 32)))))
|
||||
(i64.store (i32.wrap_i64 (local.get $_2)) (local.get $y))
|
||||
(i64.store (i32.wrap_i64 (i64.add (local.get $_2) (i64.const 8))) (local.get $y))
|
||||
(i64.store (i32.wrap_i64 (i64.add (local.get $_2) (i64.const 16))) (local.get $y))
|
||||
(local.set $hi_1 (i64.shl (i64.or (i64.shl (i64.or (i64.and (i64.shl (i64.const 128) (i64.const 8)) (local.get $_3)) (i64.and (i64.shr_u (i64.const 128) (i64.const 8)) (i64.const 255))) (i64.const 16)) (call $endian_swap_16 (i64.shr_u (i64.const 128) (i64.const 16)))) (i64.const 32)))
|
||||
(i64.store (i32.wrap_i64 (i64.add (local.get $_2) (i64.const 24))) (i64.or (local.get $hi_1) (call $endian_swap_32 (i64.shr_u (i64.const 128) (i64.const 32)))))
|
||||
(call $eth.revert (i32.wrap_i64 (i64.add (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1)) (i64.const 64))) (i32.wrap_i64 (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1))))
|
||||
)
|
||||
|
||||
(func $u256_to_i32
|
||||
@ -30,20 +30,20 @@
|
||||
(param $x4 i64)
|
||||
(result i64)
|
||||
(local $v i64)
|
||||
(if (i64.ne (get_local $v) (i64.or (i64.or (get_local $x1) (get_local $x2)) (get_local $x3))) (then
|
||||
(if (i64.ne (i64.extend_i32_u (i64.ne (local.get $v) (i64.or (i64.or (local.get $x1) (local.get $x2)) (local.get $x3)))) (i64.const 0)) (then
|
||||
(unreachable)))
|
||||
(if (i64.ne (get_local $v) (i64.shr_u (get_local $x4) (i64.const 32))) (then
|
||||
(if (i64.ne (i64.extend_i32_u (i64.ne (local.get $v) (i64.shr_u (local.get $x4) (i64.const 32)))) (i64.const 0)) (then
|
||||
(unreachable)))
|
||||
(set_local $v (get_local $x4))
|
||||
(get_local $v)
|
||||
(local.set $v (local.get $x4))
|
||||
(local.get $v)
|
||||
)
|
||||
|
||||
(func $endian_swap_16
|
||||
(param $x i64)
|
||||
(result i64)
|
||||
(local $y i64)
|
||||
(set_local $y (i64.or (i64.and (i64.shl (get_local $x) (i64.const 8)) (i64.const 65280)) (i64.and (i64.shr_u (get_local $x) (i64.const 8)) (i64.const 255))))
|
||||
(get_local $y)
|
||||
(local.set $y (i64.or (i64.and (i64.shl (local.get $x) (i64.const 8)) (i64.const 65280)) (i64.and (i64.shr_u (local.get $x) (i64.const 8)) (i64.const 255))))
|
||||
(local.get $y)
|
||||
)
|
||||
|
||||
(func $endian_swap_32
|
||||
@ -51,9 +51,9 @@
|
||||
(result i64)
|
||||
(local $y i64)
|
||||
(local $hi i64)
|
||||
(set_local $hi (i64.shl (call $endian_swap_16 (get_local $x)) (i64.const 16)))
|
||||
(set_local $y (i64.or (get_local $hi) (call $endian_swap_16 (i64.shr_u (get_local $x) (i64.const 16)))))
|
||||
(get_local $y)
|
||||
(local.set $hi (i64.shl (call $endian_swap_16 (local.get $x)) (i64.const 16)))
|
||||
(local.set $y (i64.or (local.get $hi) (call $endian_swap_16 (i64.shr_u (local.get $x) (i64.const 16)))))
|
||||
(local.get $y)
|
||||
)
|
||||
|
||||
)
|
||||
@ -62,51 +62,26 @@
|
||||
(import \"ethereum\" \"finish\" (func $eth.finish (param i32 i32)))
|
||||
(memory $memory (export \"memory\") 1)
|
||||
(export \"main\" (func $main))
|
||||
(global $global_ (mut i64) (i64.const 0))
|
||||
(global $global__1 (mut i64) (i64.const 0))
|
||||
(global $global__2 (mut i64) (i64.const 0))
|
||||
|
||||
(func $main
|
||||
(local $_1 i64)
|
||||
(local $pos i64)
|
||||
(local $_2 i64)
|
||||
(local $hi i64)
|
||||
(local $y i64)
|
||||
(local $hi_1 i64)
|
||||
(local $hi_2 i64)
|
||||
(local $_2 i64)
|
||||
(local $_3 i64)
|
||||
(local $_4 i64)
|
||||
(local $_5 i64)
|
||||
(local $_6 i64)
|
||||
(local $_7 i64)
|
||||
(local $_8 i64)
|
||||
(local $_9 i64)
|
||||
(set_local $_1 (i64.const 0))
|
||||
(set_local $pos (call $u256_to_i32 (get_local $_1) (get_local $_1) (get_local $_1) (i64.const 64)))
|
||||
(set_local $hi (i64.shl (i64.or (i64.shl (i64.or (i64.and (i64.shl (get_local $_1) (i64.const 8)) (i64.const 65280)) (i64.and (i64.shr_u (get_local $_1) (i64.const 8)) (i64.const 255))) (i64.const 16)) (call $endian_swap_16 (i64.shr_u (get_local $_1) (i64.const 16)))) (i64.const 32)))
|
||||
(set_local $y (i64.or (get_local $hi) (call $endian_swap_32 (i64.shr_u (get_local $_1) (i64.const 32)))))
|
||||
(i64.store (get_local $pos) (get_local $y))
|
||||
(i64.store (i64.add (get_local $pos) (i64.const 8)) (get_local $y))
|
||||
(i64.store (i64.add (get_local $pos) (i64.const 16)) (get_local $y))
|
||||
(set_local $hi_1 (i64.shl (call $endian_swap_16 (i64.const 64)) (i64.const 16)))
|
||||
(set_local $hi_2 (i64.shl (i64.or (get_local $hi_1) (call $endian_swap_16 (i64.shr_u (i64.const 64) (i64.const 16)))) (i64.const 32)))
|
||||
(i64.store (i64.add (get_local $pos) (i64.const 24)) (i64.or (get_local $hi_2) (call $endian_swap_32 (i64.shr_u (i64.const 64) (i64.const 32)))))
|
||||
(block
|
||||
(set_local $_2 (datasize \"C_2_deployed\"))
|
||||
(set_local $_3 (get_global $global_))
|
||||
(set_local $_4 (get_global $global__1))
|
||||
(set_local $_5 (get_global $global__2))
|
||||
|
||||
)
|
||||
(block
|
||||
(set_local $_6 (dataoffset \"C_2_deployed\"))
|
||||
(set_local $_7 (get_global $global_))
|
||||
(set_local $_8 (get_global $global__1))
|
||||
(set_local $_9 (get_global $global__2))
|
||||
|
||||
)
|
||||
(call $eth.codeCopy (call $u256_to_i32 (get_local $_1) (get_local $_1) (get_local $_1) (get_local $_1)) (call $u256_to_i32 (get_local $_6) (get_local $_7) (get_local $_8) (get_local $_9)) (call $u256_to_i32 (get_local $_2) (get_local $_3) (get_local $_4) (get_local $_5)))
|
||||
(call $eth.finish (call $u256_to_i32 (get_local $_1) (get_local $_1) (get_local $_1) (get_local $_1)) (call $u256_to_i32 (get_local $_2) (get_local $_3) (get_local $_4) (get_local $_5)))
|
||||
(local.set $_1 (i64.const 0))
|
||||
(local.set $_2 (i64.add (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 64)) (i64.const 64)))
|
||||
(local.set $hi (i64.shl (i64.or (i64.shl (i64.or (i64.and (i64.shl (local.get $_1) (i64.const 8)) (i64.const 65280)) (i64.and (i64.shr_u (local.get $_1) (i64.const 8)) (i64.const 255))) (i64.const 16)) (call $endian_swap_16 (i64.shr_u (local.get $_1) (i64.const 16)))) (i64.const 32)))
|
||||
(local.set $y (i64.or (local.get $hi) (call $endian_swap_32 (i64.shr_u (local.get $_1) (i64.const 32)))))
|
||||
(i64.store (i32.wrap_i64 (local.get $_2)) (local.get $y))
|
||||
(i64.store (i32.wrap_i64 (i64.add (local.get $_2) (i64.const 8))) (local.get $y))
|
||||
(i64.store (i32.wrap_i64 (i64.add (local.get $_2) (i64.const 16))) (local.get $y))
|
||||
(local.set $hi_1 (i64.shl (call $endian_swap_32 (i64.const 128)) (i64.const 32)))
|
||||
(i64.store (i32.wrap_i64 (i64.add (local.get $_2) (i64.const 24))) (i64.or (local.get $hi_1) (call $endian_swap_32 (i64.shr_u (i64.const 128) (i64.const 32)))))
|
||||
(local.set $_3 (datasize \"C_2_deployed\"))
|
||||
(call $eth.codeCopy (i32.wrap_i64 (i64.add (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1)) (i64.const 64))) (i32.wrap_i64 (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (dataoffset \"C_2_deployed\"))) (i32.wrap_i64 (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_3))))
|
||||
(call $eth.finish (i32.wrap_i64 (i64.add (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1)) (i64.const 64))) (i32.wrap_i64 (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_3))))
|
||||
)
|
||||
|
||||
(func $u256_to_i32
|
||||
@ -116,20 +91,20 @@
|
||||
(param $x4 i64)
|
||||
(result i64)
|
||||
(local $v i64)
|
||||
(if (i64.ne (get_local $v) (i64.or (i64.or (get_local $x1) (get_local $x2)) (get_local $x3))) (then
|
||||
(if (i64.ne (i64.extend_i32_u (i64.ne (local.get $v) (i64.or (i64.or (local.get $x1) (local.get $x2)) (local.get $x3)))) (i64.const 0)) (then
|
||||
(unreachable)))
|
||||
(if (i64.ne (get_local $v) (i64.shr_u (get_local $x4) (i64.const 32))) (then
|
||||
(if (i64.ne (i64.extend_i32_u (i64.ne (local.get $v) (i64.shr_u (local.get $x4) (i64.const 32)))) (i64.const 0)) (then
|
||||
(unreachable)))
|
||||
(set_local $v (get_local $x4))
|
||||
(get_local $v)
|
||||
(local.set $v (local.get $x4))
|
||||
(local.get $v)
|
||||
)
|
||||
|
||||
(func $endian_swap_16
|
||||
(param $x i64)
|
||||
(result i64)
|
||||
(local $y i64)
|
||||
(set_local $y (i64.or (i64.and (i64.shl (get_local $x) (i64.const 8)) (i64.const 65280)) (i64.and (i64.shr_u (get_local $x) (i64.const 8)) (i64.const 255))))
|
||||
(get_local $y)
|
||||
(local.set $y (i64.or (i64.and (i64.shl (local.get $x) (i64.const 8)) (i64.const 65280)) (i64.and (i64.shr_u (local.get $x) (i64.const 8)) (i64.const 255))))
|
||||
(local.get $y)
|
||||
)
|
||||
|
||||
(func $endian_swap_32
|
||||
@ -137,9 +112,9 @@
|
||||
(result i64)
|
||||
(local $y i64)
|
||||
(local $hi i64)
|
||||
(set_local $hi (i64.shl (call $endian_swap_16 (get_local $x)) (i64.const 16)))
|
||||
(set_local $y (i64.or (get_local $hi) (call $endian_swap_16 (i64.shr_u (get_local $x) (i64.const 16)))))
|
||||
(get_local $y)
|
||||
(local.set $hi (i64.shl (call $endian_swap_16 (local.get $x)) (i64.const 16)))
|
||||
(local.set $y (i64.or (local.get $hi) (call $endian_swap_16 (i64.shr_u (local.get $x) (i64.const 16)))))
|
||||
(local.get $y)
|
||||
)
|
||||
|
||||
)
|
||||
|
@ -11,6 +11,4 @@ contract C
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (90-97): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (115-122): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (130-149): Assertion violation happens here
|
||||
|
@ -11,6 +11,4 @@ contract C
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (92-102): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (120-130): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (138-160): Assertion violation happens here
|
||||
|
@ -13,7 +13,3 @@ contract C
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (90-97): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (115-122): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (138-145): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (153-172): Assertion violation happens here
|
||||
|
@ -13,7 +13,3 @@ contract C
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (92-102): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (120-130): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (146-156): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (164-186): Assertion violation happens here
|
||||
|
@ -17,15 +17,15 @@ contract C
|
||||
// Warning: (124-130): Assertion checker does not yet support this expression.
|
||||
// Warning: (124-128): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (124-133): Assertion checker does not yet implement this expression.
|
||||
// Warning: (124-136): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (124-136): Assertion checker does not yet implement this expression.
|
||||
// Warning: (154-160): Assertion checker does not yet support this expression.
|
||||
// Warning: (154-158): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (154-163): Assertion checker does not yet implement this expression.
|
||||
// Warning: (154-166): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (154-166): Assertion checker does not yet implement this expression.
|
||||
// Warning: (182-188): Assertion checker does not yet support this expression.
|
||||
// Warning: (182-186): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (182-191): Assertion checker does not yet implement this expression.
|
||||
// Warning: (182-194): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (182-194): Assertion checker does not yet implement this expression.
|
||||
// Warning: (209-215): Assertion checker does not yet support this expression.
|
||||
// Warning: (209-213): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (209-218): Assertion checker does not yet implement this expression.
|
||||
|
@ -10,5 +10,3 @@ contract C
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (134-145): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (154-178): Assertion violation happens here
|
||||
|
@ -10,5 +10,4 @@ contract C
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (134-145): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (154-178): Assertion violation happens here
|
||||
|
@ -10,5 +10,3 @@ contract C
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (152-167): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (176-204): Assertion violation happens here
|
||||
|
@ -10,5 +10,4 @@ contract C
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// Warning: (152-167): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (176-204): Assertion violation happens here
|
||||
|
@ -19,15 +19,15 @@ contract C
|
||||
// Warning: (117-120): Assertion checker does not yet support this expression.
|
||||
// Warning: (117-118): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (117-123): Assertion checker does not yet implement this expression.
|
||||
// Warning: (117-126): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (117-126): Assertion checker does not yet implement this expression.
|
||||
// Warning: (144-147): Assertion checker does not yet support this expression.
|
||||
// Warning: (144-145): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (144-150): Assertion checker does not yet implement this expression.
|
||||
// Warning: (144-153): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (144-153): Assertion checker does not yet implement this expression.
|
||||
// Warning: (169-172): Assertion checker does not yet support this expression.
|
||||
// Warning: (169-170): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (169-175): Assertion checker does not yet implement this expression.
|
||||
// Warning: (169-178): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (169-178): Assertion checker does not yet implement this expression.
|
||||
// Warning: (193-196): Assertion checker does not yet support this expression.
|
||||
// Warning: (193-194): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (193-199): Assertion checker does not yet implement this expression.
|
||||
|
@ -18,15 +18,15 @@ contract C
|
||||
// Warning: (124-127): Assertion checker does not yet support this expression.
|
||||
// Warning: (124-125): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (124-130): Assertion checker does not yet implement this expression.
|
||||
// Warning: (124-136): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (124-136): Assertion checker does not yet implement this expression.
|
||||
// Warning: (154-157): Assertion checker does not yet support this expression.
|
||||
// Warning: (154-155): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (154-160): Assertion checker does not yet implement this expression.
|
||||
// Warning: (154-166): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (154-166): Assertion checker does not yet implement this expression.
|
||||
// Warning: (182-185): Assertion checker does not yet support this expression.
|
||||
// Warning: (182-183): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (182-188): Assertion checker does not yet implement this expression.
|
||||
// Warning: (182-194): Assertion checker does not yet implement assignments to multi-dimensional mappings or arrays.
|
||||
// Warning: (182-194): Assertion checker does not yet implement this expression.
|
||||
// Warning: (209-212): Assertion checker does not yet support this expression.
|
||||
// Warning: (209-210): Assertion checker does not yet implement type struct C.S memory
|
||||
// Warning: (209-215): Assertion checker does not yet implement this expression.
|
||||
|
@ -27,6 +27,8 @@
|
||||
#include <libyul/optimiser/DeadCodeEliminator.h>
|
||||
#include <libyul/optimiser/Disambiguator.h>
|
||||
#include <libyul/optimiser/CallGraphGenerator.h>
|
||||
#include <libyul/optimiser/ConditionalUnsimplifier.h>
|
||||
#include <libyul/optimiser/ConditionalSimplifier.h>
|
||||
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
|
||||
#include <libyul/optimiser/NameCollector.h>
|
||||
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
|
||||
@ -157,6 +159,16 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
|
||||
disambiguate();
|
||||
CommonSubexpressionEliminator::run(*m_context, *m_ast);
|
||||
}
|
||||
else if (m_optimizerStep == "conditionalUnsimplifier")
|
||||
{
|
||||
disambiguate();
|
||||
ConditionalUnsimplifier::run(*m_context, *m_ast);
|
||||
}
|
||||
else if (m_optimizerStep == "conditionalSimplifier")
|
||||
{
|
||||
disambiguate();
|
||||
ConditionalSimplifier::run(*m_context, *m_ast);
|
||||
}
|
||||
else if (m_optimizerStep == "expressionSplitter")
|
||||
ExpressionSplitter::run(*m_context, *m_ast);
|
||||
else if (m_optimizerStep == "expressionJoiner")
|
||||
|
@ -0,0 +1,17 @@
|
||||
{
|
||||
let y := mload(0x20)
|
||||
for {} and(y, 8) { pop(y) } {
|
||||
if y { break }
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// {
|
||||
// let y := mload(0x20)
|
||||
// for { } and(y, 8) { pop(y) }
|
||||
// {
|
||||
// if y { break }
|
||||
// y := 0
|
||||
// }
|
||||
// }
|
@ -0,0 +1,17 @@
|
||||
{
|
||||
let y := mload(0x20)
|
||||
for {} and(y, 8) { pop(y) } {
|
||||
if y { continue }
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// {
|
||||
// let y := mload(0x20)
|
||||
// for { } and(y, 8) { pop(y) }
|
||||
// {
|
||||
// if y { continue }
|
||||
// y := 0
|
||||
// }
|
||||
// }
|
@ -0,0 +1,22 @@
|
||||
{
|
||||
let x := mload(0)
|
||||
let y := mload(0)
|
||||
if x { revert(0, 0) }
|
||||
if y { revert(0, 0) }
|
||||
for {} and(x, y) {} {
|
||||
x := 2
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := mload(0)
|
||||
// let y := mload(0)
|
||||
// if x { revert(0, 0) }
|
||||
// x := 0
|
||||
// if y { revert(0, 0) }
|
||||
// y := 0
|
||||
// for { } and(x, y) { }
|
||||
// { x := 2 }
|
||||
// }
|
@ -0,0 +1,23 @@
|
||||
{
|
||||
let x
|
||||
for {} x { sstore(1, x) } {
|
||||
if x { continue }
|
||||
// x is 0 here, but should not be 0
|
||||
// anymore in the for loop post block
|
||||
sstore(0, x)
|
||||
}
|
||||
sstore(0, x)
|
||||
}
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x
|
||||
// for { } x { sstore(1, x) }
|
||||
// {
|
||||
// if x { continue }
|
||||
// x := 0
|
||||
// sstore(0, x)
|
||||
// }
|
||||
// sstore(0, x)
|
||||
// }
|
@ -0,0 +1,32 @@
|
||||
{
|
||||
let x := mload(0)
|
||||
for {} 1 {} {
|
||||
if x { sstore(7, 8) break sstore(8, 9) }
|
||||
sstore(1, x)
|
||||
if x { sstore(7, 8) break }
|
||||
sstore(10, x)
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := mload(0)
|
||||
// for { } 1 { }
|
||||
// {
|
||||
// if x
|
||||
// {
|
||||
// sstore(7, 8)
|
||||
// break
|
||||
// sstore(8, 9)
|
||||
// }
|
||||
// sstore(1, x)
|
||||
// if x
|
||||
// {
|
||||
// sstore(7, 8)
|
||||
// break
|
||||
// }
|
||||
// x := 0
|
||||
// sstore(10, x)
|
||||
// }
|
||||
// }
|
@ -0,0 +1,13 @@
|
||||
{
|
||||
let x := mload(0)
|
||||
if x { sstore(0, x) }
|
||||
sstore(1, x)
|
||||
}
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := mload(0)
|
||||
// if x { sstore(0, x) }
|
||||
// sstore(1, x)
|
||||
// }
|
@ -0,0 +1,18 @@
|
||||
{
|
||||
let x := mload(0)
|
||||
if x { sstore(0, x) revert(0, 0) }
|
||||
sstore(1, x)
|
||||
}
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := mload(0)
|
||||
// if x
|
||||
// {
|
||||
// sstore(0, x)
|
||||
// revert(0, 0)
|
||||
// }
|
||||
// x := 0
|
||||
// sstore(1, x)
|
||||
// }
|
@ -0,0 +1,20 @@
|
||||
{
|
||||
let x := calldataload(0)
|
||||
switch x
|
||||
case 0 { }
|
||||
case 1 { }
|
||||
default { }
|
||||
|
||||
pop(x)
|
||||
}
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(0)
|
||||
// switch x
|
||||
// case 0 { x := 0 }
|
||||
// case 1 { x := 1 }
|
||||
// default { }
|
||||
// pop(x)
|
||||
// }
|
@ -0,0 +1,5 @@
|
||||
{ }
|
||||
// ====
|
||||
// step: conditionalSimplifier
|
||||
// ----
|
||||
// { }
|
@ -0,0 +1,15 @@
|
||||
{
|
||||
let y := mload(0x20)
|
||||
for {} and(y, 8) { pop(y) } {
|
||||
if y { break }
|
||||
y := 0
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// {
|
||||
// let y := mload(0x20)
|
||||
// for { } and(y, 8) { pop(y) }
|
||||
// { if y { break } }
|
||||
// }
|
@ -0,0 +1,15 @@
|
||||
{
|
||||
let y := mload(0x20)
|
||||
for {} and(y, 8) { pop(y) } {
|
||||
if y { continue }
|
||||
y := 0
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// {
|
||||
// let y := mload(0x20)
|
||||
// for { } and(y, 8) { pop(y) }
|
||||
// { if y { continue } }
|
||||
// }
|
@ -0,0 +1,22 @@
|
||||
{
|
||||
let x := mload(0)
|
||||
let y := mload(0)
|
||||
if x { revert(0, 0) }
|
||||
x := 0
|
||||
if y { revert(0, 0) }
|
||||
y := 0
|
||||
for {} and(x, y) {} {
|
||||
x := 2
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := mload(0)
|
||||
// let y := mload(0)
|
||||
// if x { revert(0, 0) }
|
||||
// if y { revert(0, 0) }
|
||||
// for { } and(x, y) { }
|
||||
// { x := 2 }
|
||||
// }
|
@ -0,0 +1,21 @@
|
||||
{
|
||||
let x
|
||||
for {} x { sstore(1, x) } {
|
||||
if x { continue }
|
||||
x := 0
|
||||
sstore(0, x)
|
||||
}
|
||||
sstore(0, x)
|
||||
}
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x
|
||||
// for { } x { sstore(1, x) }
|
||||
// {
|
||||
// if x { continue }
|
||||
// sstore(0, x)
|
||||
// }
|
||||
// sstore(0, x)
|
||||
// }
|
@ -0,0 +1,34 @@
|
||||
{
|
||||
let x := mload(0)
|
||||
for {} 1 {} {
|
||||
if x { sstore(7, 8) break sstore(8, 9) }
|
||||
x := 0
|
||||
sstore(1, x)
|
||||
if x { sstore(7, 8) break }
|
||||
x := 0
|
||||
sstore(10, x)
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := mload(0)
|
||||
// for { } 1 { }
|
||||
// {
|
||||
// if x
|
||||
// {
|
||||
// sstore(7, 8)
|
||||
// break
|
||||
// sstore(8, 9)
|
||||
// }
|
||||
// x := 0
|
||||
// sstore(1, x)
|
||||
// if x
|
||||
// {
|
||||
// sstore(7, 8)
|
||||
// break
|
||||
// }
|
||||
// sstore(10, x)
|
||||
// }
|
||||
// }
|
@ -0,0 +1,15 @@
|
||||
{
|
||||
let x := mload(0)
|
||||
if x { sstore(0, x) }
|
||||
x := 0
|
||||
sstore(1, x)
|
||||
}
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := mload(0)
|
||||
// if x { sstore(0, x) }
|
||||
// x := 0
|
||||
// sstore(1, x)
|
||||
// }
|
@ -0,0 +1,18 @@
|
||||
{
|
||||
let x := mload(0)
|
||||
if x { sstore(0, x) revert(0, 0) }
|
||||
x := 0
|
||||
sstore(1, x)
|
||||
}
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := mload(0)
|
||||
// if x
|
||||
// {
|
||||
// sstore(0, x)
|
||||
// revert(0, 0)
|
||||
// }
|
||||
// sstore(1, x)
|
||||
// }
|
@ -0,0 +1,22 @@
|
||||
{
|
||||
let x := calldataload(0)
|
||||
switch x
|
||||
case 0 { x := 0 }
|
||||
case 1 { x := 1 }
|
||||
case 2 { x := 8 /* wrong literal */ }
|
||||
default { }
|
||||
|
||||
pop(x)
|
||||
}
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// {
|
||||
// let x := calldataload(0)
|
||||
// switch x
|
||||
// case 0 { }
|
||||
// case 1 { }
|
||||
// case 2 { x := 8 }
|
||||
// default { }
|
||||
// pop(x)
|
||||
// }
|
@ -0,0 +1,5 @@
|
||||
{ }
|
||||
// ====
|
||||
// step: conditionalUnsimplifier
|
||||
// ----
|
||||
// { }
|
@ -17,8 +17,8 @@
|
||||
// { }
|
||||
// for { } 0 { }
|
||||
// { }
|
||||
// for { } 1 { }
|
||||
// { if iszero(a) { break } }
|
||||
// for { } a { }
|
||||
// { }
|
||||
// for { } 1 { }
|
||||
// {
|
||||
// if iszero(add(a, a)) { break }
|
||||
|
@ -0,0 +1,22 @@
|
||||
{
|
||||
let y := mload(0x20)
|
||||
for {} and(y, 8) { if y { revert(0, 0) } } {
|
||||
if y { continue }
|
||||
sstore(1, y)
|
||||
}
|
||||
if y { revert(0, 0) }
|
||||
}
|
||||
// ====
|
||||
// step: fullSuite
|
||||
// ----
|
||||
// {
|
||||
// {
|
||||
// let y := mload(0x20)
|
||||
// for { } and(y, 8) { if y { revert(0, 0) } }
|
||||
// {
|
||||
// if y { continue }
|
||||
// sstore(1, 0)
|
||||
// }
|
||||
// if y { revert(0, 0) }
|
||||
// }
|
||||
// }
|
31
test/libyul/yulOptimizerTests/fullSuite/devcon_example.yul
Normal file
31
test/libyul/yulOptimizerTests/fullSuite/devcon_example.yul
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
sstore(0, array_sum(calldataload(0)))
|
||||
|
||||
function array_sum(x) -> sum {
|
||||
let length := calldataload(x)
|
||||
for { let i := 0 } lt(i, length) { i := add(i, 1) } {
|
||||
sum := add(sum, array_load(x, i))
|
||||
}
|
||||
}
|
||||
function array_load(x, i) -> v {
|
||||
let len := calldataload(x)
|
||||
if iszero(lt(i, len)) { revert(0, 0) }
|
||||
let data := add(x, 0x20)
|
||||
v := calldataload(add(data, mul(i, 0x20)))
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// step: fullSuite
|
||||
// ----
|
||||
// {
|
||||
// {
|
||||
// let _1 := calldataload(0)
|
||||
// let sum := 0
|
||||
// let i := sum
|
||||
// for { } lt(i, calldataload(_1)) { i := add(i, 1) }
|
||||
// {
|
||||
// sum := add(sum, calldataload(add(add(_1, mul(i, 0x20)), 0x20)))
|
||||
// }
|
||||
// sstore(0, sum)
|
||||
// }
|
||||
// }
|
@ -21,10 +21,10 @@
|
||||
// ----
|
||||
// {
|
||||
// {
|
||||
// let _1 := mload(0x40)
|
||||
// mstore(0x40, add(_1, 0x20))
|
||||
// mstore(0x40, add(_1, 96))
|
||||
// mstore(add(_1, 128), 2)
|
||||
// let p := mload(0x40)
|
||||
// mstore(0x40, add(p, 0x20))
|
||||
// mstore(0x40, add(p, 96))
|
||||
// mstore(add(p, 128), 2)
|
||||
// mstore(0x40, 0x20)
|
||||
// }
|
||||
// }
|
||||
|
@ -17,30 +17,6 @@
|
||||
|
||||
syntax = "proto2";
|
||||
|
||||
// Flattened specification of array dimension
|
||||
// If is_static is false, and this array dimension is contained
|
||||
// inside another dimension e.g., x[][2] ([2] being the outer dimension)
|
||||
// then `length` for this dimension is the length of the first dynamically
|
||||
// sized array. The other (n-1) lengths are unspecified
|
||||
message ArrayDimensionInfo {
|
||||
required uint32 length = 1;
|
||||
required bool is_static = 2;
|
||||
}
|
||||
|
||||
// TODO: Add more base types
|
||||
// See https://github.com/ethereum/solidity/issues/6749
|
||||
message ArrayType {
|
||||
oneof base_type_oneof {
|
||||
IntegerType inty = 1;
|
||||
FixedByteType byty = 2;
|
||||
AddressType adty = 3;
|
||||
StructType stty = 4;
|
||||
BoolType boolty = 5;
|
||||
DynamicByteArrayType dynbytesty = 6;
|
||||
}
|
||||
repeated ArrayDimensionInfo info = 7;
|
||||
}
|
||||
|
||||
// bool
|
||||
message BoolType {}
|
||||
|
||||
@ -78,6 +54,16 @@ message DynamicByteArrayType {
|
||||
required DType type = 1;
|
||||
}
|
||||
|
||||
message ArrayType {
|
||||
required Type t = 1;
|
||||
required uint32 length = 2;
|
||||
required bool is_static = 3;
|
||||
}
|
||||
|
||||
message StructType {
|
||||
repeated Type t = 1;
|
||||
}
|
||||
|
||||
message NonValueType {
|
||||
oneof nonvalue_type_oneof {
|
||||
DynamicByteArrayType dynbytearray = 1;
|
||||
@ -86,6 +72,8 @@ message NonValueType {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add more types
|
||||
// See https://github.com/ethereum/solidity/issues/6749
|
||||
message Type {
|
||||
oneof type_oneof {
|
||||
ValueType vtype = 1;
|
||||
@ -93,9 +81,6 @@ message Type {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: This must not reference itself either directly or indirectly
|
||||
message StructType {}
|
||||
|
||||
message VarDecl {
|
||||
required Type type = 1;
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,10 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <test/tools/ossfuzz/abiV2Proto.pb.h>
|
||||
|
||||
#include <libdevcore/FixedHash.h>
|
||||
#include <libdevcore/Keccak256.h>
|
||||
#include <libdevcore/StringUtils.h>
|
||||
#include <libdevcore/Whiskers.h>
|
||||
#include <test/tools/ossfuzz/abiV2Proto.pb.h>
|
||||
|
||||
#include <liblangutil/Exceptions.h>
|
||||
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
|
||||
@ -95,6 +103,8 @@ namespace test
|
||||
{
|
||||
namespace abiv2fuzzer
|
||||
{
|
||||
/// Converts a protobuf input into a Solidity program that tests
|
||||
/// abi coding.
|
||||
class ProtoConverter
|
||||
{
|
||||
public:
|
||||
@ -103,101 +113,109 @@ public:
|
||||
m_counter(0),
|
||||
m_varCounter(0),
|
||||
m_returnValue(1),
|
||||
m_isLastDynParamRightPadded(false)
|
||||
m_isLastDynParamRightPadded(false),
|
||||
m_structCounter(0),
|
||||
m_numStructsAdded(0)
|
||||
{}
|
||||
|
||||
ProtoConverter(ProtoConverter const&) = delete;
|
||||
|
||||
ProtoConverter(ProtoConverter&&) = delete;
|
||||
|
||||
std::string contractToString(Contract const& _input);
|
||||
|
||||
private:
|
||||
using VecOfBoolUnsigned = std::vector<std::pair<bool, unsigned>>;
|
||||
|
||||
enum class Delimiter
|
||||
{
|
||||
ADD,
|
||||
SKIP
|
||||
};
|
||||
/// Enum of possible function types that decode abi
|
||||
/// encoded parameters.
|
||||
enum class CalleeType
|
||||
{
|
||||
PUBLIC,
|
||||
EXTERNAL
|
||||
};
|
||||
enum class DataType
|
||||
{
|
||||
BYTES,
|
||||
STRING,
|
||||
VALUE,
|
||||
ARRAY
|
||||
};
|
||||
|
||||
void visit(BoolType const&);
|
||||
|
||||
void visit(IntegerType const&);
|
||||
|
||||
void visit(FixedByteType const&);
|
||||
|
||||
void visit(AddressType const&);
|
||||
|
||||
void visit(ArrayType const&);
|
||||
|
||||
void visit(DynamicByteArrayType const&);
|
||||
|
||||
void visit(StructType const&);
|
||||
|
||||
void visit(ValueType const&);
|
||||
|
||||
void visit(NonValueType const&);
|
||||
|
||||
void visit(Type const&);
|
||||
|
||||
void visit(VarDecl const&);
|
||||
|
||||
void visit(TestFunction const&);
|
||||
|
||||
/// Visitors for various Protobuf types
|
||||
/// Visit top-level contract specification
|
||||
void visit(Contract const&);
|
||||
|
||||
std::string getValueByBaseType(ArrayType const&);
|
||||
/// Convert test function specification into Solidity test
|
||||
/// function
|
||||
/// @param _testSpec: Protobuf test function specification
|
||||
/// @param _storageDefs: String containing Solidity assignment
|
||||
/// statements to be placed inside the scope of the test function.
|
||||
std::string visit(TestFunction const& _testSpec, std::string const& _storageDefs);
|
||||
|
||||
DataType getDataTypeByBaseType(ArrayType const& _x);
|
||||
/// Visitors for the remaining protobuf types. They convert
|
||||
/// the input protobuf specification type into Solidity code.
|
||||
/// @return A pair of strings, first of which contains Solidity
|
||||
/// code to be placed inside contract scope, second of which contains
|
||||
/// Solidity code to be placed inside test function scope.
|
||||
std::pair<std::string, std::string> visit(VarDecl const&);
|
||||
std::pair<std::string, std::string> visit(Type const&);
|
||||
std::pair<std::string, std::string> visit(ValueType const&);
|
||||
std::pair<std::string, std::string> visit(NonValueType const&);
|
||||
std::pair<std::string, std::string> visit(BoolType const&);
|
||||
std::pair<std::string, std::string> visit(IntegerType const&);
|
||||
std::pair<std::string, std::string> visit(FixedByteType const&);
|
||||
std::pair<std::string, std::string> visit(AddressType const&);
|
||||
std::pair<std::string, std::string> visit(DynamicByteArrayType const&);
|
||||
std::pair<std::string, std::string> visit(ArrayType const&);
|
||||
std::pair<std::string, std::string> visit(StructType const&);
|
||||
|
||||
void resizeInitArray(
|
||||
ArrayType const& _x,
|
||||
std::string const& _baseType,
|
||||
std::string const& _varName,
|
||||
std::string const& _paramName
|
||||
);
|
||||
/// Convert a protobuf type @a _T into Solidity code to be placed
|
||||
/// inside contract and test function scopes.
|
||||
/// @param: _type (of parameterized type protobuf type T) is the type
|
||||
/// of protobuf input to be converted.
|
||||
/// @param: _isValueType is true if _type is a Solidity value type e.g., uint
|
||||
/// and false otherwise e.g., string
|
||||
/// @return: A pair of strings, first of which contains Solidity
|
||||
/// code to be placed inside contract scope, second of which contains
|
||||
/// Solidity code to be placed inside test function scope.
|
||||
template <typename T>
|
||||
std::pair<std::string, std::string> processType(T const& _type, bool _isValueType);
|
||||
|
||||
unsigned resizeDimension(
|
||||
bool _isStatic,
|
||||
unsigned _arrayLen,
|
||||
std::string const& _var,
|
||||
std::string const& _param,
|
||||
std::string const& _type
|
||||
);
|
||||
|
||||
void resizeHelper(
|
||||
ArrayType const& _x,
|
||||
std::vector<std::string> _arrStrVec,
|
||||
VecOfBoolUnsigned _arrInfoVec,
|
||||
std::string const& _varName,
|
||||
std::string const& _paramName
|
||||
);
|
||||
|
||||
// Utility functions
|
||||
void appendChecks(DataType _type, std::string const& _varName, std::string const& _rhs);
|
||||
|
||||
void addVarDef(std::string const& _varName, std::string const& _rhs);
|
||||
|
||||
void addCheckedVarDef(
|
||||
DataType _type,
|
||||
/// Convert a protobuf type @a _T into Solidity variable assignment and check
|
||||
/// statements to be placed inside contract and test function scopes.
|
||||
/// @param: _varName is the name of the Solidity variable
|
||||
/// @param: _paramName is the name of the Solidity parameter that is passed
|
||||
/// to the test function
|
||||
/// @param: _type (of parameterized type protobuf type T) is the type
|
||||
/// of protobuf input to be converted.
|
||||
/// @return: A pair of strings, first of which contains Solidity
|
||||
/// statements to be placed inside contract scope, second of which contains
|
||||
/// Solidity statements to be placed inside test function scope.
|
||||
template <typename T>
|
||||
std::pair<std::string, std::string> assignChecker(
|
||||
std::string const& _varName,
|
||||
std::string const& _paramName,
|
||||
std::string const& _rhs
|
||||
T _type
|
||||
);
|
||||
|
||||
/// Convert a protobuf type @a _T into Solidity variable declaration statement.
|
||||
/// @param: _varName is the name of the Solidity variable
|
||||
/// @param: _paramName is the name of the Solidity parameter that is passed
|
||||
/// to the test function
|
||||
/// @param: _type (of parameterized type protobuf type T) is the type
|
||||
/// of protobuf input to be converted.
|
||||
/// @param: _isValueType is a boolean that is true if _type is a
|
||||
/// Solidity value type e.g., uint and false otherwise e.g., string
|
||||
/// @param: _location is the Solidity location qualifier string to
|
||||
/// be used inside variable declaration statements
|
||||
/// @return: A pair of strings, first of which contains Solidity
|
||||
/// variable declaration statement to be placed inside contract scope,
|
||||
/// second of which contains Solidity variable declaration statement
|
||||
/// to be placed inside test function scope.
|
||||
template <typename T>
|
||||
std::pair<std::string, std::string> varDecl(
|
||||
std::string const& _varName,
|
||||
std::string const& _paramName,
|
||||
T _type,
|
||||
bool _isValueType,
|
||||
std::string const& _location
|
||||
);
|
||||
|
||||
/// Appends a function parameter to the function parameter stream.
|
||||
void appendTypedParams(
|
||||
CalleeType _calleeType,
|
||||
bool _isValueType,
|
||||
@ -206,6 +224,8 @@ private:
|
||||
Delimiter _delimiter
|
||||
);
|
||||
|
||||
/// Appends a function parameter to the public test function's
|
||||
/// parameter stream.
|
||||
void appendTypedParamsPublic(
|
||||
bool _isValueType,
|
||||
std::string const& _typeString,
|
||||
@ -213,6 +233,8 @@ private:
|
||||
Delimiter _delimiter = Delimiter::ADD
|
||||
);
|
||||
|
||||
/// Appends a function parameter to the external test function's
|
||||
/// parameter stream.
|
||||
void appendTypedParamsExternal(
|
||||
bool _isValueType,
|
||||
std::string const& _typeString,
|
||||
@ -220,48 +242,42 @@ private:
|
||||
Delimiter _delimiter = Delimiter::ADD
|
||||
);
|
||||
|
||||
void appendVarDeclToOutput(
|
||||
/// Returns a Solidity variable declaration statement
|
||||
/// @param _type: string containing Solidity type of the
|
||||
/// variable to be declared.
|
||||
/// @param _varName: string containing Solidity variable
|
||||
/// name
|
||||
/// @param _qualifier: string containing location where
|
||||
/// the variable will be placed
|
||||
std::string getVarDecl(
|
||||
std::string const& _type,
|
||||
std::string const& _varName,
|
||||
std::string const& _qualifier
|
||||
);
|
||||
|
||||
void checkResizeOp(std::string const& _varName, unsigned _len);
|
||||
|
||||
void visitType(DataType _dataType, std::string const& _type, std::string const& _value);
|
||||
|
||||
void visitArrayType(std::string const&, ArrayType const&);
|
||||
|
||||
void createDeclAndParamList(
|
||||
std::string const& _type,
|
||||
DataType _dataType,
|
||||
std::string& _varName,
|
||||
std::string& _paramName
|
||||
);
|
||||
|
||||
/// Return checks that are encoded as Solidity if statements
|
||||
/// as string
|
||||
std::string equalityChecksAsString();
|
||||
|
||||
/// Return comma separated typed function parameters as string
|
||||
std::string typedParametersAsString(CalleeType _calleeType);
|
||||
|
||||
void writeHelperFunctions();
|
||||
/// Return Solidity helper functions as string
|
||||
std::string helperFunctions();
|
||||
|
||||
// Function definitions
|
||||
// m_counter is used to derive values for typed variables
|
||||
unsigned getNextCounter()
|
||||
{
|
||||
return m_counter++;
|
||||
}
|
||||
/// Return top-level test code as string
|
||||
std::string testCode(unsigned _invalidLength);
|
||||
|
||||
// m_varCounter is used to derive declared and parameterized variable names
|
||||
/// Return the next variable count that is used for
|
||||
/// variable naming.
|
||||
unsigned getNextVarCounter()
|
||||
{
|
||||
return m_varCounter++;
|
||||
}
|
||||
|
||||
// Accepts an unsigned counter and returns a pair of strings
|
||||
// First string is declared name (s_varNamePrefix<varcounter_value>)
|
||||
// Second string is parameterized name (s_paramPrefix<varcounter_value>)
|
||||
auto newVarNames(unsigned _varCounter)
|
||||
/// Return a pair of names for Solidity variable and the same variable when
|
||||
/// passed as a function parameter.
|
||||
static std::pair<std::string, std::string> newVarNames(unsigned _varCounter)
|
||||
{
|
||||
return std::make_pair(
|
||||
s_varNamePrefix + std::to_string(_varCounter),
|
||||
@ -269,62 +285,80 @@ private:
|
||||
);
|
||||
}
|
||||
|
||||
std::string getQualifier(DataType _dataType)
|
||||
{
|
||||
return ((isValueType(_dataType) || m_isStateVar) ? "" : "memory");
|
||||
}
|
||||
|
||||
/// Checks if the last dynamically encoded Solidity type is right
|
||||
/// padded, returning true if it is and false otherwise.
|
||||
bool isLastDynParamRightPadded()
|
||||
{
|
||||
return m_isLastDynParamRightPadded;
|
||||
}
|
||||
|
||||
// Static declarations
|
||||
static std::string structTypeAsString(StructType const& _x);
|
||||
static std::string boolValueAsString(unsigned _counter);
|
||||
static std::string intValueAsString(unsigned _width, unsigned _counter);
|
||||
static std::string uintValueAsString(unsigned _width, unsigned _counter);
|
||||
static std::string integerValueAsString(bool _sign, unsigned _width, unsigned _counter);
|
||||
static std::string addressValueAsString(unsigned _counter);
|
||||
static std::string fixedByteValueAsString(unsigned _width, unsigned _counter);
|
||||
|
||||
/// Returns a hex literal if _isHexLiteral is true, a string literal otherwise.
|
||||
/// The size of the returned literal is _numBytes bytes.
|
||||
/// @param _decorate If true, the returned string is enclosed within double quotes
|
||||
/// if _isHexLiteral is false.
|
||||
/// @param _isHexLiteral If true, the returned string is enclosed within
|
||||
/// double quotes prefixed by the string "hex" if _decorate is true. If
|
||||
/// _decorate is false, the returned string is returned as-is.
|
||||
/// @return hex value as string
|
||||
static std::string hexValueAsString(
|
||||
unsigned _numBytes,
|
||||
unsigned _counter,
|
||||
bool _isHexLiteral,
|
||||
bool _decorate = true
|
||||
);
|
||||
|
||||
/// Concatenates the hash value obtained from monotonically increasing counter
|
||||
/// until the desired number of bytes determined by _numBytes.
|
||||
/// @param _width Desired number of bytes for hex value
|
||||
/// @param _counter A counter value used for creating a keccak256 hash
|
||||
/// @param _isHexLiteral Since this routine may be used to construct
|
||||
/// string or hex literals, this flag is used to construct a valid output.
|
||||
/// @return Valid hex or string literal of size _width bytes
|
||||
static std::string variableLengthValueAsString(
|
||||
unsigned _width,
|
||||
unsigned _counter,
|
||||
bool _isHexLiteral
|
||||
);
|
||||
static std::vector<std::pair<bool, unsigned>> arrayDimensionsAsPairVector(ArrayType const& _x);
|
||||
static std::string arrayDimInfoAsString(ArrayDimensionInfo const& _x);
|
||||
static void arrayDimensionsAsStringVector(
|
||||
ArrayType const& _x,
|
||||
std::vector<std::string>&
|
||||
);
|
||||
static std::string bytesArrayTypeAsString(DynamicByteArrayType const& _x);
|
||||
static std::string arrayTypeAsString(std::string const&, ArrayType const&);
|
||||
/// Convert delimter to a comma or null string.
|
||||
static std::string delimiterToString(Delimiter _delimiter);
|
||||
static std::string croppedString(unsigned _numBytes, unsigned _counter, bool _isHexLiteral);
|
||||
|
||||
/// Contains the test program
|
||||
std::ostringstream m_output;
|
||||
/// Contains a subset of the test program. This subset contains
|
||||
/// checks to be encoded in the test program
|
||||
std::ostringstream m_checks;
|
||||
/// Contains typed parameter list to be passed to callee functions
|
||||
std::ostringstream m_typedParamsExternal;
|
||||
std::ostringstream m_typedParamsPublic;
|
||||
/// Predicate that is true if we are in contract scope
|
||||
bool m_isStateVar;
|
||||
unsigned m_counter;
|
||||
unsigned m_varCounter;
|
||||
/// Monotonically increasing return value for error reporting
|
||||
unsigned m_returnValue;
|
||||
/// Flag that indicates if last dynamically encoded parameter
|
||||
/// passed to a function call is of a type that is going to be
|
||||
/// right padded by the ABI encoder.
|
||||
bool m_isLastDynParamRightPadded;
|
||||
/// Struct counter
|
||||
unsigned m_structCounter;
|
||||
unsigned m_numStructsAdded;
|
||||
/// Prefixes for declared and parameterized variable names
|
||||
static auto constexpr s_varNamePrefix = "x_";
|
||||
static auto constexpr s_paramNamePrefix = "c_";
|
||||
};
|
||||
|
||||
/// Visitor interface for Solidity protobuf types.
|
||||
template <typename T>
|
||||
class AbiV2ProtoVisitor
|
||||
{
|
||||
public:
|
||||
static unsigned constexpr s_maxArrayDimensions = 3;
|
||||
virtual ~AbiV2ProtoVisitor() = default;
|
||||
|
||||
virtual T visit(BoolType const& _node) = 0;
|
||||
virtual T visit(IntegerType const& _node) = 0;
|
||||
virtual T visit(FixedByteType const& _node) = 0;
|
||||
virtual T visit(AddressType const& _node) = 0;
|
||||
virtual T visit(DynamicByteArrayType const& _node) = 0;
|
||||
virtual T visit(ArrayType const& _node) = 0;
|
||||
virtual T visit(StructType const& _node) = 0;
|
||||
virtual T visit(ValueType const& _node)
|
||||
{
|
||||
return visitValueType(_node);
|
||||
}
|
||||
virtual T visit(NonValueType const& _node)
|
||||
{
|
||||
return visitNonValueType(_node);
|
||||
}
|
||||
virtual T visit(Type const& _node)
|
||||
{
|
||||
return visitType(_node);
|
||||
}
|
||||
|
||||
enum class DataType
|
||||
{
|
||||
BYTES,
|
||||
STRING,
|
||||
VALUE,
|
||||
ARRAY
|
||||
};
|
||||
|
||||
/// Prefixes for declared and parameterized variable names
|
||||
static auto constexpr s_structNamePrefix = "S";
|
||||
|
||||
// Static function definitions
|
||||
static bool isValueType(DataType _dataType)
|
||||
@ -342,11 +376,6 @@ private:
|
||||
return _x.is_signed();
|
||||
}
|
||||
|
||||
static std::string getBoolTypeAsString()
|
||||
{
|
||||
return "bool";
|
||||
}
|
||||
|
||||
static std::string getIntTypeAsString(IntegerType const& _x)
|
||||
{
|
||||
return ((isIntSigned(_x) ? "int" : "uint") + std::to_string(getIntWidth(_x)));
|
||||
@ -364,7 +393,7 @@ private:
|
||||
|
||||
static std::string getAddressTypeAsString(AddressType const& _x)
|
||||
{
|
||||
return (_x.payable() ? "address payable": "address");
|
||||
return (_x.payable() ? "address payable" : "address");
|
||||
}
|
||||
|
||||
static DataType getDataTypeOfDynBytesType(DynamicByteArrayType const& _x)
|
||||
@ -375,12 +404,6 @@ private:
|
||||
return DataType::BYTES;
|
||||
}
|
||||
|
||||
/// Returns true if input is either a string or bytes, false otherwise.
|
||||
static bool isDataTypeBytesOrString(DataType _type)
|
||||
{
|
||||
return _type == DataType::STRING || _type == DataType::BYTES;
|
||||
}
|
||||
|
||||
// Convert _counter to string and return its keccak256 hash
|
||||
static u256 hashUnsignedInt(unsigned _counter)
|
||||
{
|
||||
@ -389,7 +412,7 @@ private:
|
||||
|
||||
static u256 maskUnsignedInt(unsigned _counter, unsigned _numMaskNibbles)
|
||||
{
|
||||
return hashUnsignedInt(_counter) & u256("0x" + std::string(_numMaskNibbles, 'f'));
|
||||
return hashUnsignedInt(_counter) & u256("0x" + std::string(_numMaskNibbles, 'f'));
|
||||
}
|
||||
|
||||
// Requires caller to pass number of nibbles (twice the number of bytes) as second argument.
|
||||
@ -414,15 +437,6 @@ private:
|
||||
return _fuzz % s_maxArrayLength + 1;
|
||||
}
|
||||
|
||||
static std::pair<bool, unsigned> arrayDimInfoAsPair(ArrayDimensionInfo const& _x)
|
||||
{
|
||||
return (
|
||||
_x.is_static() ?
|
||||
std::make_pair(true, getStaticArrayLengthFromFuzz(_x.length())) :
|
||||
std::make_pair(false, getDynArrayLengthFromFuzz(_x.length(), 0))
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns a pseudo-random value for the size of a string/hex
|
||||
/// literal. Used for creating variable length hex/string literals.
|
||||
/// @param _counter Monotonically increasing counter value
|
||||
@ -435,48 +449,412 @@ private:
|
||||
return (_counter + 879) * 32 % (s_maxDynArrayLength + 1);
|
||||
}
|
||||
|
||||
static std::string bytesArrayTypeAsString(DynamicByteArrayType const& _x)
|
||||
{
|
||||
switch (_x.type())
|
||||
{
|
||||
case DynamicByteArrayType::BYTES:
|
||||
return "bytes";
|
||||
case DynamicByteArrayType::STRING:
|
||||
return "string";
|
||||
}
|
||||
}
|
||||
protected:
|
||||
T visitValueType(ValueType const& _type)
|
||||
{
|
||||
switch (_type.value_type_oneof_case())
|
||||
{
|
||||
case ValueType::kInty:
|
||||
return visit(_type.inty());
|
||||
case ValueType::kByty:
|
||||
return visit(_type.byty());
|
||||
case ValueType::kAdty:
|
||||
return visit(_type.adty());
|
||||
case ValueType::kBoolty:
|
||||
return visit(_type.boolty());
|
||||
case ValueType::VALUE_TYPE_ONEOF_NOT_SET:
|
||||
return T();
|
||||
}
|
||||
}
|
||||
|
||||
T visitNonValueType(NonValueType const& _type)
|
||||
{
|
||||
switch (_type.nonvalue_type_oneof_case())
|
||||
{
|
||||
case NonValueType::kDynbytearray:
|
||||
return visit(_type.dynbytearray());
|
||||
case NonValueType::kArrtype:
|
||||
return visit(_type.arrtype());
|
||||
case NonValueType::kStype:
|
||||
return visit(_type.stype());
|
||||
case NonValueType::NONVALUE_TYPE_ONEOF_NOT_SET:
|
||||
return T();
|
||||
}
|
||||
}
|
||||
|
||||
T visitType(Type const& _type)
|
||||
{
|
||||
switch (_type.type_oneof_case())
|
||||
{
|
||||
case Type::kVtype:
|
||||
return visit(_type.vtype());
|
||||
case Type::kNvtype:
|
||||
return visit(_type.nvtype());
|
||||
case Type::TYPE_ONEOF_NOT_SET:
|
||||
return T();
|
||||
}
|
||||
}
|
||||
private:
|
||||
static unsigned constexpr s_maxArrayLength = 4;
|
||||
static unsigned constexpr s_maxDynArrayLength = 256;
|
||||
};
|
||||
|
||||
/// Converts a protobuf type into a Solidity type string.
|
||||
class TypeVisitor: public AbiV2ProtoVisitor<std::string>
|
||||
{
|
||||
public:
|
||||
TypeVisitor(unsigned _structSuffix = 0):
|
||||
m_indentation(1),
|
||||
m_structCounter(_structSuffix),
|
||||
m_structStartCounter(_structSuffix),
|
||||
m_structFieldCounter(0),
|
||||
m_isLastDynParamRightPadded(false)
|
||||
{}
|
||||
|
||||
std::string visit(BoolType const&) override;
|
||||
std::string visit(IntegerType const&) override;
|
||||
std::string visit(FixedByteType const&) override;
|
||||
std::string visit(AddressType const&) override;
|
||||
std::string visit(ArrayType const&) override;
|
||||
std::string visit(DynamicByteArrayType const&) override;
|
||||
std::string visit(StructType const&) override;
|
||||
using AbiV2ProtoVisitor<std::string>::visit;
|
||||
std::string baseType()
|
||||
{
|
||||
return m_baseType;
|
||||
}
|
||||
bool isLastDynParamRightPadded()
|
||||
{
|
||||
return m_isLastDynParamRightPadded;
|
||||
}
|
||||
std::string structDef()
|
||||
{
|
||||
return m_structDef.str();
|
||||
}
|
||||
unsigned numStructs()
|
||||
{
|
||||
return m_structCounter - m_structStartCounter;
|
||||
}
|
||||
static bool arrayOfStruct(ArrayType const& _type)
|
||||
{
|
||||
Type const& baseType = _type.t();
|
||||
if (baseType.has_nvtype() && baseType.nvtype().has_stype())
|
||||
return true;
|
||||
else if (baseType.has_nvtype() && baseType.nvtype().has_arrtype())
|
||||
return arrayOfStruct(baseType.nvtype().arrtype());
|
||||
else
|
||||
return false;
|
||||
}
|
||||
private:
|
||||
void structDefinition(StructType const&);
|
||||
|
||||
std::string indentation()
|
||||
{
|
||||
return std::string(m_indentation * 1, '\t');
|
||||
}
|
||||
std::string lineString(std::string const& _line)
|
||||
{
|
||||
return indentation() + _line + "\n";
|
||||
}
|
||||
|
||||
std::string m_baseType;
|
||||
std::ostringstream m_structDef;
|
||||
unsigned m_indentation;
|
||||
unsigned m_structCounter;
|
||||
unsigned m_structStartCounter;
|
||||
unsigned m_structFieldCounter;
|
||||
bool m_isLastDynParamRightPadded;
|
||||
|
||||
static auto constexpr s_structTypeName = "S";
|
||||
};
|
||||
|
||||
/// Returns a pair of strings, first of which contains assignment statements
|
||||
/// to initialize a given type, and second of which contains checks to be
|
||||
/// placed inside the coder function to test abi en/decoding.
|
||||
class AssignCheckVisitor: public AbiV2ProtoVisitor<std::pair<std::string, std::string>>
|
||||
{
|
||||
public:
|
||||
AssignCheckVisitor(
|
||||
std::string _varName,
|
||||
std::string _paramName,
|
||||
unsigned _errorStart,
|
||||
bool _stateVar,
|
||||
unsigned _counter,
|
||||
unsigned _structCounter
|
||||
)
|
||||
{
|
||||
m_counter = m_counterStart = _counter;
|
||||
m_varName = _varName;
|
||||
m_paramName = _paramName;
|
||||
m_errorCode = m_errorStart = _errorStart;
|
||||
m_indentation = 2;
|
||||
m_stateVar = _stateVar;
|
||||
m_structCounter = m_structStart = _structCounter;
|
||||
}
|
||||
std::pair<std::string, std::string> visit(BoolType const&) override;
|
||||
std::pair<std::string, std::string> visit(IntegerType const&) override;
|
||||
std::pair<std::string, std::string> visit(FixedByteType const&) override;
|
||||
std::pair<std::string, std::string> visit(AddressType const&) override;
|
||||
std::pair<std::string, std::string> visit(ArrayType const&) override;
|
||||
std::pair<std::string, std::string> visit(DynamicByteArrayType const&) override;
|
||||
std::pair<std::string, std::string> visit(StructType const&) override;
|
||||
using AbiV2ProtoVisitor<std::pair<std::string, std::string>>::visit;
|
||||
|
||||
unsigned errorStmts()
|
||||
{
|
||||
return m_errorCode - m_errorStart;
|
||||
}
|
||||
|
||||
unsigned counted()
|
||||
{
|
||||
return m_counter - m_counterStart;
|
||||
}
|
||||
|
||||
unsigned structs()
|
||||
{
|
||||
return m_structCounter - m_structStart;
|
||||
}
|
||||
private:
|
||||
std::string indentation()
|
||||
{
|
||||
return std::string(m_indentation * 1, '\t');
|
||||
}
|
||||
unsigned counter()
|
||||
{
|
||||
return m_counter++;
|
||||
}
|
||||
|
||||
std::pair<std::string, std::string> assignAndCheckStringPair(
|
||||
std::string const& _varRef,
|
||||
std::string const& _checkRef,
|
||||
std::string const& _assignValue,
|
||||
std::string const& _checkValue,
|
||||
DataType _type
|
||||
);
|
||||
std::string assignString(std::string const&, std::string const&);
|
||||
std::string checkString(std::string const&, std::string const&, DataType);
|
||||
unsigned m_counter;
|
||||
unsigned m_counterStart;
|
||||
std::string m_varName;
|
||||
std::string m_paramName;
|
||||
unsigned m_errorCode;
|
||||
unsigned m_errorStart;
|
||||
unsigned m_indentation;
|
||||
bool m_stateVar;
|
||||
unsigned m_structCounter;
|
||||
unsigned m_structStart;
|
||||
};
|
||||
|
||||
/// Returns a valid value (as a string) for a given type.
|
||||
class ValueGetterVisitor: AbiV2ProtoVisitor<std::string>
|
||||
{
|
||||
public:
|
||||
ValueGetterVisitor(unsigned _counter = 0): m_counter(_counter) {}
|
||||
|
||||
std::string visit(BoolType const&) override;
|
||||
std::string visit(IntegerType const&) override;
|
||||
std::string visit(FixedByteType const&) override;
|
||||
std::string visit(AddressType const&) override;
|
||||
std::string visit(DynamicByteArrayType const&) override;
|
||||
std::string visit(ArrayType const&) override
|
||||
{
|
||||
solAssert(false, "ABIv2 proto fuzzer: Cannot call valuegettervisitor on complex type");
|
||||
}
|
||||
std::string visit(StructType const&) override
|
||||
{
|
||||
solAssert(false, "ABIv2 proto fuzzer: Cannot call valuegettervisitor on complex type");
|
||||
}
|
||||
using AbiV2ProtoVisitor<std::string>::visit;
|
||||
private:
|
||||
unsigned counter()
|
||||
{
|
||||
return m_counter++;
|
||||
}
|
||||
|
||||
static std::string intValueAsString(unsigned _width, unsigned _counter);
|
||||
static std::string uintValueAsString(unsigned _width, unsigned _counter);
|
||||
static std::string integerValueAsString(bool _sign, unsigned _width, unsigned _counter);
|
||||
static std::string addressValueAsString(unsigned _counter);
|
||||
static std::string fixedByteValueAsString(unsigned _width, unsigned _counter);
|
||||
|
||||
/// Returns a hex literal if _isHexLiteral is true, a string literal otherwise.
|
||||
/// The size of the returned literal is _numBytes bytes.
|
||||
/// @param _decorate If true, the returned string is enclosed within double quotes
|
||||
/// if _isHexLiteral is false.
|
||||
/// @param _isHexLiteral If true, the returned string is enclosed within
|
||||
/// double quotes prefixed by the string "hex" if _decorate is true. If
|
||||
/// _decorate is false, the returned string is returned as-is.
|
||||
/// @return hex value as string
|
||||
static std::string hexValueAsString(
|
||||
unsigned _numBytes,
|
||||
unsigned _counter,
|
||||
bool _isHexLiteral,
|
||||
bool _decorate = true
|
||||
);
|
||||
|
||||
/// Returns a hex/string literal of variable length whose value and
|
||||
/// size are pseudo-randomly determined from the counter value.
|
||||
/// @param _counter A monotonically increasing counter value
|
||||
/// @param _isHexLiteral Flag that indicates whether hex (if true) or
|
||||
/// string literal (false) is desired
|
||||
/// @return A variable length hex/string value
|
||||
static std::string bytesArrayValueAsString(unsigned _counter, bool _isHexLiteral)
|
||||
static std::string bytesArrayValueAsString(unsigned _counter, bool _isHexLiteral);
|
||||
|
||||
/// Concatenates the hash value obtained from monotonically increasing counter
|
||||
/// until the desired number of bytes determined by _numBytes.
|
||||
/// @param _width Desired number of bytes for hex value
|
||||
/// @param _counter A counter value used for creating a keccak256 hash
|
||||
/// @param _isHexLiteral Since this routine may be used to construct
|
||||
/// string or hex literals, this flag is used to construct a valid output.
|
||||
/// @return Valid hex or string literal of size _width bytes
|
||||
static std::string variableLengthValueAsString(
|
||||
unsigned _width,
|
||||
unsigned _counter,
|
||||
bool _isHexLiteral
|
||||
);
|
||||
|
||||
/// Returns a value that is @a _numBytes bytes long.
|
||||
/// @param _numBytes: Number of bytes of desired value
|
||||
/// @param _counter: A counter value
|
||||
/// @param _isHexLiteral: True if desired value is a hex literal, false otherwise
|
||||
static std::string croppedString(unsigned _numBytes, unsigned _counter, bool _isHexLiteral);
|
||||
|
||||
unsigned m_counter;
|
||||
};
|
||||
|
||||
/// Returns true if protobuf array specification is well-formed, false otherwise
|
||||
class ValidityVisitor: AbiV2ProtoVisitor<bool>
|
||||
{
|
||||
public:
|
||||
ValidityVisitor(): m_arrayDimensions(0) {}
|
||||
|
||||
bool visit(BoolType const&) override
|
||||
{
|
||||
return variableLengthValueAsString(
|
||||
getVarLength(_counter),
|
||||
_counter,
|
||||
_isHexLiteral
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Contains the test program
|
||||
std::ostringstream m_output;
|
||||
/// Temporary storage for state variable definitions
|
||||
std::ostringstream m_statebuffer;
|
||||
/// Contains a subset of the test program. This subset contains
|
||||
/// checks to be encoded in the test program
|
||||
std::ostringstream m_checks;
|
||||
/// Contains typed parameter list to be passed to callee functions
|
||||
std::ostringstream m_typedParamsExternal;
|
||||
std::ostringstream m_typedParamsPublic;
|
||||
/// Predicate that is true if we are in contract scope
|
||||
bool m_isStateVar;
|
||||
unsigned m_counter;
|
||||
unsigned m_varCounter;
|
||||
/// Monotonically increasing return value for error reporting
|
||||
unsigned m_returnValue;
|
||||
/// Flag that indicates if last dynamically encoded parameter
|
||||
/// passed to a function call is of a type that is going to be
|
||||
/// right padded by the ABI encoder.
|
||||
bool m_isLastDynParamRightPadded;
|
||||
static unsigned constexpr s_maxArrayLength = 4;
|
||||
static unsigned constexpr s_maxArrayDimensions = 4;
|
||||
static unsigned constexpr s_maxDynArrayLength = 256;
|
||||
/// Prefixes for declared and parameterized variable names
|
||||
static auto constexpr s_varNamePrefix = "x_";
|
||||
static auto constexpr s_paramNamePrefix = "c_";
|
||||
bool visit(IntegerType const&) override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool visit(FixedByteType const&) override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool visit(AddressType const&) override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool visit(DynamicByteArrayType const&) override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool visit(ArrayType const& _type) override
|
||||
{
|
||||
// Mark array type as invalid in one of the following is true
|
||||
// - contains more than s_maxArrayDimensions dimensions
|
||||
// - contains an invalid base type, which happens in the
|
||||
// following cases
|
||||
// - array base type is invalid
|
||||
// - array base type is empty
|
||||
m_arrayDimensions++;
|
||||
if (m_arrayDimensions > s_maxArrayDimensions)
|
||||
return false;
|
||||
return visit(_type.t());
|
||||
}
|
||||
|
||||
bool visit(StructType const& _type) override
|
||||
{
|
||||
// A struct is marked invalid only if all of its fields
|
||||
// are invalid. This is done to prevent an empty struct
|
||||
// being defined (which is a Solidity error).
|
||||
for (auto const& t: _type.t())
|
||||
if (visit(t))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned m_arrayDimensions;
|
||||
using AbiV2ProtoVisitor<bool>::visit;
|
||||
};
|
||||
|
||||
/// Returns true if visited type is dynamically encoded by the abi coder,
|
||||
/// false otherwise.
|
||||
class DynParamVisitor: AbiV2ProtoVisitor<bool>
|
||||
{
|
||||
public:
|
||||
DynParamVisitor() = default;
|
||||
|
||||
bool visit(BoolType const&) override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool visit(IntegerType const&) override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool visit(FixedByteType const&) override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool visit(AddressType const&) override
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool visit(DynamicByteArrayType const&) override
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool visit(ArrayType const& _type) override
|
||||
{
|
||||
// Return early if array spec is not well-formed
|
||||
if (!ValidityVisitor().visit(_type))
|
||||
return false;
|
||||
|
||||
// Array is dynamically encoded if it at least one of the following is true
|
||||
// - at least one dimension is dynamically sized
|
||||
// - base type is dynamically encoded
|
||||
if (!_type.is_static())
|
||||
return true;
|
||||
else
|
||||
return visit(_type.t());
|
||||
}
|
||||
|
||||
bool visit(StructType const& _type) override
|
||||
{
|
||||
// Return early if empty struct
|
||||
if (!ValidityVisitor().visit(_type))
|
||||
return false;
|
||||
|
||||
// Struct is dynamically encoded if at least one of its fields
|
||||
// is dynamically encoded.
|
||||
for (auto const& t: _type.t())
|
||||
if (visit(t))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
using AbiV2ProtoVisitor<bool>::visit;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -34,6 +34,7 @@
|
||||
#include <libyul/optimiser/Disambiguator.h>
|
||||
#include <libyul/optimiser/CallGraphGenerator.h>
|
||||
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
|
||||
#include <libyul/optimiser/ConditionalSimplifier.h>
|
||||
#include <libyul/optimiser/ControlFlowSimplifier.h>
|
||||
#include <libyul/optimiser/NameCollector.h>
|
||||
#include <libyul/optimiser/EquivalentFunctionCombiner.h>
|
||||
@ -138,7 +139,8 @@ public:
|
||||
cout << " (e)xpr inline/(i)nline/(s)implify/varname c(l)eaner/(u)nusedprune/ss(a) transform/" << endl;
|
||||
cout << " (r)edundant assign elim./re(m)aterializer/f(o)r-loop-init-rewriter/for-loop-condition-(I)nto-body/" << endl;
|
||||
cout << " for-loop-condition-(O)ut-of-body/s(t)ructural simplifier/equi(v)alent function combiner/ssa re(V)erser/" << endl;
|
||||
cout << " co(n)trol flow simplifier/stack com(p)ressor/(D)ead code eliminator/(L)oad resolver/? " << endl;
|
||||
cout << " co(n)trol flow simplifier/stack com(p)ressor/(D)ead code eliminator/(L)oad resolver/ " << endl;
|
||||
cout << " (C)onditional simplifier?" << endl;
|
||||
cout.flush();
|
||||
int option = readStandardInputChar();
|
||||
cout << ' ' << char(option) << endl;
|
||||
@ -163,6 +165,9 @@ public:
|
||||
case 'c':
|
||||
CommonSubexpressionEliminator::run(context, *m_ast);
|
||||
break;
|
||||
case 'C':
|
||||
ConditionalSimplifier::run(context, *m_ast);
|
||||
break;
|
||||
case 'd':
|
||||
VarDeclInitializer::run(context, *m_ast);
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user