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
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2015-04-24 15:35:16 +00:00
|
|
|
/** @file Assembly.cpp
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
*/
|
|
|
|
|
2019-03-28 12:07:52 +00:00
|
|
|
#include <libevmasm/Assembly.h>
|
2016-11-11 13:11:07 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
#include <libevmasm/CommonSubexpressionEliminator.h>
|
|
|
|
#include <libevmasm/ControlFlowGraph.h>
|
2016-11-11 13:11:07 +00:00
|
|
|
#include <libevmasm/PeepholeOptimiser.h>
|
2021-01-14 12:02:14 +00:00
|
|
|
#include <libevmasm/Inliner.h>
|
2017-07-27 22:28:49 +00:00
|
|
|
#include <libevmasm/JumpdestRemover.h>
|
2015-05-12 14:16:44 +00:00
|
|
|
#include <libevmasm/BlockDeduplicator.h>
|
2015-06-01 10:32:59 +00:00
|
|
|
#include <libevmasm/ConstantOptimiser.h>
|
|
|
|
#include <libevmasm/GasMeter.h>
|
2016-11-11 13:11:07 +00:00
|
|
|
|
2020-07-21 13:20:05 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
#include <json/json.h>
|
2016-11-11 13:11:07 +00:00
|
|
|
|
2021-03-17 18:37:39 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <range/v3/algorithm/any_of.hpp>
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
using namespace std;
|
2019-12-11 16:31:36 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::util;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2018-09-19 16:29:43 +00:00
|
|
|
AssemblyItem const& Assembly::append(AssemblyItem const& _i)
|
|
|
|
{
|
|
|
|
assertThrow(m_deposit >= 0, AssemblyException, "Stack underflow.");
|
2020-06-02 13:10:14 +00:00
|
|
|
m_deposit += static_cast<int>(_i.deposit());
|
2018-12-10 18:02:39 +00:00
|
|
|
m_items.emplace_back(_i);
|
2020-02-03 07:04:21 +00:00
|
|
|
if (!m_items.back().location().isValid() && m_currentSourceLocation.isValid())
|
2018-09-19 16:29:43 +00:00
|
|
|
m_items.back().setLocation(m_currentSourceLocation);
|
2019-10-08 09:00:40 +00:00
|
|
|
m_items.back().m_modifierDepth = m_currentModifierDepth;
|
2020-01-15 12:55:30 +00:00
|
|
|
return m_items.back();
|
2018-09-19 16:29:43 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
unsigned Assembly::bytesRequired(unsigned subTagSize) const
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2016-11-16 14:09:01 +00:00
|
|
|
for (unsigned tagSize = subTagSize; true; ++tagSize)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
size_t ret = 1;
|
2015-04-24 15:35:16 +00:00
|
|
|
for (auto const& i: m_data)
|
|
|
|
ret += i.second.size();
|
|
|
|
|
|
|
|
for (AssemblyItem const& i: m_items)
|
2016-11-11 10:41:50 +00:00
|
|
|
ret += i.bytesRequired(tagSize);
|
2019-12-11 16:31:36 +00:00
|
|
|
if (util::bytesRequired(ret) <= tagSize)
|
2019-12-12 23:39:29 +00:00
|
|
|
return static_cast<unsigned>(ret);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:09:10 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
string locationFromSources(StringMap const& _sourceCodes, SourceLocation const& _location)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2020-02-03 07:04:21 +00:00
|
|
|
if (!_location.hasText() || _sourceCodes.empty())
|
2015-04-24 15:35:16 +00:00
|
|
|
return "";
|
|
|
|
|
2018-11-28 15:19:22 +00:00
|
|
|
auto it = _sourceCodes.find(_location.source->name());
|
2015-04-24 15:35:16 +00:00
|
|
|
if (it == _sourceCodes.end())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
string const& source = it->second;
|
2020-06-02 13:10:14 +00:00
|
|
|
if (static_cast<size_t>(_location.start) >= source.size())
|
2015-04-24 15:35:16 +00:00
|
|
|
return "";
|
|
|
|
|
2020-06-02 13:10:14 +00:00
|
|
|
string cut = source.substr(static_cast<size_t>(_location.start), static_cast<size_t>(_location.end - _location.start));
|
2015-04-24 15:35:16 +00:00
|
|
|
auto newLinePos = cut.find_first_of("\n");
|
|
|
|
if (newLinePos != string::npos)
|
|
|
|
cut = cut.substr(0, newLinePos) + "...";
|
|
|
|
|
2015-06-08 10:09:24 +00:00
|
|
|
return cut;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 00:09:10 +00:00
|
|
|
class Functionalizer
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2017-01-24 00:09:10 +00:00
|
|
|
public:
|
2020-06-17 09:17:35 +00:00
|
|
|
Functionalizer (ostream& _out, string const& _prefix, StringMap const& _sourceCodes, Assembly const& _assembly):
|
|
|
|
m_out(_out), m_prefix(_prefix), m_sourceCodes(_sourceCodes), m_assembly(_assembly)
|
2017-01-24 00:09:10 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void feed(AssemblyItem const& _item)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2020-02-03 07:04:21 +00:00
|
|
|
if (_item.location().isValid() && _item.location() != m_location)
|
2017-01-24 00:09:10 +00:00
|
|
|
{
|
|
|
|
flush();
|
|
|
|
m_location = _item.location();
|
2017-02-16 18:58:08 +00:00
|
|
|
printLocation();
|
2017-01-24 00:09:10 +00:00
|
|
|
}
|
2020-06-17 09:17:35 +00:00
|
|
|
|
|
|
|
string expression = _item.toAssemblyText(m_assembly);
|
|
|
|
|
2017-01-24 00:09:10 +00:00
|
|
|
if (!(
|
|
|
|
_item.canBeFunctional() &&
|
|
|
|
_item.returnValues() <= 1 &&
|
2020-06-02 13:10:14 +00:00
|
|
|
_item.arguments() <= m_pending.size()
|
2017-01-24 00:09:10 +00:00
|
|
|
))
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2017-01-24 00:09:10 +00:00
|
|
|
flush();
|
2020-06-17 09:17:35 +00:00
|
|
|
m_out << m_prefix << (_item.type() == Tag ? "" : " ") << expression << endl;
|
2017-01-24 00:09:10 +00:00
|
|
|
return;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
2017-01-24 00:09:10 +00:00
|
|
|
if (_item.arguments() > 0)
|
|
|
|
{
|
|
|
|
expression += "(";
|
2020-06-02 13:10:14 +00:00
|
|
|
for (size_t i = 0; i < _item.arguments(); ++i)
|
2017-01-24 00:09:10 +00:00
|
|
|
{
|
|
|
|
expression += m_pending.back();
|
|
|
|
m_pending.pop_back();
|
|
|
|
if (i + 1 < _item.arguments())
|
|
|
|
expression += ", ";
|
|
|
|
}
|
|
|
|
expression += ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pending.push_back(expression);
|
|
|
|
if (_item.returnValues() != 1)
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush()
|
|
|
|
{
|
|
|
|
for (string const& expression: m_pending)
|
|
|
|
m_out << m_prefix << " " << expression << endl;
|
|
|
|
m_pending.clear();
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 00:09:10 +00:00
|
|
|
void printLocation()
|
|
|
|
{
|
2020-02-03 07:04:21 +00:00
|
|
|
if (!m_location.isValid())
|
2017-01-24 00:09:10 +00:00
|
|
|
return;
|
|
|
|
m_out << m_prefix << " /*";
|
2018-11-28 15:19:22 +00:00
|
|
|
if (m_location.source)
|
|
|
|
m_out << " \"" + m_location.source->name() + "\"";
|
2020-02-03 07:04:21 +00:00
|
|
|
if (m_location.hasText())
|
2017-01-24 00:09:10 +00:00
|
|
|
m_out << ":" << to_string(m_location.start) + ":" + to_string(m_location.end);
|
|
|
|
m_out << " " << locationFromSources(m_sourceCodes, m_location);
|
|
|
|
m_out << " */" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
strings m_pending;
|
|
|
|
SourceLocation m_location;
|
|
|
|
|
|
|
|
ostream& m_out;
|
|
|
|
string const& m_prefix;
|
|
|
|
StringMap const& m_sourceCodes;
|
2020-06-17 09:17:35 +00:00
|
|
|
Assembly const& m_assembly;
|
2017-01-24 00:09:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-30 00:58:19 +00:00
|
|
|
void Assembly::assemblyStream(ostream& _out, string const& _prefix, StringMap const& _sourceCodes) const
|
2017-01-24 00:09:10 +00:00
|
|
|
{
|
2020-06-17 09:17:35 +00:00
|
|
|
Functionalizer f(_out, _prefix, _sourceCodes, *this);
|
2017-01-24 00:09:10 +00:00
|
|
|
|
|
|
|
for (auto const& i: m_items)
|
|
|
|
f.feed(i);
|
|
|
|
f.flush();
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
if (!m_data.empty() || !m_subs.empty())
|
|
|
|
{
|
2016-11-15 13:01:11 +00:00
|
|
|
_out << _prefix << "stop" << endl;
|
2015-04-24 15:35:16 +00:00
|
|
|
for (auto const& i: m_data)
|
2017-04-11 17:53:55 +00:00
|
|
|
if (u256(i.first) >= m_subs.size())
|
|
|
|
_out << _prefix << "data_" << toHex(u256(i.first)) << " " << toHex(i.second) << endl;
|
2016-11-15 13:01:11 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
for (size_t i = 0; i < m_subs.size(); ++i)
|
|
|
|
{
|
2016-11-15 13:01:11 +00:00
|
|
|
_out << endl << _prefix << "sub_" << i << ": assembly {\n";
|
2017-06-15 09:22:47 +00:00
|
|
|
m_subs[i]->assemblyStream(_out, _prefix + " ", _sourceCodes);
|
2016-11-15 13:01:11 +00:00
|
|
|
_out << _prefix << "}" << endl;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-15 13:01:11 +00:00
|
|
|
|
2017-06-14 12:31:04 +00:00
|
|
|
if (m_auxiliaryData.size() > 0)
|
|
|
|
_out << endl << _prefix << "auxdata: 0x" << toHex(m_auxiliaryData) << endl;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 01:17:15 +00:00
|
|
|
string Assembly::assemblyString(StringMap const& _sourceCodes) const
|
|
|
|
{
|
|
|
|
ostringstream tmp;
|
|
|
|
assemblyStream(tmp, "", _sourceCodes);
|
|
|
|
return tmp.str();
|
|
|
|
}
|
|
|
|
|
2020-02-18 09:22:34 +00:00
|
|
|
Json::Value Assembly::createJsonValue(string _name, int _source, int _begin, int _end, string _value, string _jumpType)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
Json::Value value;
|
|
|
|
value["name"] = _name;
|
2020-02-18 09:22:34 +00:00
|
|
|
value["source"] = _source;
|
2015-04-24 15:35:16 +00:00
|
|
|
value["begin"] = _begin;
|
|
|
|
value["end"] = _end;
|
|
|
|
if (!_value.empty())
|
|
|
|
value["value"] = _value;
|
|
|
|
if (!_jumpType.empty())
|
|
|
|
value["jumpType"] = _jumpType;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-08-28 14:30:01 +00:00
|
|
|
string Assembly::toStringInHex(u256 _value)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
std::stringstream hexStr;
|
2019-11-04 16:00:48 +00:00
|
|
|
hexStr << std::uppercase << hex << _value;
|
2015-04-24 15:35:16 +00:00
|
|
|
return hexStr.str();
|
|
|
|
}
|
|
|
|
|
2020-02-18 09:22:34 +00:00
|
|
|
Json::Value Assembly::assemblyJSON(map<string, unsigned> const& _sourceIndices) const
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
Json::Value root;
|
|
|
|
|
2017-07-27 14:40:01 +00:00
|
|
|
Json::Value& collection = root[".code"] = Json::arrayValue;
|
2015-04-24 15:35:16 +00:00
|
|
|
for (AssemblyItem const& i: m_items)
|
|
|
|
{
|
2020-06-02 13:10:14 +00:00
|
|
|
int sourceIndex = -1;
|
2020-02-18 09:22:34 +00:00
|
|
|
if (i.location().source)
|
|
|
|
{
|
|
|
|
auto iter = _sourceIndices.find(i.location().source->name());
|
|
|
|
if (iter != _sourceIndices.end())
|
2020-06-02 13:10:14 +00:00
|
|
|
sourceIndex = static_cast<int>(iter->second);
|
2020-02-18 09:22:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
switch (i.type())
|
|
|
|
{
|
|
|
|
case Operation:
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue(
|
|
|
|
instructionInfo(i.instruction()).name,
|
|
|
|
sourceIndex,
|
|
|
|
i.location().start,
|
|
|
|
i.location().end,
|
|
|
|
i.getJumpTypeAsString())
|
|
|
|
);
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case Push:
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("PUSH", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data()), i.getJumpTypeAsString()));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushString:
|
|
|
|
collection.append(
|
2019-12-12 23:39:29 +00:00
|
|
|
createJsonValue("PUSH tag", sourceIndex, i.location().start, i.location().end, m_strings.at(h256(i.data()))));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushTag:
|
2015-05-15 10:23:13 +00:00
|
|
|
if (i.data() == 0)
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("PUSH [ErrorTag]", sourceIndex, i.location().start, i.location().end, ""));
|
2015-05-20 11:15:01 +00:00
|
|
|
else
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("PUSH [tag]", sourceIndex, i.location().start, i.location().end, toString(i.data())));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushSub:
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("PUSH [$]", sourceIndex, i.location().start, i.location().end, toString(h256(i.data()))));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushSubSize:
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("PUSH #[$]", sourceIndex, i.location().start, i.location().end, toString(h256(i.data()))));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushProgramSize:
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("PUSHSIZE", sourceIndex, i.location().start, i.location().end));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
2015-09-10 10:02:18 +00:00
|
|
|
case PushLibraryAddress:
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("PUSHLIB", sourceIndex, i.location().start, i.location().end, m_libraries.at(h256(i.data())))
|
2015-09-10 10:02:18 +00:00
|
|
|
);
|
|
|
|
break;
|
2017-11-14 11:58:04 +00:00
|
|
|
case PushDeployTimeAddress:
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("PUSHDEPLOYADDRESS", sourceIndex, i.location().start, i.location().end)
|
2017-11-14 11:58:04 +00:00
|
|
|
);
|
|
|
|
break;
|
2020-03-10 13:30:04 +00:00
|
|
|
case PushImmutable:
|
|
|
|
collection.append(createJsonValue(
|
|
|
|
"PUSHIMMUTABLE",
|
|
|
|
sourceIndex,
|
|
|
|
i.location().start,
|
|
|
|
i.location().end,
|
|
|
|
m_immutables.at(h256(i.data()))
|
|
|
|
));
|
|
|
|
break;
|
|
|
|
case AssignImmutable:
|
|
|
|
collection.append(createJsonValue(
|
|
|
|
"ASSIGNIMMUTABLE",
|
|
|
|
sourceIndex,
|
|
|
|
i.location().start,
|
|
|
|
i.location().end,
|
|
|
|
m_immutables.at(h256(i.data()))
|
|
|
|
));
|
|
|
|
break;
|
2015-04-24 15:35:16 +00:00
|
|
|
case Tag:
|
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("tag", sourceIndex, i.location().start, i.location().end, toString(i.data())));
|
2015-04-24 15:35:16 +00:00
|
|
|
collection.append(
|
2020-02-18 09:22:34 +00:00
|
|
|
createJsonValue("JUMPDEST", sourceIndex, i.location().start, i.location().end));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushData:
|
2020-02-18 09:22:34 +00:00
|
|
|
collection.append(createJsonValue("PUSH data", sourceIndex, i.location().start, i.location().end, toStringInHex(i.data())));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
2021-03-17 18:37:39 +00:00
|
|
|
case VerbatimBytecode:
|
|
|
|
collection.append(createJsonValue("VERBATIM", sourceIndex, i.location().start, i.location().end, toHex(i.verbatimData())));
|
|
|
|
break;
|
2015-04-24 15:35:16 +00:00
|
|
|
default:
|
2019-11-22 17:02:12 +00:00
|
|
|
assertThrow(false, InvalidOpcode, "");
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_data.empty() || !m_subs.empty())
|
|
|
|
{
|
2017-07-27 14:40:01 +00:00
|
|
|
Json::Value& data = root[".data"] = Json::objectValue;
|
2015-04-24 15:35:16 +00:00
|
|
|
for (auto const& i: m_data)
|
|
|
|
if (u256(i.first) >= m_subs.size())
|
|
|
|
data[toStringInHex((u256)i.first)] = toHex(i.second);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_subs.size(); ++i)
|
|
|
|
{
|
|
|
|
std::stringstream hexStr;
|
|
|
|
hexStr << hex << i;
|
2020-02-18 09:22:34 +00:00
|
|
|
data[hexStr.str()] = m_subs[i]->assemblyJSON(_sourceIndices);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 12:30:34 +00:00
|
|
|
|
2017-06-14 12:31:04 +00:00
|
|
|
if (m_auxiliaryData.size() > 0)
|
|
|
|
root[".auxdata"] = toHex(m_auxiliaryData);
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:46:47 +00:00
|
|
|
AssemblyItem Assembly::namedTag(string const& _name, size_t _params, size_t _returns, optional<uint64_t> _sourceID)
|
2017-08-25 15:04:31 +00:00
|
|
|
{
|
2017-09-18 10:04:09 +00:00
|
|
|
assertThrow(!_name.empty(), AssemblyException, "Empty named tag.");
|
2020-05-07 12:46:47 +00:00
|
|
|
if (m_namedTags.count(_name))
|
|
|
|
{
|
|
|
|
assertThrow(m_namedTags.at(_name).params == _params, AssemblyException, "");
|
|
|
|
assertThrow(m_namedTags.at(_name).returns == _returns, AssemblyException, "");
|
|
|
|
assertThrow(m_namedTags.at(_name).sourceID == _sourceID, AssemblyException, "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_namedTags[_name] = {static_cast<size_t>(newTag().data()), _sourceID, _params, _returns};
|
|
|
|
return AssemblyItem{Tag, m_namedTags.at(_name).id};
|
2017-08-25 15:04:31 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 10:02:18 +00:00
|
|
|
AssemblyItem Assembly::newPushLibraryAddress(string const& _identifier)
|
|
|
|
{
|
2019-12-11 16:31:36 +00:00
|
|
|
h256 h(util::keccak256(_identifier));
|
2015-09-10 10:02:18 +00:00
|
|
|
m_libraries[h] = _identifier;
|
2018-12-10 18:03:47 +00:00
|
|
|
return AssemblyItem{PushLibraryAddress, h};
|
2015-09-10 10:02:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 13:30:04 +00:00
|
|
|
AssemblyItem Assembly::newPushImmutable(string const& _identifier)
|
|
|
|
{
|
|
|
|
h256 h(util::keccak256(_identifier));
|
|
|
|
m_immutables[h] = _identifier;
|
|
|
|
return AssemblyItem{PushImmutable, h};
|
|
|
|
}
|
|
|
|
|
|
|
|
AssemblyItem Assembly::newImmutableAssignment(string const& _identifier)
|
|
|
|
{
|
|
|
|
h256 h(util::keccak256(_identifier));
|
|
|
|
m_immutables[h] = _identifier;
|
|
|
|
return AssemblyItem{AssignImmutable, h};
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:06:36 +00:00
|
|
|
Assembly& Assembly::optimise(bool _enable, EVMVersion _evmVersion, bool _isCreation, size_t _runs)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2017-06-30 21:10:55 +00:00
|
|
|
OptimiserSettings settings;
|
|
|
|
settings.isCreation = _isCreation;
|
2021-01-14 12:02:14 +00:00
|
|
|
settings.runInliner = true;
|
2017-07-27 22:28:49 +00:00
|
|
|
settings.runJumpdestRemover = true;
|
2017-06-30 21:10:55 +00:00
|
|
|
settings.runPeephole = true;
|
|
|
|
if (_enable)
|
|
|
|
{
|
|
|
|
settings.runDeduplicate = true;
|
|
|
|
settings.runCSE = true;
|
|
|
|
settings.runConstantOptimiser = true;
|
|
|
|
}
|
2018-03-01 11:06:36 +00:00
|
|
|
settings.evmVersion = _evmVersion;
|
2017-06-30 21:10:55 +00:00
|
|
|
settings.expectedExecutionsPerDeployment = _runs;
|
2017-07-27 22:28:49 +00:00
|
|
|
optimise(settings);
|
2017-06-30 21:10:55 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-27 22:28:49 +00:00
|
|
|
Assembly& Assembly::optimise(OptimiserSettings const& _settings)
|
2017-06-30 21:10:55 +00:00
|
|
|
{
|
2017-07-27 22:28:49 +00:00
|
|
|
optimiseInternal(_settings, {});
|
2016-11-10 17:16:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2017-07-27 22:28:49 +00:00
|
|
|
map<u256, u256> Assembly::optimiseInternal(
|
|
|
|
OptimiserSettings const& _settings,
|
2019-02-20 11:10:29 +00:00
|
|
|
std::set<size_t> _tagsReferencedFromOutside
|
2017-07-27 22:28:49 +00:00
|
|
|
)
|
2016-11-10 17:16:21 +00:00
|
|
|
{
|
2017-06-30 21:10:55 +00:00
|
|
|
// Run optimisation for sub-assemblies.
|
2016-11-10 17:16:21 +00:00
|
|
|
for (size_t subId = 0; subId < m_subs.size(); ++subId)
|
|
|
|
{
|
2017-06-30 21:10:55 +00:00
|
|
|
OptimiserSettings settings = _settings;
|
|
|
|
// Disable creation mode for sub-assemblies.
|
|
|
|
settings.isCreation = false;
|
2017-07-27 22:28:49 +00:00
|
|
|
map<u256, u256> subTagReplacements = m_subs[subId]->optimiseInternal(
|
|
|
|
settings,
|
|
|
|
JumpdestRemover::referencedTags(m_items, subId)
|
|
|
|
);
|
2017-06-30 21:10:55 +00:00
|
|
|
// Apply the replacements (can be empty).
|
2016-11-10 17:16:21 +00:00
|
|
|
BlockDeduplicator::applyTagReplacement(m_items, subTagReplacements, subId);
|
|
|
|
}
|
|
|
|
|
|
|
|
map<u256, u256> tagReplacements;
|
2017-06-30 21:10:55 +00:00
|
|
|
// Iterate until no new optimisation possibilities are found.
|
2016-12-01 10:31:58 +00:00
|
|
|
for (unsigned count = 1; count > 0;)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
|
2021-01-14 12:02:14 +00:00
|
|
|
if (_settings.runInliner)
|
|
|
|
Inliner{
|
|
|
|
m_items,
|
|
|
|
_tagsReferencedFromOutside,
|
|
|
|
_settings.expectedExecutionsPerDeployment,
|
|
|
|
_settings.isCreation,
|
|
|
|
_settings.evmVersion
|
|
|
|
}.optimise();
|
|
|
|
|
2017-07-27 22:28:49 +00:00
|
|
|
if (_settings.runJumpdestRemover)
|
|
|
|
{
|
2018-12-10 18:03:47 +00:00
|
|
|
JumpdestRemover jumpdestOpt{m_items};
|
2017-07-27 22:28:49 +00:00
|
|
|
if (jumpdestOpt.optimise(_tagsReferencedFromOutside))
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:10:55 +00:00
|
|
|
if (_settings.runPeephole)
|
|
|
|
{
|
2018-12-10 18:03:47 +00:00
|
|
|
PeepholeOptimiser peepOpt{m_items};
|
2017-10-16 16:45:21 +00:00
|
|
|
while (peepOpt.optimise())
|
|
|
|
{
|
2017-06-30 21:10:55 +00:00
|
|
|
count++;
|
2017-10-16 16:45:21 +00:00
|
|
|
assertThrow(count < 64000, OptimizerException, "Peephole optimizer seems to be stuck.");
|
|
|
|
}
|
2017-06-30 21:10:55 +00:00
|
|
|
}
|
2016-11-11 13:11:07 +00:00
|
|
|
|
2015-05-28 12:43:46 +00:00
|
|
|
// This only modifies PushTags, we have to run again to actually remove code.
|
2017-06-30 21:10:55 +00:00
|
|
|
if (_settings.runDeduplicate)
|
2016-11-10 17:16:21 +00:00
|
|
|
{
|
2020-05-24 17:41:15 +00:00
|
|
|
BlockDeduplicator deduplicator{m_items};
|
|
|
|
if (deduplicator.deduplicate())
|
2017-06-30 21:10:55 +00:00
|
|
|
{
|
2020-05-24 17:41:15 +00:00
|
|
|
for (auto const& replacement: deduplicator.replacedTags())
|
2019-02-20 11:10:29 +00:00
|
|
|
{
|
|
|
|
assertThrow(
|
2020-06-02 13:10:14 +00:00
|
|
|
replacement.first <= numeric_limits<size_t>::max() && replacement.second <= numeric_limits<size_t>::max(),
|
2019-02-20 11:10:29 +00:00
|
|
|
OptimizerException,
|
|
|
|
"Invalid tag replacement."
|
|
|
|
);
|
|
|
|
assertThrow(
|
|
|
|
!tagReplacements.count(replacement.first),
|
|
|
|
OptimizerException,
|
|
|
|
"Replacement already known."
|
|
|
|
);
|
|
|
|
tagReplacements[replacement.first] = replacement.second;
|
2020-06-02 13:10:14 +00:00
|
|
|
if (_tagsReferencedFromOutside.erase(static_cast<size_t>(replacement.first)))
|
|
|
|
_tagsReferencedFromOutside.insert(static_cast<size_t>(replacement.second));
|
2019-02-20 11:10:29 +00:00
|
|
|
}
|
2017-06-30 21:10:55 +00:00
|
|
|
count++;
|
|
|
|
}
|
2016-11-10 17:16:21 +00:00
|
|
|
}
|
2015-05-28 12:43:46 +00:00
|
|
|
|
2017-06-30 21:10:55 +00:00
|
|
|
if (_settings.runCSE)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2016-11-10 17:16:21 +00:00
|
|
|
// Control flow graph optimization has been here before but is disabled because it
|
|
|
|
// assumes we only jump to tags that are pushed. This is not the case anymore with
|
|
|
|
// function types that can be stored in storage.
|
2015-05-06 17:15:14 +00:00
|
|
|
AssemblyItems optimisedItems;
|
2016-11-10 17:16:21 +00:00
|
|
|
|
2021-03-17 18:37:39 +00:00
|
|
|
bool usesMSize = ranges::any_of(m_items, [](AssemblyItem const& _i) {
|
|
|
|
return _i == AssemblyItem{Instruction::MSIZE} || _i.type() == VerbatimBytecode;
|
|
|
|
});
|
2018-03-08 18:41:29 +00:00
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
auto iter = m_items.begin();
|
|
|
|
while (iter != m_items.end())
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2016-10-21 15:33:30 +00:00
|
|
|
KnownState emptyState;
|
2018-12-10 18:03:47 +00:00
|
|
|
CommonSubexpressionEliminator eliminator{emptyState};
|
2016-11-22 13:55:09 +00:00
|
|
|
auto orig = iter;
|
2018-03-08 18:41:29 +00:00
|
|
|
iter = eliminator.feedItems(iter, m_items.end(), usesMSize);
|
2016-11-22 13:55:09 +00:00
|
|
|
bool shouldReplace = false;
|
|
|
|
AssemblyItems optimisedChunk;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
optimisedChunk = eliminator.getOptimizedItems();
|
2020-06-02 13:10:14 +00:00
|
|
|
shouldReplace = (optimisedChunk.size() < static_cast<size_t>(iter - orig));
|
2016-11-22 13:55:09 +00:00
|
|
|
}
|
|
|
|
catch (StackTooDeepException const&)
|
|
|
|
{
|
|
|
|
// This might happen if the opcode reconstruction is not as efficient
|
|
|
|
// as the hand-crafted code.
|
|
|
|
}
|
|
|
|
catch (ItemNotAvailableException const&)
|
2015-05-06 17:15:14 +00:00
|
|
|
{
|
2016-11-22 13:55:09 +00:00
|
|
|
// This might happen if e.g. associativity and commutativity rules
|
|
|
|
// reorganise the expression tree, but not all leaves are available.
|
2015-05-06 17:15:14 +00:00
|
|
|
}
|
2016-11-22 13:55:09 +00:00
|
|
|
|
|
|
|
if (shouldReplace)
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
optimisedItems += optimisedChunk;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
copy(orig, iter, back_inserter(optimisedItems));
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
2016-11-11 10:41:50 +00:00
|
|
|
if (optimisedItems.size() < m_items.size())
|
|
|
|
{
|
|
|
|
m_items = move(optimisedItems);
|
|
|
|
count++;
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:10:55 +00:00
|
|
|
if (_settings.runConstantOptimiser)
|
2016-12-01 10:31:58 +00:00
|
|
|
ConstantOptimisationMethod::optimiseConstants(
|
2017-06-30 21:10:55 +00:00
|
|
|
_settings.isCreation,
|
|
|
|
_settings.isCreation ? 1 : _settings.expectedExecutionsPerDeployment,
|
2018-03-01 11:06:36 +00:00
|
|
|
_settings.evmVersion,
|
2019-01-28 16:29:10 +00:00
|
|
|
*this
|
2016-11-11 13:11:07 +00:00
|
|
|
);
|
2015-06-01 10:32:59 +00:00
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
return tagReplacements;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 10:02:18 +00:00
|
|
|
LinkerObject const& Assembly::assemble() const
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2020-07-15 09:32:34 +00:00
|
|
|
assertThrow(!m_invalid, AssemblyException, "Attempted to assemble invalid Assembly object.");
|
2019-11-20 21:34:39 +00:00
|
|
|
// Return the already assembled object, if present.
|
2015-09-10 10:02:18 +00:00
|
|
|
if (!m_assembledObject.bytecode.empty())
|
|
|
|
return m_assembledObject;
|
2019-11-20 21:34:39 +00:00
|
|
|
// Otherwise ensure the object is actually clear.
|
|
|
|
assertThrow(m_assembledObject.linkReferences.empty(), AssemblyException, "Unexpected link references.");
|
2015-09-10 10:02:18 +00:00
|
|
|
|
2020-03-10 13:30:04 +00:00
|
|
|
LinkerObject& ret = m_assembledObject;
|
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
size_t subTagSize = 1;
|
2020-04-02 15:27:35 +00:00
|
|
|
map<u256, pair<string, vector<size_t>>> immutableReferencesBySub;
|
2016-11-11 10:41:50 +00:00
|
|
|
for (auto const& sub: m_subs)
|
|
|
|
{
|
2020-03-10 13:30:04 +00:00
|
|
|
auto const& linkerObject = sub->assemble();
|
|
|
|
if (!linkerObject.immutableReferences.empty())
|
|
|
|
{
|
|
|
|
assertThrow(
|
|
|
|
immutableReferencesBySub.empty(),
|
|
|
|
AssemblyException,
|
|
|
|
"More than one sub-assembly references immutables."
|
|
|
|
);
|
|
|
|
immutableReferencesBySub = linkerObject.immutableReferences;
|
|
|
|
}
|
2017-07-27 22:28:49 +00:00
|
|
|
for (size_t tagPos: sub->m_tagPositionsInBytecode)
|
2020-06-02 13:10:14 +00:00
|
|
|
if (tagPos != numeric_limits<size_t>::max() && tagPos > subTagSize)
|
2017-07-27 22:28:49 +00:00
|
|
|
subTagSize = tagPos;
|
2016-11-11 10:41:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 13:30:04 +00:00
|
|
|
bool setsImmutables = false;
|
|
|
|
bool pushesImmutables = false;
|
|
|
|
|
|
|
|
for (auto const& i: m_items)
|
|
|
|
if (i.type() == AssignImmutable)
|
|
|
|
{
|
2020-04-02 15:27:35 +00:00
|
|
|
i.setImmutableOccurrences(immutableReferencesBySub[i.data()].second.size());
|
2020-03-10 13:30:04 +00:00
|
|
|
setsImmutables = true;
|
|
|
|
}
|
|
|
|
else if (i.type() == PushImmutable)
|
|
|
|
pushesImmutables = true;
|
|
|
|
if (setsImmutables || pushesImmutables)
|
|
|
|
assertThrow(
|
|
|
|
setsImmutables != pushesImmutables,
|
|
|
|
AssemblyException,
|
|
|
|
"Cannot push and assign immutables in the same assembly subroutine."
|
|
|
|
);
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2019-12-12 23:39:29 +00:00
|
|
|
unsigned bytesRequiredForCode = bytesRequired(static_cast<unsigned>(subTagSize));
|
2020-06-02 13:10:14 +00:00
|
|
|
m_tagPositionsInBytecode = vector<size_t>(m_usedTags, numeric_limits<size_t>::max());
|
2016-11-10 17:16:21 +00:00
|
|
|
map<size_t, pair<size_t, size_t>> tagRef;
|
2015-04-24 15:35:16 +00:00
|
|
|
multimap<h256, unsigned> dataRef;
|
2015-09-10 10:02:18 +00:00
|
|
|
multimap<size_t, size_t> subRef;
|
2015-04-24 15:35:16 +00:00
|
|
|
vector<unsigned> sizeRef; ///< Pointers to code locations where the size of the program is inserted
|
2019-12-11 16:31:36 +00:00
|
|
|
unsigned bytesPerTag = util::bytesRequired(bytesRequiredForCode);
|
2019-12-12 23:39:29 +00:00
|
|
|
uint8_t tagPush = static_cast<uint8_t>(pushInstruction(bytesPerTag));
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2019-12-12 23:39:29 +00:00
|
|
|
unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1 + static_cast<unsigned>(m_auxiliaryData.size());
|
2015-09-10 10:02:18 +00:00
|
|
|
for (auto const& sub: m_subs)
|
2019-12-12 23:39:29 +00:00
|
|
|
bytesRequiredIncludingData += static_cast<unsigned>(sub->assemble().bytecode.size());
|
2015-09-10 10:02:18 +00:00
|
|
|
|
2019-12-11 16:31:36 +00:00
|
|
|
unsigned bytesPerDataRef = util::bytesRequired(bytesRequiredIncludingData);
|
2019-12-12 23:39:29 +00:00
|
|
|
uint8_t dataRefPush = static_cast<uint8_t>(pushInstruction(bytesPerDataRef));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.reserve(bytesRequiredIncludingData);
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
for (AssemblyItem const& i: m_items)
|
2015-05-15 10:23:13 +00:00
|
|
|
{
|
|
|
|
// store position of the invalid jump destination
|
2020-06-02 13:10:14 +00:00
|
|
|
if (i.type() != Tag && m_tagPositionsInBytecode[0] == numeric_limits<size_t>::max())
|
2016-11-10 17:16:21 +00:00
|
|
|
m_tagPositionsInBytecode[0] = ret.bytecode.size();
|
2015-05-15 10:23:13 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
switch (i.type())
|
|
|
|
{
|
|
|
|
case Operation:
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(i.instruction()));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushString:
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::PUSH32));
|
2015-04-24 15:35:16 +00:00
|
|
|
unsigned ii = 0;
|
2019-12-12 23:39:29 +00:00
|
|
|
for (auto j: m_strings.at(h256(i.data())))
|
2015-04-24 15:35:16 +00:00
|
|
|
if (++ii > 32)
|
|
|
|
break;
|
|
|
|
else
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(uint8_t(j));
|
2015-04-24 15:35:16 +00:00
|
|
|
while (ii++ < 32)
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back(0);
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Push:
|
|
|
|
{
|
2019-12-12 23:39:29 +00:00
|
|
|
unsigned b = max<unsigned>(1, util::bytesRequired(i.data()));
|
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(b)));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.resize(ret.bytecode.size() + b);
|
|
|
|
bytesRef byr(&ret.bytecode.back() + 1 - b, b);
|
2015-04-24 15:35:16 +00:00
|
|
|
toBigEndian(i.data(), byr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PushTag:
|
|
|
|
{
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back(tagPush);
|
2016-11-10 17:16:21 +00:00
|
|
|
tagRef[ret.bytecode.size()] = i.splitForeignPushTag();
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.resize(ret.bytecode.size() + bytesPerTag);
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-10 10:02:18 +00:00
|
|
|
case PushData:
|
|
|
|
ret.bytecode.push_back(dataRefPush);
|
2019-12-12 23:39:29 +00:00
|
|
|
dataRef.insert(make_pair(h256(i.data()), ret.bytecode.size()));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef);
|
|
|
|
break;
|
|
|
|
case PushSub:
|
2020-06-02 13:10:14 +00:00
|
|
|
assertThrow(i.data() <= numeric_limits<size_t>::max(), AssemblyException, "");
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back(dataRefPush);
|
2020-06-02 13:10:14 +00:00
|
|
|
subRef.insert(make_pair(static_cast<size_t>(i.data()), ret.bytecode.size()));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef);
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushSubSize:
|
|
|
|
{
|
2020-06-02 13:10:14 +00:00
|
|
|
assertThrow(i.data() <= numeric_limits<size_t>::max(), AssemblyException, "");
|
2020-06-17 09:17:35 +00:00
|
|
|
auto s = subAssemblyById(static_cast<size_t>(i.data()))->assemble().bytecode.size();
|
2015-05-19 22:27:07 +00:00
|
|
|
i.setPushedValue(u256(s));
|
2019-12-12 23:39:29 +00:00
|
|
|
unsigned b = max<unsigned>(1, util::bytesRequired(s));
|
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(b)));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.resize(ret.bytecode.size() + b);
|
|
|
|
bytesRef byr(&ret.bytecode.back() + 1 - b, b);
|
2015-04-24 15:35:16 +00:00
|
|
|
toBigEndian(s, byr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PushProgramSize:
|
|
|
|
{
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back(dataRefPush);
|
2019-12-12 23:39:29 +00:00
|
|
|
sizeRef.push_back(static_cast<unsigned>(ret.bytecode.size()));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef);
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-10 10:02:18 +00:00
|
|
|
case PushLibraryAddress:
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::PUSH20));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.linkReferences[ret.bytecode.size()] = m_libraries.at(i.data());
|
|
|
|
ret.bytecode.resize(ret.bytecode.size() + 20);
|
|
|
|
break;
|
2020-03-10 13:30:04 +00:00
|
|
|
case PushImmutable:
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::PUSH32));
|
2020-04-02 15:27:35 +00:00
|
|
|
ret.immutableReferences[i.data()].first = m_immutables.at(i.data());
|
|
|
|
ret.immutableReferences[i.data()].second.emplace_back(ret.bytecode.size());
|
2020-03-10 13:30:04 +00:00
|
|
|
ret.bytecode.resize(ret.bytecode.size() + 32);
|
|
|
|
break;
|
2021-03-17 18:37:39 +00:00
|
|
|
case VerbatimBytecode:
|
|
|
|
ret.bytecode += i.verbatimData();
|
|
|
|
break;
|
2020-03-10 13:30:04 +00:00
|
|
|
case AssignImmutable:
|
2020-10-14 15:31:01 +00:00
|
|
|
{
|
|
|
|
auto const& offsets = immutableReferencesBySub[i.data()].second;
|
|
|
|
for (size_t i = 0; i < offsets.size(); ++i)
|
2020-03-10 13:30:04 +00:00
|
|
|
{
|
2020-10-14 15:31:01 +00:00
|
|
|
if (i != offsets.size() - 1)
|
|
|
|
{
|
|
|
|
ret.bytecode.push_back(uint8_t(Instruction::DUP2));
|
|
|
|
ret.bytecode.push_back(uint8_t(Instruction::DUP2));
|
|
|
|
}
|
2020-03-10 13:30:04 +00:00
|
|
|
// TODO: should we make use of the constant optimizer methods for pushing the offsets?
|
2020-10-14 15:31:01 +00:00
|
|
|
bytes offsetBytes = toCompactBigEndian(u256(offsets[i]));
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(pushInstruction(static_cast<unsigned>(offsetBytes.size()))));
|
2020-03-10 13:30:04 +00:00
|
|
|
ret.bytecode += offsetBytes;
|
2020-10-14 11:20:20 +00:00
|
|
|
ret.bytecode.push_back(uint8_t(Instruction::ADD));
|
2020-03-10 13:30:04 +00:00
|
|
|
ret.bytecode.push_back(uint8_t(Instruction::MSTORE));
|
|
|
|
}
|
2020-10-14 15:31:01 +00:00
|
|
|
if (offsets.empty())
|
|
|
|
{
|
|
|
|
ret.bytecode.push_back(uint8_t(Instruction::POP));
|
|
|
|
ret.bytecode.push_back(uint8_t(Instruction::POP));
|
|
|
|
}
|
2020-03-10 13:30:04 +00:00
|
|
|
immutableReferencesBySub.erase(i.data());
|
|
|
|
break;
|
2020-10-14 15:31:01 +00:00
|
|
|
}
|
2017-11-14 11:58:04 +00:00
|
|
|
case PushDeployTimeAddress:
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::PUSH20));
|
2017-11-14 11:58:04 +00:00
|
|
|
ret.bytecode.resize(ret.bytecode.size() + 20);
|
|
|
|
break;
|
2015-04-24 15:35:16 +00:00
|
|
|
case Tag:
|
2020-05-07 12:46:47 +00:00
|
|
|
{
|
2017-09-18 10:04:09 +00:00
|
|
|
assertThrow(i.data() != 0, AssemblyException, "Invalid tag position.");
|
2020-06-02 13:10:14 +00:00
|
|
|
assertThrow(i.splitForeignPushTag().first == numeric_limits<size_t>::max(), AssemblyException, "Foreign tag.");
|
2020-05-07 12:46:47 +00:00
|
|
|
size_t tagId = static_cast<size_t>(i.data());
|
2016-11-10 17:16:21 +00:00
|
|
|
assertThrow(ret.bytecode.size() < 0xffffffffL, AssemblyException, "Tag too large.");
|
2020-05-07 12:46:47 +00:00
|
|
|
assertThrow(m_tagPositionsInBytecode[tagId] == numeric_limits<size_t>::max(), AssemblyException, "Duplicate tag position.");
|
|
|
|
m_tagPositionsInBytecode[tagId] = ret.bytecode.size();
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::JUMPDEST));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
2020-05-07 12:46:47 +00:00
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
default:
|
2019-11-22 17:02:12 +00:00
|
|
|
assertThrow(false, InvalidOpcode, "Unexpected opcode while assembling.");
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
2015-05-15 10:23:13 +00:00
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2020-07-21 13:20:05 +00:00
|
|
|
if (!immutableReferencesBySub.empty())
|
|
|
|
throw
|
2020-10-31 02:40:41 +00:00
|
|
|
langutil::Error(
|
|
|
|
1284_error,
|
|
|
|
langutil::Error::Type::CodeGenerationError,
|
|
|
|
"Some immutables were read from but never assigned, possibly because of optimization."
|
|
|
|
);
|
2020-03-10 13:30:04 +00:00
|
|
|
|
2016-11-24 11:32:42 +00:00
|
|
|
if (!m_subs.empty() || !m_data.empty() || !m_auxiliaryData.empty())
|
2018-08-14 09:41:40 +00:00
|
|
|
// Append an INVALID here to help tests find miscompilation.
|
2019-12-12 23:39:29 +00:00
|
|
|
ret.bytecode.push_back(static_cast<uint8_t>(Instruction::INVALID));
|
2016-11-24 09:32:52 +00:00
|
|
|
|
2020-06-17 09:17:35 +00:00
|
|
|
for (auto const& [subIdPath, bytecodeOffset]: subRef)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2020-06-17 09:17:35 +00:00
|
|
|
bytesRef r(ret.bytecode.data() + bytecodeOffset, bytesPerDataRef);
|
|
|
|
toBigEndian(ret.bytecode.size(), r);
|
|
|
|
ret.append(subAssemblyById(subIdPath)->assemble());
|
2015-09-10 10:02:18 +00:00
|
|
|
}
|
2020-06-17 09:17:35 +00:00
|
|
|
|
2016-11-10 17:16:21 +00:00
|
|
|
for (auto const& i: tagRef)
|
|
|
|
{
|
|
|
|
size_t subId;
|
|
|
|
size_t tagId;
|
|
|
|
tie(subId, tagId) = i.second;
|
2020-06-02 13:10:14 +00:00
|
|
|
assertThrow(subId == numeric_limits<size_t>::max() || subId < m_subs.size(), AssemblyException, "Invalid sub id");
|
2020-06-17 09:17:35 +00:00
|
|
|
vector<size_t> const& tagPositions =
|
2020-06-02 13:10:14 +00:00
|
|
|
subId == numeric_limits<size_t>::max() ?
|
2016-11-10 17:16:21 +00:00
|
|
|
m_tagPositionsInBytecode :
|
2016-11-11 10:41:50 +00:00
|
|
|
m_subs[subId]->m_tagPositionsInBytecode;
|
2016-11-10 17:16:21 +00:00
|
|
|
assertThrow(tagId < tagPositions.size(), AssemblyException, "Reference to non-existing tag.");
|
|
|
|
size_t pos = tagPositions[tagId];
|
2020-06-02 13:10:14 +00:00
|
|
|
assertThrow(pos != numeric_limits<size_t>::max(), AssemblyException, "Reference to tag without position.");
|
2019-12-11 16:31:36 +00:00
|
|
|
assertThrow(util::bytesRequired(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space.");
|
2016-11-10 17:16:21 +00:00
|
|
|
bytesRef r(ret.bytecode.data() + i.first, bytesPerTag);
|
|
|
|
toBigEndian(pos, r);
|
|
|
|
}
|
2020-05-07 12:46:47 +00:00
|
|
|
for (auto const& [name, tagInfo]: m_namedTags)
|
|
|
|
{
|
|
|
|
size_t position = m_tagPositionsInBytecode.at(tagInfo.id);
|
|
|
|
ret.functionDebugData[name] = {
|
|
|
|
position == numeric_limits<size_t>::max() ? nullopt : optional<size_t>{position},
|
|
|
|
tagInfo.sourceID,
|
|
|
|
tagInfo.params,
|
|
|
|
tagInfo.returns
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-09-10 10:02:18 +00:00
|
|
|
for (auto const& dataItem: m_data)
|
|
|
|
{
|
|
|
|
auto references = dataRef.equal_range(dataItem.first);
|
|
|
|
if (references.first == references.second)
|
|
|
|
continue;
|
|
|
|
for (auto ref = references.first; ref != references.second; ++ref)
|
|
|
|
{
|
|
|
|
bytesRef r(ret.bytecode.data() + ref->second, bytesPerDataRef);
|
|
|
|
toBigEndian(ret.bytecode.size(), r);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode += dataItem.second;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
2016-11-24 09:32:52 +00:00
|
|
|
|
|
|
|
ret.bytecode += m_auxiliaryData;
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
for (unsigned pos: sizeRef)
|
|
|
|
{
|
2015-09-10 10:02:18 +00:00
|
|
|
bytesRef r(ret.bytecode.data() + pos, bytesPerDataRef);
|
|
|
|
toBigEndian(ret.bytecode.size(), r);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2020-06-17 09:17:35 +00:00
|
|
|
|
|
|
|
vector<size_t> Assembly::decodeSubPath(size_t _subObjectId) const
|
|
|
|
{
|
|
|
|
if (_subObjectId < m_subs.size())
|
|
|
|
return {_subObjectId};
|
|
|
|
|
|
|
|
auto subIdPathIt = find_if(
|
|
|
|
m_subPaths.begin(),
|
|
|
|
m_subPaths.end(),
|
|
|
|
[_subObjectId](auto const& subId) { return subId.second == _subObjectId; }
|
|
|
|
);
|
|
|
|
|
|
|
|
assertThrow(subIdPathIt != m_subPaths.end(), AssemblyException, "");
|
|
|
|
return subIdPathIt->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Assembly::encodeSubPath(vector<size_t> const& _subPath)
|
|
|
|
{
|
|
|
|
assertThrow(!_subPath.empty(), AssemblyException, "");
|
|
|
|
if (_subPath.size() == 1)
|
|
|
|
{
|
|
|
|
assertThrow(_subPath[0] < m_subs.size(), AssemblyException, "");
|
|
|
|
return _subPath[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_subPaths.find(_subPath) == m_subPaths.end())
|
|
|
|
{
|
|
|
|
size_t objectId = numeric_limits<size_t>::max() - m_subPaths.size();
|
|
|
|
assertThrow(objectId >= m_subs.size(), AssemblyException, "");
|
|
|
|
m_subPaths[_subPath] = objectId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_subPaths[_subPath];
|
|
|
|
}
|
|
|
|
|
|
|
|
Assembly const* Assembly::subAssemblyById(size_t _subId) const
|
|
|
|
{
|
|
|
|
vector<size_t> subIds = decodeSubPath(_subId);
|
|
|
|
Assembly const* currentAssembly = this;
|
|
|
|
for (size_t currentSubId: subIds)
|
|
|
|
{
|
|
|
|
currentAssembly = currentAssembly->m_subs.at(currentSubId).get();
|
|
|
|
assertThrow(currentAssembly, AssemblyException, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
assertThrow(currentAssembly != this, AssemblyException, "");
|
|
|
|
return currentAssembly;
|
|
|
|
}
|