2019-05-28 10:57: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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-05-28 10:57:15 +00:00
|
|
|
/**
|
|
|
|
* Module providing metrics for the EVM optimizer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/backends/evm/EVMMetrics.h>
|
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2019-05-28 10:57:15 +00:00
|
|
|
#include <libyul/Exceptions.h>
|
|
|
|
#include <libyul/Utilities.h>
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
|
|
|
|
|
|
|
#include <libevmasm/Instruction.h>
|
|
|
|
#include <libevmasm/GasMeter.h>
|
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2019-05-28 10:57:15 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
using namespace solidity::util;
|
2019-05-28 10:57:15 +00:00
|
|
|
|
2021-04-19 11:08:19 +00:00
|
|
|
bigint GasMeter::costs(Expression const& _expression) const
|
2019-05-28 10:57:15 +00:00
|
|
|
{
|
|
|
|
return combineCosts(GasMeterVisitor::costs(_expression, m_dialect, m_isCreation));
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:08:19 +00:00
|
|
|
bigint GasMeter::instructionCosts(evmasm::Instruction _instruction) const
|
2019-05-28 10:57:15 +00:00
|
|
|
{
|
|
|
|
return combineCosts(GasMeterVisitor::instructionCosts(_instruction, m_dialect, m_isCreation));
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:08:19 +00:00
|
|
|
bigint GasMeter::combineCosts(std::pair<bigint, bigint> _costs) const
|
2019-05-28 10:57:15 +00:00
|
|
|
{
|
|
|
|
return _costs.first * m_runs + _costs.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-19 11:08:19 +00:00
|
|
|
pair<bigint, bigint> GasMeterVisitor::costs(
|
2019-05-28 10:57:15 +00:00
|
|
|
Expression const& _expression,
|
|
|
|
EVMDialect const& _dialect,
|
|
|
|
bool _isCreation
|
|
|
|
)
|
|
|
|
{
|
|
|
|
GasMeterVisitor gmv(_dialect, _isCreation);
|
|
|
|
gmv.visit(_expression);
|
|
|
|
return {gmv.m_runGas, gmv.m_dataGas};
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:08:19 +00:00
|
|
|
pair<bigint, bigint> GasMeterVisitor::instructionCosts(
|
2019-12-11 16:31:36 +00:00
|
|
|
evmasm::Instruction _instruction,
|
2019-05-28 10:57:15 +00:00
|
|
|
EVMDialect const& _dialect,
|
|
|
|
bool _isCreation
|
|
|
|
)
|
|
|
|
{
|
|
|
|
GasMeterVisitor gmv(_dialect, _isCreation);
|
|
|
|
gmv.instructionCostsInternal(_instruction);
|
|
|
|
return {gmv.m_runGas, gmv.m_dataGas};
|
|
|
|
}
|
|
|
|
|
|
|
|
void GasMeterVisitor::operator()(FunctionCall const& _funCall)
|
|
|
|
{
|
|
|
|
ASTWalker::operator()(_funCall);
|
|
|
|
if (BuiltinFunctionForEVM const* f = m_dialect.builtin(_funCall.functionName.name))
|
|
|
|
if (f->instruction)
|
|
|
|
{
|
|
|
|
instructionCostsInternal(*f->instruction);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
yulAssert(false, "Functions not implemented.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void GasMeterVisitor::operator()(Literal const& _lit)
|
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
m_runGas += evmasm::GasMeter::runGas(evmasm::Instruction::PUSH1, m_dialect.evmVersion());
|
2019-05-28 10:57:15 +00:00
|
|
|
m_dataGas +=
|
|
|
|
singleByteDataGas() +
|
2021-04-19 11:08:19 +00:00
|
|
|
evmasm::GasMeter::dataGas(
|
2020-06-02 13:34:28 +00:00
|
|
|
toCompactBigEndian(valueOfLiteral(_lit), 1),
|
|
|
|
m_isCreation,
|
|
|
|
m_dialect.evmVersion()
|
2021-04-19 11:08:19 +00:00
|
|
|
);
|
2019-05-28 10:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GasMeterVisitor::operator()(Identifier const&)
|
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
m_runGas += evmasm::GasMeter::runGas(evmasm::Instruction::DUP1, m_dialect.evmVersion());
|
2019-05-28 10:57:15 +00:00
|
|
|
m_dataGas += singleByteDataGas();
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:08:19 +00:00
|
|
|
bigint GasMeterVisitor::singleByteDataGas() const
|
2019-05-28 10:57:15 +00:00
|
|
|
{
|
|
|
|
if (m_isCreation)
|
2019-12-11 16:31:36 +00:00
|
|
|
return evmasm::GasCosts::txDataNonZeroGas(m_dialect.evmVersion());
|
2019-05-28 10:57:15 +00:00
|
|
|
else
|
2019-12-11 16:31:36 +00:00
|
|
|
return evmasm::GasCosts::createDataGas;
|
2019-05-28 10:57:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
void GasMeterVisitor::instructionCostsInternal(evmasm::Instruction _instruction)
|
2019-05-28 10:57:15 +00:00
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
if (_instruction == evmasm::Instruction::EXP)
|
|
|
|
m_runGas += evmasm::GasCosts::expGas + evmasm::GasCosts::expByteGas(m_dialect.evmVersion());
|
2021-04-12 10:24:42 +00:00
|
|
|
else if (_instruction == evmasm::Instruction::KECCAK256)
|
|
|
|
// Assumes that Keccak-256 is computed on a single word (rounded up).
|
|
|
|
m_runGas += evmasm::GasCosts::keccak256Gas + evmasm::GasCosts::keccak256WordGas;
|
2019-05-28 10:57:15 +00:00
|
|
|
else
|
2022-11-23 10:51:34 +00:00
|
|
|
m_runGas += evmasm::GasMeter::runGas(_instruction, m_dialect.evmVersion());
|
2019-05-28 10:57:15 +00:00
|
|
|
m_dataGas += singleByteDataGas();
|
|
|
|
}
|