/*
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 .
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace solidity::yul
{
class MemoryLoadResolver: public Scoper
{
public:
static constexpr char const* name{"MemoryLoadResolver"};
static void run(OptimiserStepContext& _context, Block& _ast);
using ASTModifier::operator();
void operator()(VariableDeclaration& _variableDeclaration) override;
void operator()(FunctionCall& _functionCall) override;
void operator()(If& _if) override;
void operator()(Switch& _switch) override;
void operator()(ForLoop& _forLoop) override;
void operator()(FunctionDefinition& _functionDefinition) override;
using ASTModifier::visit;
void visit(Expression& _e) override;
private:
MemoryLoadResolver(
Dialect const& _dialect,
std::map _functionSideEffects,
std::set _ssaVariables,
bool _containsMsize
) :
m_functionSideEffects(std::move(_functionSideEffects)),
m_ssaVariables(std::move(_ssaVariables)),
m_containsMsize(_containsMsize),
m_dialect(_dialect),
m_solver(std::make_unique())
{
for (auto const& name: m_ssaVariables)
std::cout << name.str() << std::endl;
}
/// The mapping for memory
std::map m_memory;
/// Side-effects of user-defined functions. Worst-case side-effects are assumed
/// if this is not provided or the function is not found.
std::map const m_functionSideEffects;
/// A set of all SSA variables in the AST
std::set const m_ssaVariables;
// TODO is this needed? loadresolver seems to use it
bool m_containsMsize = true;
Dialect const& m_dialect;
/// Joins the `_older` and current memory during control flow joins
void joinMemory(std::map const& _older);
void clearMemoryIfInvalidated(Block const& _block);
void clearMemoryIfInvalidated(Expression const& _expr);
/// Checks if a function call is identical to a mstore(x, y), where x and y are SSA variables.
std::optional> isSimpleMStore(
FunctionCall const& _functionCall
) const;
smtutil::Expression encodeExpression(
Expression const& _expression
);
virtual smtutil::Expression encodeEVMBuiltin(
evmasm::Instruction _instruction,
std::vector const& _arguments
);
smtutil::Expression newVariable();
virtual smtutil::Expression newRestrictedVariable(bigint _maxValue = (bigint(1) << 256));
std::string uniqueName();
virtual std::shared_ptr defaultSort() const;
virtual smtutil::Expression wrap(smtutil::Expression _value);
virtual smtutil::Expression constantValue(size_t _value) const;
virtual smtutil::Expression literalValue(Literal const& _literal) const;
bool invalidatesMemoryLocation(YulString const& _name, Expression const& _expression);
std::unique_ptr m_solver;
std::map m_variables;
size_t m_varCounter = 0;
// TODO have a variable for function side effects
};
}