2015-04-24 15:35:16 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-04-24 15:35:16 +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-04-24 15:35:16 +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-04-24 15:35:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-04-01 20:11:01 +00:00
|
|
|
#include <libevmasm/Instruction.h>
|
2015-04-24 15:35:16 +00:00
|
|
|
#include <libevmasm/SourceLocation.h>
|
|
|
|
#include <libevmasm/AssemblyItem.h>
|
2015-09-10 10:02:18 +00:00
|
|
|
#include <libevmasm/LinkerObject.h>
|
2016-11-11 10:41:50 +00:00
|
|
|
#include <libevmasm/Exceptions.h>
|
|
|
|
|
2018-03-01 11:06:36 +00:00
|
|
|
#include <libsolidity/interface/EVMVersion.h>
|
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
#include <libdevcore/Common.h>
|
|
|
|
#include <libdevcore/Assertions.h>
|
2018-10-18 11:35:20 +00:00
|
|
|
#include <libdevcore/Keccak256.h>
|
2016-11-11 10:41:50 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
#include <json/json.h>
|
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <memory>
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace eth
|
|
|
|
{
|
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
using AssemblyPointer = std::shared_ptr<Assembly>;
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
class Assembly
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Assembly() {}
|
|
|
|
|
2018-04-30 17:37:50 +00:00
|
|
|
AssemblyItem newTag() { assertThrow(m_usedTags < 0xffffffff, AssemblyException, ""); return AssemblyItem(Tag, m_usedTags++); }
|
|
|
|
AssemblyItem newPushTag() { assertThrow(m_usedTags < 0xffffffff, AssemblyException, ""); return AssemblyItem(PushTag, m_usedTags++); }
|
2017-08-25 15:04:31 +00:00
|
|
|
/// Returns a tag identified by the given name. Creates it if it does not yet exist.
|
|
|
|
AssemblyItem namedTag(std::string const& _name);
|
2016-10-05 10:30:28 +00:00
|
|
|
AssemblyItem newData(bytes const& _data) { h256 h(dev::keccak256(asString(_data))); m_data[h] = _data; return AssemblyItem(PushData, h); }
|
2018-09-21 13:19:14 +00:00
|
|
|
bytes const& data(h256 const& _i) const { return m_data.at(_i); }
|
2016-11-11 10:41:50 +00:00
|
|
|
AssemblyItem newSub(AssemblyPointer const& _sub) { m_subs.push_back(_sub); return AssemblyItem(PushSub, m_subs.size() - 1); }
|
|
|
|
Assembly const& sub(size_t _sub) const { return *m_subs.at(_sub); }
|
|
|
|
Assembly& sub(size_t _sub) { return *m_subs.at(_sub); }
|
2015-04-24 15:35:16 +00:00
|
|
|
AssemblyItem newPushSubSize(u256 const& _subId) { return AssemblyItem(PushSubSize, _subId); }
|
2015-09-10 10:02:18 +00:00
|
|
|
AssemblyItem newPushLibraryAddress(std::string const& _identifier);
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
AssemblyItem const& append(AssemblyItem const& _i);
|
|
|
|
AssemblyItem const& append(std::string const& _data) { return append(newPushString(_data)); }
|
|
|
|
AssemblyItem const& append(bytes const& _data) { return append(newData(_data)); }
|
2018-09-21 13:19:14 +00:00
|
|
|
|
|
|
|
template <class T> Assembly& operator<<(T const& _d) { append(_d); return *this; }
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
/// Pushes the final size of the current assembly itself. Use this when the code is modified
|
|
|
|
/// after compilation and CODESIZE is not an option.
|
|
|
|
void appendProgramSize() { append(AssemblyItem(PushProgramSize)); }
|
2015-09-10 10:02:18 +00:00
|
|
|
void appendLibraryAddress(std::string const& _identifier) { append(newPushLibraryAddress(_identifier)); }
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2016-04-04 11:18:24 +00:00
|
|
|
AssemblyItem appendJump() { auto ret = append(newPushTag()); append(solidity::Instruction::JUMP); return ret; }
|
|
|
|
AssemblyItem appendJumpI() { auto ret = append(newPushTag()); append(solidity::Instruction::JUMPI); return ret; }
|
|
|
|
AssemblyItem appendJump(AssemblyItem const& _tag) { auto ret = append(_tag.pushTag()); append(solidity::Instruction::JUMP); return ret; }
|
|
|
|
AssemblyItem appendJumpI(AssemblyItem const& _tag) { auto ret = append(_tag.pushTag()); append(solidity::Instruction::JUMPI); return ret; }
|
2015-05-15 10:23:13 +00:00
|
|
|
|
2017-06-14 19:38:59 +00:00
|
|
|
/// Adds a subroutine to the code (in the data section) and pushes its size (via a tag)
|
|
|
|
/// on the stack. @returns the pushsub assembly item.
|
|
|
|
AssemblyItem appendSubroutine(AssemblyPointer const& _assembly) { auto sub = newSub(_assembly); append(newPushSubSize(size_t(sub.data()))); return sub; }
|
|
|
|
void pushSubroutineSize(size_t _subRoutine) { append(newPushSubSize(_subRoutine)); }
|
|
|
|
/// Pushes the offset of the subroutine.
|
|
|
|
void pushSubroutineOffset(size_t _subRoutine) { append(AssemblyItem(PushSub, _subRoutine)); }
|
|
|
|
|
2016-11-24 09:32:52 +00:00
|
|
|
/// Appends @a _data literally to the very end of the bytecode.
|
|
|
|
void appendAuxiliaryDataToEnd(bytes const& _data) { m_auxiliaryData += _data; }
|
|
|
|
|
2018-09-21 13:19:14 +00:00
|
|
|
/// Returns the assembly items.
|
2015-09-08 15:11:02 +00:00
|
|
|
AssemblyItems const& items() const { return m_items; }
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
int deposit() const { return m_deposit; }
|
2017-06-20 08:52:58 +00:00
|
|
|
void adjustDeposit(int _adjustment) { m_deposit += _adjustment; assertThrow(m_deposit >= 0, InvalidDeposit, ""); }
|
|
|
|
void setDeposit(int _deposit) { m_deposit = _deposit; assertThrow(m_deposit >= 0, InvalidDeposit, ""); }
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
/// Changes the source location used for each appended item.
|
|
|
|
void setSourceLocation(SourceLocation const& _location) { m_currentSourceLocation = _location; }
|
|
|
|
|
2018-09-21 13:19:14 +00:00
|
|
|
/// Assembles the assembly into bytecode. The assembly should not be modified after this call, since the assembled version is cached.
|
2015-09-10 10:02:18 +00:00
|
|
|
LinkerObject const& assemble() const;
|
2015-06-01 10:32:59 +00:00
|
|
|
|
2017-06-30 21:10:55 +00:00
|
|
|
struct OptimiserSettings
|
|
|
|
{
|
|
|
|
bool isCreation = false;
|
2017-07-27 22:28:49 +00:00
|
|
|
bool runJumpdestRemover = false;
|
2017-06-30 21:10:55 +00:00
|
|
|
bool runPeephole = false;
|
|
|
|
bool runDeduplicate = false;
|
|
|
|
bool runCSE = false;
|
|
|
|
bool runConstantOptimiser = false;
|
2018-03-01 11:06:36 +00:00
|
|
|
solidity::EVMVersion evmVersion;
|
2017-06-30 21:10:55 +00:00
|
|
|
/// This specifies an estimate on how often each opcode in this assembly will be executed,
|
|
|
|
/// i.e. use a small value to optimise for size and a large value to optimise for runtime gas usage.
|
|
|
|
size_t expectedExecutionsPerDeployment = 200;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Execute optimisation passes as defined by @a _settings and return the optimised assembly.
|
2017-07-27 22:28:49 +00:00
|
|
|
Assembly& optimise(OptimiserSettings const& _settings);
|
2017-06-30 21:10:55 +00:00
|
|
|
|
2015-06-01 10:32:59 +00:00
|
|
|
/// Modify (if @a _enable is set) and return the current assembly such that creation and
|
|
|
|
/// execution gas usage is optimised. @a _isCreation should be true for the top-level assembly.
|
|
|
|
/// @a _runs specifes an estimate on how often each opcode in this assembly will be executed,
|
|
|
|
/// i.e. use a small value to optimise for size and a large value to optimise for runtime.
|
2016-11-11 13:11:07 +00:00
|
|
|
/// If @a _enable is not set, will perform some simple peephole optimizations.
|
2018-03-01 11:06:36 +00:00
|
|
|
Assembly& optimise(bool _enable, EVMVersion _evmVersion, bool _isCreation = true, size_t _runs = 200);
|
2017-06-30 21:10:55 +00:00
|
|
|
|
2017-06-15 09:22:47 +00:00
|
|
|
/// Create a text representation of the assembly.
|
2017-08-30 01:17:15 +00:00
|
|
|
std::string assemblyString(
|
|
|
|
StringMap const& _sourceCodes = StringMap()
|
|
|
|
) const;
|
2017-08-30 00:58:19 +00:00
|
|
|
void assemblyStream(
|
2015-04-24 15:35:16 +00:00
|
|
|
std::ostream& _out,
|
|
|
|
std::string const& _prefix = "",
|
2017-06-15 09:22:47 +00:00
|
|
|
StringMap const& _sourceCodes = StringMap()
|
|
|
|
) const;
|
|
|
|
|
|
|
|
/// Create a JSON representation of the assembly.
|
|
|
|
Json::Value assemblyJSON(
|
|
|
|
StringMap const& _sourceCodes = StringMap()
|
2015-04-24 15:35:16 +00:00
|
|
|
) const;
|
2015-09-10 10:02:18 +00:00
|
|
|
|
2018-09-21 13:19:14 +00:00
|
|
|
public:
|
|
|
|
// These features are only used by LLL
|
|
|
|
AssemblyItem newPushString(std::string const& _data) { h256 h(dev::keccak256(_data)); m_strings[h] = _data; return AssemblyItem(PushString, h); }
|
|
|
|
|
|
|
|
void append(Assembly const& _a);
|
|
|
|
void append(Assembly const& _a, int _deposit);
|
|
|
|
|
|
|
|
void injectStart(AssemblyItem const& _i);
|
|
|
|
|
|
|
|
AssemblyItem const& back() const { return m_items.back(); }
|
|
|
|
std::string backString() const { return m_items.size() && m_items.back().type() == PushString ? m_strings.at((h256)m_items.back().data()) : std::string(); }
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
protected:
|
2016-11-10 17:16:21 +00:00
|
|
|
/// Does the same operations as @a optimise, but should only be applied to a sub and
|
2017-07-27 22:28:49 +00:00
|
|
|
/// returns the replaced tags. Also takes an argument containing the tags of this assembly
|
|
|
|
/// that are referenced in a super-assembly.
|
|
|
|
std::map<u256, u256> optimiseInternal(OptimiserSettings const& _settings, std::set<size_t> const& _tagsReferencedFromOutside);
|
2016-11-10 17:16:21 +00:00
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
unsigned bytesRequired(unsigned subTagSize) const;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
private:
|
2017-08-28 14:30:01 +00:00
|
|
|
static Json::Value createJsonValue(std::string _name, int _begin, int _end, std::string _value = std::string(), std::string _jumpType = std::string());
|
|
|
|
static std::string toStringInHex(u256 _value);
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
protected:
|
2016-11-24 09:32:52 +00:00
|
|
|
/// 0 is reserved for exception
|
2015-05-15 10:23:13 +00:00
|
|
|
unsigned m_usedTags = 1;
|
2017-08-25 15:04:31 +00:00
|
|
|
std::map<std::string, size_t> m_namedTags;
|
2015-04-24 15:35:16 +00:00
|
|
|
AssemblyItems m_items;
|
2015-09-10 10:02:18 +00:00
|
|
|
std::map<h256, bytes> m_data;
|
2016-11-24 09:32:52 +00:00
|
|
|
/// Data that is appended to the very end of the contract.
|
|
|
|
bytes m_auxiliaryData;
|
2016-11-11 10:41:50 +00:00
|
|
|
std::vector<std::shared_ptr<Assembly>> m_subs;
|
2015-04-24 15:35:16 +00:00
|
|
|
std::map<h256, std::string> m_strings;
|
2015-09-10 10:02:18 +00:00
|
|
|
std::map<h256, std::string> m_libraries; ///< Identifiers of libraries to be linked.
|
|
|
|
|
|
|
|
mutable LinkerObject m_assembledObject;
|
2016-11-10 17:16:21 +00:00
|
|
|
mutable std::vector<size_t> m_tagPositionsInBytecode;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
int m_deposit = 0;
|
|
|
|
|
|
|
|
SourceLocation m_currentSourceLocation;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& _out, Assembly const& _a)
|
|
|
|
{
|
2017-06-15 09:22:47 +00:00
|
|
|
_a.assemblyStream(_out);
|
2015-04-24 15:35:16 +00:00
|
|
|
return _out;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|