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
|
|
|
*/
|
|
|
|
/** @file Assembly.cpp
|
|
|
|
* @author Gav Wood <i@gavwood.com>
|
|
|
|
* @date 2014
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "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>
|
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
|
|
|
|
|
|
|
#include <fstream>
|
2015-04-24 15:35:16 +00:00
|
|
|
#include <json/json.h>
|
2016-11-11 13:11:07 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::eth;
|
|
|
|
|
|
|
|
void Assembly::append(Assembly const& _a)
|
|
|
|
{
|
|
|
|
auto newDeposit = m_deposit + _a.deposit();
|
|
|
|
for (AssemblyItem i: _a.m_items)
|
|
|
|
{
|
|
|
|
if (i.type() == Tag || i.type() == PushTag)
|
|
|
|
i.setData(i.data() + m_usedTags);
|
|
|
|
else if (i.type() == PushSub || i.type() == PushSubSize)
|
2015-06-25 16:41:26 +00:00
|
|
|
i.setData(i.data() + m_subs.size());
|
2015-04-24 15:35:16 +00:00
|
|
|
append(i);
|
|
|
|
}
|
|
|
|
m_deposit = newDeposit;
|
|
|
|
m_usedTags += _a.m_usedTags;
|
|
|
|
for (auto const& i: _a.m_data)
|
|
|
|
m_data.insert(i);
|
|
|
|
for (auto const& i: _a.m_strings)
|
|
|
|
m_strings.insert(i);
|
2015-09-10 10:02:18 +00:00
|
|
|
m_subs += _a.m_subs;
|
|
|
|
for (auto const& lib: _a.m_libraries)
|
|
|
|
m_libraries.insert(lib);
|
2015-04-24 15:35:16 +00:00
|
|
|
|
|
|
|
assert(!_a.m_baseDeposit);
|
|
|
|
assert(!_a.m_totalDeposit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Assembly::append(Assembly const& _a, int _deposit)
|
|
|
|
{
|
|
|
|
if (_deposit > _a.m_deposit)
|
|
|
|
BOOST_THROW_EXCEPTION(InvalidDeposit());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
append(_a);
|
|
|
|
while (_deposit++ < _a.m_deposit)
|
|
|
|
append(Instruction::POP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
string Assembly::out() const
|
|
|
|
{
|
|
|
|
stringstream ret;
|
|
|
|
stream(ret);
|
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
unsigned ret = 1;
|
|
|
|
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);
|
|
|
|
if (dev::bytesRequired(ret) <= tagSize)
|
2015-04-24 15:35:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 15:11:02 +00:00
|
|
|
string Assembly::locationFromSources(StringMap const& _sourceCodes, SourceLocation const& _location) const
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
|
|
|
if (_location.isEmpty() || _sourceCodes.empty() || _location.start >= _location.end || _location.start < 0)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
auto it = _sourceCodes.find(*_location.sourceName);
|
|
|
|
if (it == _sourceCodes.end())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
string const& source = it->second;
|
|
|
|
if (size_t(_location.start) >= source.size())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
string cut = source.substr(_location.start, _location.end - _location.start);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ostream& Assembly::streamAsm(ostream& _out, string const& _prefix, StringMap const& _sourceCodes) const
|
|
|
|
{
|
|
|
|
_out << _prefix << ".code:" << endl;
|
|
|
|
for (AssemblyItem const& i: m_items)
|
|
|
|
{
|
|
|
|
_out << _prefix;
|
|
|
|
switch (i.type())
|
|
|
|
{
|
|
|
|
case Operation:
|
|
|
|
_out << " " << instructionInfo(i.instruction()).name << "\t" << i.getJumpTypeAsString();
|
|
|
|
break;
|
|
|
|
case Push:
|
2016-08-18 14:37:42 +00:00
|
|
|
_out << " PUSH" << dec << max<unsigned>(1, dev::bytesRequired(i.data())) << " 0x" << hex << i.data();
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushString:
|
|
|
|
_out << " PUSH \"" << m_strings.at((h256)i.data()) << "\"";
|
|
|
|
break;
|
|
|
|
case PushTag:
|
2015-05-15 10:23:13 +00:00
|
|
|
if (i.data() == 0)
|
|
|
|
_out << " PUSH [ErrorTag]";
|
|
|
|
else
|
2016-11-11 10:41:50 +00:00
|
|
|
{
|
|
|
|
size_t subId = i.splitForeignPushTag().first;
|
|
|
|
if (subId == size_t(-1))
|
|
|
|
_out << " PUSH [tag" << dec << i.splitForeignPushTag().second << "]";
|
|
|
|
else
|
|
|
|
_out << " PUSH [tag" << dec << subId << ":" << i.splitForeignPushTag().second << "]";
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushSub:
|
2016-11-11 10:41:50 +00:00
|
|
|
_out << " PUSH [$" << size_t(i.data()) << "]";
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushSubSize:
|
2016-11-11 10:41:50 +00:00
|
|
|
_out << " PUSH #[$" << size_t(i.data()) << "]";
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushProgramSize:
|
|
|
|
_out << " PUSHSIZE";
|
|
|
|
break;
|
2015-09-10 10:02:18 +00:00
|
|
|
case PushLibraryAddress:
|
|
|
|
_out << " PUSHLIB \"" << m_libraries.at(h256(i.data())) << "\"";
|
|
|
|
break;
|
2015-04-24 15:35:16 +00:00
|
|
|
case Tag:
|
2015-05-15 13:36:14 +00:00
|
|
|
_out << "tag" << dec << i.data() << ": " << endl << _prefix << " JUMPDEST";
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushData:
|
|
|
|
_out << " PUSH [" << hex << (unsigned)i.data() << "]";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BOOST_THROW_EXCEPTION(InvalidOpcode());
|
|
|
|
}
|
2015-09-08 15:11:02 +00:00
|
|
|
_out << "\t\t" << locationFromSources(_sourceCodes, i.location()) << endl;
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_data.empty() || !m_subs.empty())
|
|
|
|
{
|
|
|
|
_out << _prefix << ".data:" << endl;
|
|
|
|
for (auto const& i: m_data)
|
|
|
|
if (u256(i.first) >= m_subs.size())
|
2015-09-10 10:02:18 +00:00
|
|
|
_out << _prefix << " " << hex << (unsigned)(u256)i.first << ": " << dev::toHex(i.second) << endl;
|
2015-04-24 15:35:16 +00:00
|
|
|
for (size_t i = 0; i < m_subs.size(); ++i)
|
|
|
|
{
|
|
|
|
_out << _prefix << " " << hex << i << ": " << endl;
|
2016-11-11 10:41:50 +00:00
|
|
|
m_subs[i]->stream(_out, _prefix + " ", _sourceCodes);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return _out;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value Assembly::createJsonValue(string _name, int _begin, int _end, string _value, string _jumpType) const
|
|
|
|
{
|
|
|
|
Json::Value value;
|
|
|
|
value["name"] = _name;
|
|
|
|
value["begin"] = _begin;
|
|
|
|
value["end"] = _end;
|
|
|
|
if (!_value.empty())
|
|
|
|
value["value"] = _value;
|
|
|
|
if (!_jumpType.empty())
|
|
|
|
value["jumpType"] = _jumpType;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
string toStringInHex(u256 _value)
|
|
|
|
{
|
|
|
|
std::stringstream hexStr;
|
|
|
|
hexStr << hex << _value;
|
|
|
|
return hexStr.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value Assembly::streamAsmJson(ostream& _out, StringMap const& _sourceCodes) const
|
|
|
|
{
|
|
|
|
Json::Value root;
|
|
|
|
|
|
|
|
Json::Value collection(Json::arrayValue);
|
|
|
|
for (AssemblyItem const& i: m_items)
|
|
|
|
{
|
|
|
|
switch (i.type())
|
|
|
|
{
|
|
|
|
case Operation:
|
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue(instructionInfo(i.instruction()).name, i.location().start, i.location().end, i.getJumpTypeAsString()));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case Push:
|
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("PUSH", i.location().start, i.location().end, toStringInHex(i.data()), i.getJumpTypeAsString()));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushString:
|
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("PUSH tag", 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(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("PUSH [ErrorTag]", i.location().start, i.location().end, ""));
|
2015-05-20 11:15:01 +00:00
|
|
|
else
|
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("PUSH [tag]", i.location().start, i.location().end, string(i.data())));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushSub:
|
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("PUSH [$]", i.location().start, i.location().end, dev::toString(h256(i.data()))));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushSubSize:
|
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("PUSH #[$]", i.location().start, i.location().end, dev::toString(h256(i.data()))));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushProgramSize:
|
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("PUSHSIZE", 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(
|
|
|
|
createJsonValue("PUSHLIB", i.location().start, i.location().end, m_libraries.at(h256(i.data())))
|
|
|
|
);
|
|
|
|
break;
|
2015-04-24 15:35:16 +00:00
|
|
|
case Tag:
|
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("tag", i.location().start, i.location().end, string(i.data())));
|
2015-04-24 15:35:16 +00:00
|
|
|
collection.append(
|
2015-09-08 15:11:02 +00:00
|
|
|
createJsonValue("JUMPDEST", i.location().start, i.location().end));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushData:
|
2015-09-08 15:11:02 +00:00
|
|
|
collection.append(createJsonValue("PUSH data", i.location().start, i.location().end, toStringInHex(i.data())));
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BOOST_THROW_EXCEPTION(InvalidOpcode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root[".code"] = collection;
|
|
|
|
|
|
|
|
if (!m_data.empty() || !m_subs.empty())
|
|
|
|
{
|
|
|
|
Json::Value data;
|
|
|
|
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;
|
2016-11-11 10:41:50 +00:00
|
|
|
data[hexStr.str()] = m_subs[i]->stream(_out, "", _sourceCodes, true);
|
2015-04-24 15:35:16 +00:00
|
|
|
}
|
|
|
|
root[".data"] = data;
|
|
|
|
_out << root;
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
Json::Value Assembly::stream(ostream& _out, string const& _prefix, StringMap const& _sourceCodes, bool _inJsonFormat) const
|
|
|
|
{
|
|
|
|
if (_inJsonFormat)
|
|
|
|
return streamAsmJson(_out, _sourceCodes);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
streamAsm(_out, _prefix, _sourceCodes);
|
|
|
|
return Json::Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AssemblyItem const& Assembly::append(AssemblyItem const& _i)
|
|
|
|
{
|
|
|
|
m_deposit += _i.deposit();
|
|
|
|
m_items.push_back(_i);
|
2015-09-08 15:11:02 +00:00
|
|
|
if (m_items.back().location().isEmpty() && !m_currentSourceLocation.isEmpty())
|
2015-04-24 15:35:16 +00:00
|
|
|
m_items.back().setLocation(m_currentSourceLocation);
|
|
|
|
return back();
|
|
|
|
}
|
|
|
|
|
2015-09-10 10:02:18 +00:00
|
|
|
AssemblyItem Assembly::newPushLibraryAddress(string const& _identifier)
|
|
|
|
{
|
2016-10-05 10:30:28 +00:00
|
|
|
h256 h(dev::keccak256(_identifier));
|
2015-09-10 10:02:18 +00:00
|
|
|
m_libraries[h] = _identifier;
|
|
|
|
return AssemblyItem(PushLibraryAddress, h);
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
void Assembly::injectStart(AssemblyItem const& _i)
|
|
|
|
{
|
|
|
|
m_items.insert(m_items.begin(), _i);
|
|
|
|
}
|
|
|
|
|
2015-06-01 10:32:59 +00:00
|
|
|
Assembly& Assembly::optimise(bool _enable, bool _isCreation, size_t _runs)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2016-11-11 13:11:07 +00:00
|
|
|
optimiseInternal(_enable, _isCreation, _runs);
|
2016-11-10 17:16:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2016-11-11 13:11:07 +00:00
|
|
|
map<u256, u256> Assembly::optimiseInternal(bool _enable, bool _isCreation, size_t _runs)
|
2016-11-10 17:16:21 +00:00
|
|
|
{
|
|
|
|
for (size_t subId = 0; subId < m_subs.size(); ++subId)
|
|
|
|
{
|
2016-11-11 13:11:07 +00:00
|
|
|
map<u256, u256> subTagReplacements = m_subs[subId]->optimiseInternal(_enable, false, _runs);
|
2016-11-10 17:16:21 +00:00
|
|
|
BlockDeduplicator::applyTagReplacement(m_items, subTagReplacements, subId);
|
|
|
|
}
|
|
|
|
|
|
|
|
map<u256, u256> tagReplacements;
|
2015-04-24 15:35:16 +00:00
|
|
|
unsigned total = 0;
|
|
|
|
for (unsigned count = 1; count > 0; total += count)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
|
2016-11-11 13:11:07 +00:00
|
|
|
PeepholeOptimiser peepOpt(m_items);
|
|
|
|
if (peepOpt.optimise())
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if (!_enable)
|
|
|
|
continue;
|
|
|
|
|
2015-05-28 12:43:46 +00:00
|
|
|
// This only modifies PushTags, we have to run again to actually remove code.
|
|
|
|
BlockDeduplicator dedup(m_items);
|
|
|
|
if (dedup.deduplicate())
|
2016-11-10 17:16:21 +00:00
|
|
|
{
|
|
|
|
tagReplacements.insert(dedup.replacedTags().begin(), dedup.replacedTags().end());
|
2015-05-28 12:43:46 +00:00
|
|
|
count++;
|
2016-11-10 17:16:21 +00:00
|
|
|
}
|
2015-05-28 12:43:46 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
CommonSubexpressionEliminator eliminator(emptyState);
|
2016-11-22 13:55:09 +00:00
|
|
|
auto orig = iter;
|
|
|
|
iter = eliminator.feedItems(iter, m_items.end());
|
|
|
|
bool shouldReplace = false;
|
|
|
|
AssemblyItems optimisedChunk;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
optimisedChunk = eliminator.getOptimizedItems();
|
|
|
|
shouldReplace = (optimisedChunk.size() < size_t(iter - orig));
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:11:07 +00:00
|
|
|
if (_enable)
|
|
|
|
total += ConstantOptimisationMethod::optimiseConstants(
|
|
|
|
_isCreation,
|
|
|
|
_isCreation ? 1 : _runs,
|
|
|
|
*this,
|
|
|
|
m_items
|
|
|
|
);
|
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
|
|
|
{
|
2015-09-10 10:02:18 +00:00
|
|
|
if (!m_assembledObject.bytecode.empty())
|
|
|
|
return m_assembledObject;
|
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
size_t subTagSize = 1;
|
|
|
|
for (auto const& sub: m_subs)
|
|
|
|
{
|
|
|
|
sub->assemble();
|
|
|
|
if (!sub->m_tagPositionsInBytecode.empty())
|
|
|
|
subTagSize = max(subTagSize, *max_element(sub->m_tagPositionsInBytecode.begin(), sub->m_tagPositionsInBytecode.end()));
|
|
|
|
}
|
|
|
|
|
2015-09-10 10:02:18 +00:00
|
|
|
LinkerObject& ret = m_assembledObject;
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
size_t bytesRequiredForCode = bytesRequired(subTagSize);
|
2016-11-10 17:16:21 +00:00
|
|
|
m_tagPositionsInBytecode = vector<size_t>(m_usedTags, -1);
|
|
|
|
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
|
2016-11-11 10:41:50 +00:00
|
|
|
unsigned bytesPerTag = dev::bytesRequired(bytesRequiredForCode);
|
2015-04-24 15:35:16 +00:00
|
|
|
byte tagPush = (byte)Instruction::PUSH1 - 1 + bytesPerTag;
|
|
|
|
|
2016-11-11 10:41:50 +00:00
|
|
|
unsigned bytesRequiredIncludingData = bytesRequiredForCode + 1;
|
2015-09-10 10:02:18 +00:00
|
|
|
for (auto const& sub: m_subs)
|
2016-11-11 10:41:50 +00:00
|
|
|
bytesRequiredIncludingData += sub->assemble().bytecode.size();
|
2015-09-10 10:02:18 +00:00
|
|
|
|
2015-04-24 15:35:16 +00:00
|
|
|
unsigned bytesPerDataRef = dev::bytesRequired(bytesRequiredIncludingData);
|
|
|
|
byte dataRefPush = (byte)Instruction::PUSH1 - 1 + 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
|
2016-11-10 17:16:21 +00:00
|
|
|
if (i.type() != Tag && m_tagPositionsInBytecode[0] == size_t(-1))
|
|
|
|
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:
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back((byte)i.data());
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushString:
|
|
|
|
{
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back((byte)Instruction::PUSH32);
|
2015-04-24 15:35:16 +00:00
|
|
|
unsigned ii = 0;
|
|
|
|
for (auto j: m_strings.at((h256)i.data()))
|
|
|
|
if (++ii > 32)
|
|
|
|
break;
|
|
|
|
else
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back((byte)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:
|
|
|
|
{
|
|
|
|
byte b = max<unsigned>(1, dev::bytesRequired(i.data()));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back((byte)Instruction::PUSH1 - 1 + b);
|
|
|
|
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);
|
|
|
|
dataRef.insert(make_pair((h256)i.data(), ret.bytecode.size()));
|
|
|
|
ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef);
|
|
|
|
break;
|
|
|
|
case PushSub:
|
|
|
|
ret.bytecode.push_back(dataRefPush);
|
|
|
|
subRef.insert(make_pair(size_t(i.data()), ret.bytecode.size()));
|
|
|
|
ret.bytecode.resize(ret.bytecode.size() + bytesPerDataRef);
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
case PushSubSize:
|
|
|
|
{
|
2016-11-11 10:41:50 +00:00
|
|
|
auto s = m_subs.at(size_t(i.data()))->assemble().bytecode.size();
|
2015-05-19 22:27:07 +00:00
|
|
|
i.setPushedValue(u256(s));
|
2015-04-24 15:35:16 +00:00
|
|
|
byte b = max<unsigned>(1, dev::bytesRequired(s));
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back((byte)Instruction::PUSH1 - 1 + b);
|
|
|
|
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);
|
|
|
|
sizeRef.push_back(ret.bytecode.size());
|
|
|
|
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:
|
|
|
|
ret.bytecode.push_back(byte(Instruction::PUSH20));
|
|
|
|
ret.linkReferences[ret.bytecode.size()] = m_libraries.at(i.data());
|
|
|
|
ret.bytecode.resize(ret.bytecode.size() + 20);
|
|
|
|
break;
|
2015-04-24 15:35:16 +00:00
|
|
|
case Tag:
|
2015-05-15 10:23:13 +00:00
|
|
|
assertThrow(i.data() != 0, AssemblyException, "");
|
2016-11-10 17:16:21 +00:00
|
|
|
assertThrow(i.splitForeignPushTag().first == size_t(-1), AssemblyException, "Foreign tag.");
|
|
|
|
assertThrow(ret.bytecode.size() < 0xffffffffL, AssemblyException, "Tag too large.");
|
|
|
|
m_tagPositionsInBytecode[size_t(i.data())] = ret.bytecode.size();
|
2015-09-10 10:02:18 +00:00
|
|
|
ret.bytecode.push_back((byte)Instruction::JUMPDEST);
|
2015-04-24 15:35:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BOOST_THROW_EXCEPTION(InvalidOpcode());
|
|
|
|
}
|
2015-05-15 10:23:13 +00:00
|
|
|
}
|
2015-04-24 15:35:16 +00:00
|
|
|
|
2015-09-10 10:02:18 +00:00
|
|
|
if (!dataRef.empty() && !subRef.empty())
|
|
|
|
ret.bytecode.push_back(0);
|
|
|
|
for (size_t i = 0; i < m_subs.size(); ++i)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2015-09-10 10:02:18 +00:00
|
|
|
auto references = subRef.equal_range(i);
|
|
|
|
if (references.first == references.second)
|
|
|
|
continue;
|
|
|
|
for (auto ref = references.first; ref != references.second; ++ref)
|
2015-04-24 15:35:16 +00:00
|
|
|
{
|
2015-09-10 10:02:18 +00:00
|
|
|
bytesRef r(ret.bytecode.data() + ref->second, bytesPerDataRef);
|
|
|
|
toBigEndian(ret.bytecode.size(), r);
|
|
|
|
}
|
2016-11-11 10:41:50 +00:00
|
|
|
ret.append(m_subs[i]->assemble());
|
2015-09-10 10:02:18 +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;
|
|
|
|
assertThrow(subId == size_t(-1) || subId < m_subs.size(), AssemblyException, "Invalid sub id");
|
|
|
|
std::vector<size_t> const& tagPositions =
|
|
|
|
subId == size_t(-1) ?
|
|
|
|
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];
|
|
|
|
assertThrow(pos != size_t(-1), AssemblyException, "Reference to tag without position.");
|
|
|
|
assertThrow(dev::bytesRequired(pos) <= bytesPerTag, AssemblyException, "Tag too large for reserved space.");
|
|
|
|
bytesRef r(ret.bytecode.data() + i.first, bytesPerTag);
|
|
|
|
toBigEndian(pos, r);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|