2019-05-21 13:52:15 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Optimisation stage that replaces expressions of type ``sload(x)`` by the value
|
|
|
|
* currently stored in storage, if known.
|
|
|
|
*/
|
|
|
|
|
2019-05-27 22:14:01 +00:00
|
|
|
#include <libyul/optimiser/LoadResolver.h>
|
2019-05-21 13:52:15 +00:00
|
|
|
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2019-05-27 22:14:01 +00:00
|
|
|
#include <libyul/optimiser/Semantics.h>
|
2019-05-21 13:52:15 +00:00
|
|
|
#include <libyul/AsmData.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace yul;
|
|
|
|
|
2019-05-27 22:14:01 +00:00
|
|
|
void LoadResolver::run(Dialect const& _dialect, Block& _ast)
|
|
|
|
{
|
2019-08-13 11:34:33 +00:00
|
|
|
bool containsMSize = MSizeFinder::containsMSize(_dialect, _ast);
|
2019-05-27 22:14:01 +00:00
|
|
|
LoadResolver{_dialect, !containsMSize}(_ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadResolver::visit(Expression& _e)
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
|
|
|
if (_e.type() == typeid(FunctionCall))
|
|
|
|
{
|
|
|
|
FunctionCall const& funCall = boost::get<FunctionCall>(_e);
|
|
|
|
if (auto const* builtin = dynamic_cast<EVMDialect const&>(m_dialect).builtin(funCall.functionName.name))
|
2019-05-27 22:14:01 +00:00
|
|
|
if (!builtin->parameters.empty() && funCall.arguments.at(0).type() == typeid(Identifier))
|
|
|
|
{
|
|
|
|
YulString key = boost::get<Identifier>(funCall.arguments.at(0)).name;
|
|
|
|
if (
|
|
|
|
builtin->instruction == dev::eth::Instruction::SLOAD &&
|
|
|
|
m_storage.values.count(key)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
_e = Identifier{locationOf(_e), m_storage.values[key]};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (
|
|
|
|
m_optimizeMLoad &&
|
|
|
|
builtin->instruction == dev::eth::Instruction::MLOAD &&
|
|
|
|
m_memory.values.count(key)
|
|
|
|
)
|
2019-05-21 13:52:15 +00:00
|
|
|
{
|
2019-05-27 22:14:01 +00:00
|
|
|
_e = Identifier{locationOf(_e), m_memory.values[key]};
|
|
|
|
return;
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
2019-05-27 22:14:01 +00:00
|
|
|
}
|
2019-05-21 13:52:15 +00:00
|
|
|
}
|
|
|
|
}
|