2019-04-26 05:39:40 +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-04-26 05:39:40 +00:00
|
|
|
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2019-04-26 05:39:40 +00:00
|
|
|
#include <libyul/backends/wasm/WordSizeTransform.h>
|
|
|
|
#include <libyul/Utilities.h>
|
2019-06-18 16:14:49 +00:00
|
|
|
#include <libyul/Dialect.h>
|
|
|
|
#include <libyul/optimiser/NameDisplacer.h>
|
2019-04-26 05:39:40 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2019-04-26 05:39:40 +00:00
|
|
|
|
|
|
|
#include <array>
|
2019-06-25 09:27:58 +00:00
|
|
|
#include <map>
|
2019-11-19 15:42:49 +00:00
|
|
|
#include <variant>
|
2019-04-26 05:39:40 +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-04-26 05:39:40 +00:00
|
|
|
|
|
|
|
void WordSizeTransform::operator()(FunctionDefinition& _fd)
|
|
|
|
{
|
|
|
|
rewriteVarDeclList(_fd.parameters);
|
|
|
|
rewriteVarDeclList(_fd.returnVariables);
|
|
|
|
(*this)(_fd.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WordSizeTransform::operator()(FunctionCall& _fc)
|
|
|
|
{
|
2020-08-04 15:30:16 +00:00
|
|
|
vector<optional<LiteralKind>> const* literalArguments = nullptr;
|
2020-04-06 12:47:44 +00:00
|
|
|
|
2019-06-18 16:14:49 +00:00
|
|
|
if (BuiltinFunction const* fun = m_inputDialect.builtin(_fc.functionName.name))
|
2020-08-04 15:30:16 +00:00
|
|
|
if (!fun->literalArguments.empty())
|
|
|
|
literalArguments = &fun->literalArguments;
|
2020-04-06 12:47:44 +00:00
|
|
|
|
|
|
|
vector<Expression> newArgs;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < _fc.arguments.size(); i++)
|
2020-08-04 15:30:16 +00:00
|
|
|
if (!literalArguments || !(*literalArguments)[i].has_value())
|
2020-04-06 12:47:44 +00:00
|
|
|
newArgs += expandValueToVector(_fc.arguments[i]);
|
|
|
|
else
|
2020-02-04 17:12:22 +00:00
|
|
|
{
|
2020-04-06 12:47:44 +00:00
|
|
|
get<Literal>(_fc.arguments[i]).type = m_targetDialect.defaultType;
|
|
|
|
newArgs.emplace_back(std::move(_fc.arguments[i]));
|
2020-02-04 17:12:22 +00:00
|
|
|
}
|
2019-06-18 16:14:49 +00:00
|
|
|
|
2020-04-06 12:47:44 +00:00
|
|
|
_fc.arguments = std::move(newArgs);
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:47:51 +00:00
|
|
|
void WordSizeTransform::operator()(If& _if)
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
2019-05-09 14:47:51 +00:00
|
|
|
_if.condition = make_unique<Expression>(FunctionCall{
|
|
|
|
locationOf(*_if.condition),
|
2019-06-18 16:14:49 +00:00
|
|
|
Identifier{locationOf(*_if.condition), "or_bool"_yulstring},
|
2019-05-09 14:47:51 +00:00
|
|
|
expandValueToVector(*_if.condition)
|
|
|
|
});
|
|
|
|
(*this)(_if.body);
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WordSizeTransform::operator()(Switch&)
|
|
|
|
{
|
2019-06-25 09:27:58 +00:00
|
|
|
yulAssert(false, "Switch statement has to be handled inside the containing block.");
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 16:14:49 +00:00
|
|
|
void WordSizeTransform::operator()(ForLoop& _for)
|
|
|
|
{
|
|
|
|
(*this)(_for.pre);
|
|
|
|
_for.condition = make_unique<Expression>(FunctionCall{
|
|
|
|
locationOf(*_for.condition),
|
|
|
|
Identifier{locationOf(*_for.condition), "or_bool"_yulstring},
|
|
|
|
expandValueToVector(*_for.condition)
|
|
|
|
});
|
|
|
|
(*this)(_for.post);
|
|
|
|
(*this)(_for.body);
|
|
|
|
}
|
|
|
|
|
2019-04-26 05:39:40 +00:00
|
|
|
void WordSizeTransform::operator()(Block& _block)
|
|
|
|
{
|
|
|
|
iterateReplacing(
|
|
|
|
_block.statements,
|
2019-10-28 10:39:30 +00:00
|
|
|
[&](Statement& _s) -> std::optional<vector<Statement>>
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<VariableDeclaration>(_s))
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
VariableDeclaration& varDecl = std::get<VariableDeclaration>(_s);
|
2019-10-31 16:59:08 +00:00
|
|
|
|
2020-02-04 17:12:22 +00:00
|
|
|
if (!varDecl.value)
|
|
|
|
rewriteVarDeclList(varDecl.variables);
|
|
|
|
else if (holds_alternative<FunctionCall>(*varDecl.value))
|
|
|
|
{
|
|
|
|
visit(*varDecl.value);
|
|
|
|
|
|
|
|
// Special handling for datasize and dataoffset - they will only need one variable.
|
2019-11-19 15:42:49 +00:00
|
|
|
if (BuiltinFunction const* f = m_inputDialect.builtin(std::get<FunctionCall>(*varDecl.value).functionName.name))
|
2020-04-06 12:47:44 +00:00
|
|
|
if (f->name == "datasize"_yulstring || f->name == "dataoffset"_yulstring)
|
2019-10-31 16:59:08 +00:00
|
|
|
{
|
2020-08-04 15:30:16 +00:00
|
|
|
yulAssert(f->literalArguments.size() == 1, "");
|
|
|
|
yulAssert(f->literalArguments.at(0) == LiteralKind::String, "");
|
2019-10-31 16:59:08 +00:00
|
|
|
yulAssert(varDecl.variables.size() == 1, "");
|
|
|
|
auto newLhs = generateU64IdentifierNames(varDecl.variables[0].name);
|
|
|
|
vector<Statement> ret;
|
2020-06-02 13:34:28 +00:00
|
|
|
for (size_t i = 0; i < 3; i++)
|
2020-04-01 02:39:38 +00:00
|
|
|
ret.emplace_back(VariableDeclaration{
|
2019-10-31 16:59:08 +00:00
|
|
|
varDecl.location,
|
2020-02-20 15:17:09 +00:00
|
|
|
{TypedName{varDecl.location, newLhs[i], m_targetDialect.defaultType}},
|
|
|
|
make_unique<Expression>(Literal{
|
|
|
|
locationOf(*varDecl.value),
|
|
|
|
LiteralKind::Number,
|
|
|
|
"0"_yulstring,
|
|
|
|
m_targetDialect.defaultType
|
|
|
|
})
|
2019-10-31 16:59:08 +00:00
|
|
|
});
|
2020-04-01 02:39:38 +00:00
|
|
|
ret.emplace_back(VariableDeclaration{
|
2019-10-31 16:59:08 +00:00
|
|
|
varDecl.location,
|
2020-02-20 15:17:09 +00:00
|
|
|
{TypedName{varDecl.location, newLhs[3], m_targetDialect.defaultType}},
|
2019-10-31 16:59:08 +00:00
|
|
|
std::move(varDecl.value)
|
|
|
|
});
|
|
|
|
return {std::move(ret)};
|
|
|
|
}
|
|
|
|
|
2019-04-26 05:39:40 +00:00
|
|
|
rewriteVarDeclList(varDecl.variables);
|
2019-10-28 10:39:30 +00:00
|
|
|
return std::nullopt;
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
else if (
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<Identifier>(*varDecl.value) ||
|
|
|
|
holds_alternative<Literal>(*varDecl.value)
|
2019-04-26 05:39:40 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
yulAssert(varDecl.variables.size() == 1, "");
|
|
|
|
auto newRhs = expandValue(*varDecl.value);
|
|
|
|
auto newLhs = generateU64IdentifierNames(varDecl.variables[0].name);
|
|
|
|
vector<Statement> ret;
|
2020-06-02 13:34:28 +00:00
|
|
|
for (size_t i = 0; i < 4; i++)
|
2020-04-01 02:39:38 +00:00
|
|
|
ret.emplace_back(VariableDeclaration{
|
2019-04-26 05:39:40 +00:00
|
|
|
varDecl.location,
|
2020-02-20 15:17:09 +00:00
|
|
|
{TypedName{varDecl.location, newLhs[i], m_targetDialect.defaultType}},
|
2019-04-26 05:39:40 +00:00
|
|
|
std::move(newRhs[i])
|
|
|
|
}
|
|
|
|
);
|
2019-10-28 10:39:30 +00:00
|
|
|
return {std::move(ret)};
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
yulAssert(false, "");
|
|
|
|
}
|
2019-11-19 15:42:49 +00:00
|
|
|
else if (holds_alternative<Assignment>(_s))
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
Assignment& assignment = std::get<Assignment>(_s);
|
2019-04-26 05:39:40 +00:00
|
|
|
yulAssert(assignment.value, "");
|
2019-10-31 16:59:08 +00:00
|
|
|
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<FunctionCall>(*assignment.value))
|
2020-02-04 17:12:22 +00:00
|
|
|
{
|
|
|
|
visit(*assignment.value);
|
|
|
|
|
|
|
|
// Special handling for datasize and dataoffset - they will only need one variable.
|
2019-11-19 15:42:49 +00:00
|
|
|
if (BuiltinFunction const* f = m_inputDialect.builtin(std::get<FunctionCall>(*assignment.value).functionName.name))
|
2020-04-06 12:47:44 +00:00
|
|
|
if (f->name == "datasize"_yulstring || f->name == "dataoffset"_yulstring)
|
2019-10-31 16:59:08 +00:00
|
|
|
{
|
2020-08-04 15:30:16 +00:00
|
|
|
yulAssert(f->literalArguments.size() == 1, "");
|
|
|
|
yulAssert(f->literalArguments[0] == LiteralKind::String, "");
|
2019-10-31 16:59:08 +00:00
|
|
|
yulAssert(assignment.variableNames.size() == 1, "");
|
|
|
|
auto newLhs = generateU64IdentifierNames(assignment.variableNames[0].name);
|
|
|
|
vector<Statement> ret;
|
2020-06-02 13:34:28 +00:00
|
|
|
for (size_t i = 0; i < 3; i++)
|
2020-04-01 02:39:38 +00:00
|
|
|
ret.emplace_back(Assignment{
|
2019-10-31 16:59:08 +00:00
|
|
|
assignment.location,
|
|
|
|
{Identifier{assignment.location, newLhs[i]}},
|
2020-02-20 15:17:09 +00:00
|
|
|
make_unique<Expression>(Literal{
|
|
|
|
locationOf(*assignment.value),
|
|
|
|
LiteralKind::Number,
|
|
|
|
"0"_yulstring,
|
|
|
|
m_targetDialect.defaultType
|
|
|
|
})
|
2019-10-31 16:59:08 +00:00
|
|
|
});
|
2020-04-01 02:39:38 +00:00
|
|
|
ret.emplace_back(Assignment{
|
2019-10-31 16:59:08 +00:00
|
|
|
assignment.location,
|
|
|
|
{Identifier{assignment.location, newLhs[3]}},
|
|
|
|
std::move(assignment.value)
|
|
|
|
});
|
|
|
|
return {std::move(ret)};
|
|
|
|
}
|
|
|
|
|
2019-04-26 05:39:40 +00:00
|
|
|
rewriteIdentifierList(assignment.variableNames);
|
2019-10-28 10:39:30 +00:00
|
|
|
return std::nullopt;
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
else if (
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<Identifier>(*assignment.value) ||
|
|
|
|
holds_alternative<Literal>(*assignment.value)
|
2019-04-26 05:39:40 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
yulAssert(assignment.variableNames.size() == 1, "");
|
|
|
|
auto newRhs = expandValue(*assignment.value);
|
|
|
|
YulString lhsName = assignment.variableNames[0].name;
|
|
|
|
vector<Statement> ret;
|
2020-06-02 13:34:28 +00:00
|
|
|
for (size_t i = 0; i < 4; i++)
|
2020-04-01 02:39:38 +00:00
|
|
|
ret.emplace_back(Assignment{
|
2019-04-26 05:39:40 +00:00
|
|
|
assignment.location,
|
|
|
|
{Identifier{assignment.location, m_variableMapping.at(lhsName)[i]}},
|
|
|
|
std::move(newRhs[i])
|
|
|
|
}
|
|
|
|
);
|
2019-10-28 10:39:30 +00:00
|
|
|
return {std::move(ret)};
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
yulAssert(false, "");
|
|
|
|
}
|
2019-11-19 15:42:49 +00:00
|
|
|
else if (holds_alternative<Switch>(_s))
|
|
|
|
return handleSwitch(std::get<Switch>(_s));
|
2019-04-26 05:39:40 +00:00
|
|
|
else
|
|
|
|
visit(_s);
|
2019-10-28 10:39:30 +00:00
|
|
|
return std::nullopt;
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:40:22 +00:00
|
|
|
void WordSizeTransform::run(
|
|
|
|
Dialect const& _inputDialect,
|
2020-02-20 15:17:09 +00:00
|
|
|
Dialect const& _targetDialect,
|
2020-01-30 18:40:22 +00:00
|
|
|
Block& _ast,
|
|
|
|
NameDispenser& _nameDispenser
|
|
|
|
)
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
2019-06-18 16:14:49 +00:00
|
|
|
// Free the name `or_bool`.
|
|
|
|
NameDisplacer{_nameDispenser, {"or_bool"_yulstring}}(_ast);
|
2020-02-20 15:17:09 +00:00
|
|
|
WordSizeTransform{_inputDialect, _targetDialect, _nameDispenser}(_ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
WordSizeTransform::WordSizeTransform(
|
|
|
|
Dialect const& _inputDialect,
|
|
|
|
Dialect const& _targetDialect,
|
|
|
|
NameDispenser& _nameDispenser
|
|
|
|
):
|
|
|
|
m_inputDialect(_inputDialect),
|
|
|
|
m_targetDialect(_targetDialect),
|
|
|
|
m_nameDispenser(_nameDispenser)
|
|
|
|
{
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WordSizeTransform::rewriteVarDeclList(TypedNameList& _nameList)
|
|
|
|
{
|
|
|
|
iterateReplacing(
|
|
|
|
_nameList,
|
2019-10-28 10:39:30 +00:00
|
|
|
[&](TypedName const& _n) -> std::optional<TypedNameList>
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
|
|
|
TypedNameList ret;
|
2019-05-09 15:00:46 +00:00
|
|
|
for (auto newName: generateU64IdentifierNames(_n.name))
|
2020-02-20 15:17:09 +00:00
|
|
|
ret.emplace_back(TypedName{_n.location, newName, m_targetDialect.defaultType});
|
2019-04-26 05:39:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WordSizeTransform::rewriteIdentifierList(vector<Identifier>& _ids)
|
|
|
|
{
|
|
|
|
iterateReplacing(
|
|
|
|
_ids,
|
2019-10-28 10:39:30 +00:00
|
|
|
[&](Identifier const& _id) -> std::optional<vector<Identifier>>
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
|
|
|
vector<Identifier> ret;
|
|
|
|
for (auto newId: m_variableMapping.at(_id.name))
|
|
|
|
ret.push_back(Identifier{_id.location, newId});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-25 09:27:58 +00:00
|
|
|
vector<Statement> WordSizeTransform::handleSwitchInternal(
|
|
|
|
langutil::SourceLocation const& _location,
|
|
|
|
vector<YulString> const& _splitExpressions,
|
|
|
|
vector<Case> _cases,
|
|
|
|
YulString _runDefaultFlag,
|
|
|
|
size_t _depth
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (_depth == 4)
|
|
|
|
{
|
|
|
|
yulAssert(_cases.size() == 1, "");
|
|
|
|
return std::move(_cases.front().body.statements);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract current 64 bit segment and group by it.
|
|
|
|
map<u256, vector<Case>> cases;
|
|
|
|
for (Case& c: _cases)
|
|
|
|
{
|
|
|
|
yulAssert(c.value, "Default case still present.");
|
|
|
|
cases[
|
|
|
|
(valueOfLiteral(*c.value) >> (256 - 64 * (_depth + 1))) &
|
|
|
|
std::numeric_limits<uint64_t>::max()
|
|
|
|
].emplace_back(std::move(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
Switch ret{
|
|
|
|
_location,
|
|
|
|
make_unique<Expression>(Identifier{_location, _splitExpressions.at(_depth)}),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& c: cases)
|
|
|
|
{
|
2020-02-20 15:17:09 +00:00
|
|
|
Literal label{_location, LiteralKind::Number, YulString(c.first.str()), m_targetDialect.defaultType};
|
2019-06-25 09:27:58 +00:00
|
|
|
ret.cases.emplace_back(Case{
|
|
|
|
c.second.front().location,
|
|
|
|
make_unique<Literal>(std::move(label)),
|
|
|
|
Block{_location, handleSwitchInternal(
|
|
|
|
_location,
|
|
|
|
_splitExpressions,
|
|
|
|
std::move(c.second),
|
|
|
|
_runDefaultFlag,
|
|
|
|
_depth + 1
|
|
|
|
)}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!_runDefaultFlag.empty())
|
|
|
|
ret.cases.emplace_back(Case{
|
|
|
|
_location,
|
|
|
|
nullptr,
|
|
|
|
Block{_location, make_vector<Statement>(
|
|
|
|
Assignment{
|
|
|
|
_location,
|
|
|
|
{{_location, _runDefaultFlag}},
|
2020-02-20 15:17:09 +00:00
|
|
|
make_unique<Expression>(Literal{_location, LiteralKind::Boolean, "true"_yulstring, m_targetDialect.boolType})
|
2019-06-25 09:27:58 +00:00
|
|
|
}
|
|
|
|
)}
|
|
|
|
});
|
|
|
|
return make_vector<Statement>(std::move(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Statement> WordSizeTransform::handleSwitch(Switch& _switch)
|
|
|
|
{
|
|
|
|
for (auto& c: _switch.cases)
|
|
|
|
(*this)(c.body);
|
|
|
|
|
|
|
|
// Turns the switch into a quadruply-nested switch plus
|
|
|
|
// a flag that tells to execute the default case after all the switches.
|
|
|
|
vector<Statement> ret;
|
|
|
|
|
|
|
|
YulString runDefaultFlag;
|
|
|
|
Case defaultCase;
|
|
|
|
if (!_switch.cases.back().value)
|
|
|
|
{
|
|
|
|
runDefaultFlag = m_nameDispenser.newName("run_default"_yulstring);
|
|
|
|
defaultCase = std::move(_switch.cases.back());
|
|
|
|
_switch.cases.pop_back();
|
|
|
|
ret.emplace_back(VariableDeclaration{
|
|
|
|
_switch.location,
|
2020-02-20 15:17:09 +00:00
|
|
|
{TypedName{_switch.location, runDefaultFlag, m_targetDialect.boolType}},
|
2019-06-25 09:27:58 +00:00
|
|
|
{}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
vector<YulString> splitExpressions;
|
|
|
|
for (auto const& expr: expandValue(*_switch.expression))
|
2019-11-19 15:42:49 +00:00
|
|
|
splitExpressions.emplace_back(std::get<Identifier>(*expr).name);
|
2019-06-25 09:27:58 +00:00
|
|
|
|
|
|
|
ret += handleSwitchInternal(
|
|
|
|
_switch.location,
|
|
|
|
splitExpressions,
|
|
|
|
std::move(_switch.cases),
|
|
|
|
runDefaultFlag,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
if (!runDefaultFlag.empty())
|
|
|
|
ret.emplace_back(If{
|
|
|
|
_switch.location,
|
|
|
|
make_unique<Expression>(Identifier{_switch.location, runDefaultFlag}),
|
|
|
|
std::move(defaultCase.body)
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-26 05:39:40 +00:00
|
|
|
array<YulString, 4> WordSizeTransform::generateU64IdentifierNames(YulString const& _s)
|
|
|
|
{
|
|
|
|
yulAssert(m_variableMapping.find(_s) == m_variableMapping.end(), "");
|
2020-06-02 13:34:28 +00:00
|
|
|
for (size_t i = 0; i < 4; i++)
|
2019-05-09 15:00:46 +00:00
|
|
|
m_variableMapping[_s][i] = m_nameDispenser.newName(YulString{_s.str() + "_" + to_string(i)});
|
|
|
|
return m_variableMapping[_s];
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
array<unique_ptr<Expression>, 4> WordSizeTransform::expandValue(Expression const& _e)
|
|
|
|
{
|
|
|
|
array<unique_ptr<Expression>, 4> ret;
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<Identifier>(_e))
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
2020-06-02 13:34:28 +00:00
|
|
|
auto const& id = std::get<Identifier>(_e);
|
|
|
|
for (size_t i = 0; i < 4; i++)
|
2019-04-26 05:39:40 +00:00
|
|
|
ret[i] = make_unique<Expression>(Identifier{id.location, m_variableMapping.at(id.name)[i]});
|
|
|
|
}
|
2019-11-19 15:42:49 +00:00
|
|
|
else if (holds_alternative<Literal>(_e))
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
2020-06-02 13:34:28 +00:00
|
|
|
auto const& lit = std::get<Literal>(_e);
|
2019-04-26 05:39:40 +00:00
|
|
|
u256 val = valueOfLiteral(lit);
|
2020-06-02 13:34:28 +00:00
|
|
|
for (size_t exprIndex = 0; exprIndex < 4; ++exprIndex)
|
2019-04-26 05:39:40 +00:00
|
|
|
{
|
2020-06-02 13:34:28 +00:00
|
|
|
size_t exprIndexReverse = 3 - exprIndex;
|
2019-04-26 05:39:40 +00:00
|
|
|
u256 currentVal = val & std::numeric_limits<uint64_t>::max();
|
|
|
|
val >>= 64;
|
2020-06-02 13:34:28 +00:00
|
|
|
ret[exprIndexReverse] = make_unique<Expression>(
|
2019-04-26 05:39:40 +00:00
|
|
|
Literal{
|
|
|
|
lit.location,
|
|
|
|
LiteralKind::Number,
|
|
|
|
YulString(currentVal.str()),
|
2020-02-20 15:17:09 +00:00
|
|
|
m_targetDialect.defaultType
|
2019-04-26 05:39:40 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-06-25 09:27:58 +00:00
|
|
|
yulAssert(false, "Invalid expression to split.");
|
2019-04-26 05:39:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:47:51 +00:00
|
|
|
vector<Expression> WordSizeTransform::expandValueToVector(Expression const& _e)
|
|
|
|
{
|
|
|
|
vector<Expression> ret;
|
|
|
|
for (unique_ptr<Expression>& val: expandValue(_e))
|
|
|
|
ret.emplace_back(std::move(*val));
|
|
|
|
return ret;
|
|
|
|
}
|