2015-06-01 10:32:59 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-06-01 10:32:59 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-06-01 10:32:59 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-06-01 10:32:59 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-06-01 10:32:59 +00:00
|
|
|
*/
|
|
|
|
/** @file ConstantOptimiser.cpp
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-04-03 12:40:39 +00:00
|
|
|
#include <libevmasm/Exceptions.h>
|
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/EVMVersion.h>
|
2018-03-01 11:06:36 +00:00
|
|
|
|
2017-04-27 10:14:15 +00:00
|
|
|
#include <libdevcore/Assertions.h>
|
2015-06-01 10:32:59 +00:00
|
|
|
#include <libdevcore/CommonData.h>
|
|
|
|
#include <libdevcore/CommonIO.h>
|
|
|
|
|
2017-04-03 12:40:39 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-06-01 10:32:59 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace eth
|
|
|
|
{
|
|
|
|
|
|
|
|
class AssemblyItem;
|
|
|
|
using AssemblyItems = std::vector<AssemblyItem>;
|
|
|
|
class Assembly;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract base class for one way to change how constants are represented in the code.
|
|
|
|
*/
|
|
|
|
class ConstantOptimisationMethod
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Tries to optimised how constants are represented in the source code and modifies
|
2019-01-28 16:29:10 +00:00
|
|
|
/// @a _assembly.
|
2015-06-01 10:32:59 +00:00
|
|
|
/// @returns zero if no optimisations could be performed.
|
|
|
|
static unsigned optimiseConstants(
|
|
|
|
bool _isCreation,
|
|
|
|
size_t _runs,
|
2018-03-01 11:06:36 +00:00
|
|
|
solidity::EVMVersion _evmVersion,
|
2019-01-28 16:29:10 +00:00
|
|
|
Assembly& _assembly
|
2015-06-01 10:32:59 +00:00
|
|
|
);
|
|
|
|
|
2019-02-04 23:21:11 +00:00
|
|
|
protected:
|
|
|
|
/// This is the public API for the optimiser methods, but it doesn't need to be exposed to the caller.
|
|
|
|
|
2015-06-01 10:32:59 +00:00
|
|
|
struct Params
|
|
|
|
{
|
|
|
|
bool isCreation; ///< Whether this is called during contract creation or runtime.
|
|
|
|
size_t runs; ///< Estimated number of calls per opcode oven the lifetime of the contract.
|
|
|
|
size_t multiplicity; ///< Number of times the constant appears in the code.
|
2018-03-01 11:06:36 +00:00
|
|
|
solidity::EVMVersion evmVersion; ///< Version of the EVM
|
2015-06-01 10:32:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit ConstantOptimisationMethod(Params const& _params, u256 const& _value):
|
|
|
|
m_params(_params), m_value(_value) {}
|
2018-05-02 11:29:16 +00:00
|
|
|
virtual ~ConstantOptimisationMethod() = default;
|
2017-08-17 00:14:15 +00:00
|
|
|
virtual bigint gasNeeded() const = 0;
|
2017-01-06 16:05:27 +00:00
|
|
|
/// Executes the method, potentially appending to the assembly and returns a vector of
|
|
|
|
/// assembly items the constant should be relpaced with in one sweep.
|
|
|
|
/// If the vector is empty, the constants will not be deleted.
|
2017-08-17 00:14:15 +00:00
|
|
|
virtual AssemblyItems execute(Assembly& _assembly) const = 0;
|
2015-06-01 10:32:59 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// @returns the run gas for the given items ignoring special gas costs
|
|
|
|
static bigint simpleRunGas(AssemblyItems const& _items);
|
|
|
|
/// @returns the gas needed to store the given data literally
|
|
|
|
bigint dataGas(bytes const& _data) const;
|
|
|
|
static size_t bytesRequired(AssemblyItems const& _items);
|
|
|
|
/// @returns the combined estimated gas usage taking @a m_params into account.
|
|
|
|
bigint combineGas(
|
|
|
|
bigint const& _runGas,
|
|
|
|
bigint const& _repeatedDataGas,
|
|
|
|
bigint const& _uniqueDataGas
|
2017-08-17 00:14:15 +00:00
|
|
|
) const
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
|
|
|
// _runGas is not multiplied by _multiplicity because the runs are "per opcode"
|
|
|
|
return m_params.runs * _runGas + m_params.multiplicity * _repeatedDataGas + _uniqueDataGas;
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:05:27 +00:00
|
|
|
/// Replaces all constants i by the code given in @a _replacement[i].
|
2017-08-22 17:24:02 +00:00
|
|
|
static void replaceConstants(AssemblyItems& _items, std::map<u256, AssemblyItems> const& _replacements);
|
2015-06-01 10:32:59 +00:00
|
|
|
|
|
|
|
Params m_params;
|
|
|
|
u256 const& m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Optimisation method that pushes the constant to the stack literally. This is the default method,
|
|
|
|
* i.e. executing it does not alter the Assembly.
|
|
|
|
*/
|
|
|
|
class LiteralMethod: public ConstantOptimisationMethod
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit LiteralMethod(Params const& _params, u256 const& _value):
|
|
|
|
ConstantOptimisationMethod(_params, _value) {}
|
2018-11-16 01:09:04 +00:00
|
|
|
bigint gasNeeded() const override;
|
|
|
|
AssemblyItems execute(Assembly&) const override { return AssemblyItems{}; }
|
2015-06-01 10:32:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method that stores the data in the .data section of the code and copies it to the stack.
|
|
|
|
*/
|
|
|
|
class CodeCopyMethod: public ConstantOptimisationMethod
|
|
|
|
{
|
|
|
|
public:
|
2019-01-28 11:20:46 +00:00
|
|
|
explicit CodeCopyMethod(Params const& _params, u256 const& _value):
|
|
|
|
ConstantOptimisationMethod(_params, _value) {}
|
2018-11-16 01:09:04 +00:00
|
|
|
bigint gasNeeded() const override;
|
|
|
|
AssemblyItems execute(Assembly& _assembly) const override;
|
2015-06-01 10:32:59 +00:00
|
|
|
|
|
|
|
protected:
|
2017-08-21 21:05:24 +00:00
|
|
|
static AssemblyItems const& copyRoutine();
|
2015-06-01 10:32:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method that tries to compute the constant.
|
|
|
|
*/
|
|
|
|
class ComputeMethod: public ConstantOptimisationMethod
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ComputeMethod(Params const& _params, u256 const& _value):
|
|
|
|
ConstantOptimisationMethod(_params, _value)
|
|
|
|
{
|
|
|
|
m_routine = findRepresentation(m_value);
|
2017-04-27 10:14:15 +00:00
|
|
|
assertThrow(
|
2017-04-27 10:29:26 +00:00
|
|
|
checkRepresentation(m_value, m_routine),
|
2017-04-27 10:14:15 +00:00
|
|
|
OptimizerException,
|
|
|
|
"Invalid constant expression created."
|
|
|
|
);
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 01:09:04 +00:00
|
|
|
bigint gasNeeded() const override { return gasNeeded(m_routine); }
|
|
|
|
AssemblyItems execute(Assembly&) const override
|
2015-06-01 10:32:59 +00:00
|
|
|
{
|
2017-01-06 16:05:27 +00:00
|
|
|
return m_routine;
|
2015-06-01 10:32:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Tries to recursively find a way to compute @a _value.
|
|
|
|
AssemblyItems findRepresentation(u256 const& _value);
|
2017-04-03 12:40:39 +00:00
|
|
|
/// Recomputes the value from the calculated representation and checks for correctness.
|
2017-08-21 21:05:24 +00:00
|
|
|
static bool checkRepresentation(u256 const& _value, AssemblyItems const& _routine);
|
2017-08-17 00:14:15 +00:00
|
|
|
bigint gasNeeded(AssemblyItems const& _routine) const;
|
2015-06-01 10:32:59 +00:00
|
|
|
|
2017-03-03 15:17:21 +00:00
|
|
|
/// Counter for the complexity of optimization, will stop when it reaches zero.
|
|
|
|
size_t m_maxSteps = 10000;
|
2015-06-01 10:32:59 +00:00
|
|
|
AssemblyItems m_routine;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|