2019-04-18 12:39:48 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
2019-12-11 13:27:24 +00:00
|
|
|
* Common code generator for translating Yul / inline assembly to Wasm.
|
2019-04-18 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
#include <libyul/backends/wasm/WasmCodeTransform.h>
|
2019-04-18 12:39:48 +00:00
|
|
|
|
2020-05-28 19:44:52 +00:00
|
|
|
#include <libyul/backends/wasm/WasmDialect.h>
|
|
|
|
|
2019-04-23 08:45:16 +00:00
|
|
|
#include <libyul/optimiser/NameCollector.h>
|
2019-04-18 14:37:59 +00:00
|
|
|
|
2019-04-18 12:39:48 +00:00
|
|
|
#include <libyul/AsmData.h>
|
|
|
|
#include <libyul/Dialect.h>
|
|
|
|
#include <libyul/Utilities.h>
|
|
|
|
#include <libyul/Exceptions.h>
|
|
|
|
|
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
2020-05-12 15:17:23 +00:00
|
|
|
#include <optional>
|
|
|
|
|
2019-04-18 12:39:48 +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-18 12:39:48 +00:00
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Module WasmCodeTransform::run(Dialect const& _dialect, yul::Block const& _ast)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-10-31 16:27:52 +00:00
|
|
|
wasm::Module module;
|
|
|
|
|
2020-05-28 19:44:52 +00:00
|
|
|
TypeInfo typeInfo(_dialect, _ast);
|
|
|
|
WasmCodeTransform transform(_dialect, _ast, typeInfo);
|
2019-04-18 12:39:48 +00:00
|
|
|
|
|
|
|
for (auto const& statement: _ast.statements)
|
|
|
|
{
|
2019-04-23 19:32:58 +00:00
|
|
|
yulAssert(
|
2019-11-19 15:42:49 +00:00
|
|
|
holds_alternative<yul::FunctionDefinition>(statement),
|
2019-04-23 19:32:58 +00:00
|
|
|
"Expected only function definitions at the highest level."
|
|
|
|
);
|
2019-11-19 15:42:49 +00:00
|
|
|
if (holds_alternative<yul::FunctionDefinition>(statement))
|
|
|
|
module.functions.emplace_back(transform.translateFunction(std::get<yul::FunctionDefinition>(statement)));
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 13:19:50 +00:00
|
|
|
for (auto& imp: transform.m_functionsToImport)
|
2019-10-31 16:27:52 +00:00
|
|
|
module.imports.emplace_back(std::move(imp.second));
|
|
|
|
module.globals = transform.m_globalVariables;
|
|
|
|
|
|
|
|
return module;
|
2019-04-23 08:45:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::generateMultiAssignment(
|
2019-04-23 08:45:16 +00:00
|
|
|
vector<string> _variableNames,
|
|
|
|
unique_ptr<wasm::Expression> _firstValue
|
|
|
|
)
|
|
|
|
{
|
|
|
|
yulAssert(!_variableNames.empty(), "");
|
2019-04-23 19:32:58 +00:00
|
|
|
wasm::LocalAssignment assignment{move(_variableNames.front()), std::move(_firstValue)};
|
2019-04-23 08:45:16 +00:00
|
|
|
|
|
|
|
if (_variableNames.size() == 1)
|
2019-05-14 14:44:28 +00:00
|
|
|
return { std::move(assignment) };
|
2019-04-23 08:45:16 +00:00
|
|
|
|
2020-05-28 19:44:52 +00:00
|
|
|
vector<wasm::Type> typesForGlobals;
|
|
|
|
for (size_t i = 1; i < _variableNames.size(); ++i)
|
|
|
|
typesForGlobals.push_back(translatedType(m_typeInfo.typeOfVariable(YulString(_variableNames[i]))));
|
|
|
|
vector<size_t> allocatedIndices = allocateGlobals(typesForGlobals);
|
|
|
|
yulAssert(allocatedIndices.size() == _variableNames.size() - 1, "");
|
2019-05-09 15:56:25 +00:00
|
|
|
|
2019-04-23 08:45:16 +00:00
|
|
|
wasm::Block block;
|
2019-04-23 19:32:58 +00:00
|
|
|
block.statements.emplace_back(move(assignment));
|
2019-04-23 08:45:16 +00:00
|
|
|
for (size_t i = 1; i < _variableNames.size(); ++i)
|
|
|
|
block.statements.emplace_back(wasm::LocalAssignment{
|
2019-04-23 19:32:58 +00:00
|
|
|
move(_variableNames.at(i)),
|
2020-05-28 19:44:52 +00:00
|
|
|
make_unique<wasm::Expression>(wasm::GlobalVariable{m_globalVariables.at(allocatedIndices[i - 1]).variableName})
|
2019-04-23 08:45:16 +00:00
|
|
|
});
|
2019-05-14 14:44:28 +00:00
|
|
|
return { std::move(block) };
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(VariableDeclaration const& _varDecl)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-04-23 08:45:16 +00:00
|
|
|
vector<string> variableNames;
|
2019-04-18 12:39:48 +00:00
|
|
|
for (auto const& var: _varDecl.variables)
|
|
|
|
{
|
2019-04-23 08:45:16 +00:00
|
|
|
variableNames.emplace_back(var.name.str());
|
2020-05-28 19:44:52 +00:00
|
|
|
m_localVariables.emplace_back(wasm::VariableDeclaration{variableNames.back(), translatedType(var.type)});
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
2019-04-23 08:45:16 +00:00
|
|
|
|
|
|
|
if (_varDecl.value)
|
2019-04-23 19:32:58 +00:00
|
|
|
return generateMultiAssignment(move(variableNames), visit(*_varDecl.value));
|
2019-04-18 12:39:48 +00:00
|
|
|
else
|
|
|
|
return wasm::BuiltinCall{"nop", {}};
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(Assignment const& _assignment)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-04-23 08:45:16 +00:00
|
|
|
vector<string> variableNames;
|
|
|
|
for (auto const& var: _assignment.variableNames)
|
|
|
|
variableNames.emplace_back(var.name.str());
|
2019-04-23 19:32:58 +00:00
|
|
|
return generateMultiAssignment(move(variableNames), visit(*_assignment.value));
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(ExpressionStatement const& _statement)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
return visitReturnByValue(_statement.expression);
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(FunctionCall const& _call)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-06-24 14:51:59 +00:00
|
|
|
if (BuiltinFunction const* builtin = m_dialect.builtin(_call.functionName.name))
|
|
|
|
{
|
2019-07-09 13:19:50 +00:00
|
|
|
if (_call.functionName.name.str().substr(0, 4) == "eth.")
|
|
|
|
{
|
|
|
|
yulAssert(builtin->returns.size() <= 1, "");
|
|
|
|
// Imported function, use regular call, but mark for import.
|
|
|
|
if (!m_functionsToImport.count(builtin->name))
|
|
|
|
{
|
|
|
|
wasm::FunctionImport imp{
|
|
|
|
"ethereum",
|
|
|
|
builtin->name.str().substr(4),
|
|
|
|
builtin->name.str(),
|
|
|
|
{},
|
2020-06-10 22:54:12 +00:00
|
|
|
builtin->returns.empty() ? nullopt : make_optional<wasm::Type>(translatedType(builtin->returns.front()))
|
2019-07-09 13:19:50 +00:00
|
|
|
};
|
|
|
|
for (auto const& param: builtin->parameters)
|
2020-06-10 22:54:12 +00:00
|
|
|
imp.paramTypes.emplace_back(translatedType(param));
|
2019-07-09 13:19:50 +00:00
|
|
|
m_functionsToImport[builtin->name] = std::move(imp);
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 12:47:44 +00:00
|
|
|
else if (builtin->literalArguments && contains(builtin->literalArguments.value(), true))
|
2019-06-24 14:51:59 +00:00
|
|
|
{
|
|
|
|
vector<wasm::Expression> literals;
|
2020-04-06 12:47:44 +00:00
|
|
|
for (size_t i = 0; i < _call.arguments.size(); i++)
|
|
|
|
if (builtin->literalArguments.value()[i])
|
|
|
|
literals.emplace_back(wasm::StringLiteral{std::get<Literal>(_call.arguments[i]).value.str()});
|
|
|
|
else
|
|
|
|
literals.emplace_back(visitReturnByValue(_call.arguments[i]));
|
|
|
|
|
2019-06-24 14:51:59 +00:00
|
|
|
return wasm::BuiltinCall{_call.functionName.name.str(), std::move(literals)};
|
|
|
|
}
|
|
|
|
else
|
2020-02-04 22:28:44 +00:00
|
|
|
return wasm::BuiltinCall{
|
2019-10-31 16:31:35 +00:00
|
|
|
_call.functionName.name.str(),
|
2020-02-04 22:28:44 +00:00
|
|
|
visit(_call.arguments)
|
2019-10-31 16:31:35 +00:00
|
|
|
};
|
2019-06-24 14:51:59 +00:00
|
|
|
}
|
2019-07-09 13:19:50 +00:00
|
|
|
|
|
|
|
// If this function returns multiple values, then the first one will
|
|
|
|
// be returned in the expression itself and the others in global variables.
|
|
|
|
// The values have to be used right away in an assignment or variable declaration,
|
|
|
|
// so it is handled there.
|
2019-10-31 16:31:35 +00:00
|
|
|
|
2020-02-04 22:28:44 +00:00
|
|
|
return wasm::FunctionCall{_call.functionName.name.str(), visit(_call.arguments)};
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(Identifier const& _identifier)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-04-23 08:45:16 +00:00
|
|
|
return wasm::LocalVariable{_identifier.name.str()};
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(Literal const& _literal)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2020-05-28 19:44:52 +00:00
|
|
|
return makeLiteral(translatedType(_literal.type), valueOfLiteral(_literal));
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(If const& _if)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2020-05-28 19:44:52 +00:00
|
|
|
yul::Type conditionType = m_typeInfo.typeOf(*_if.condition);
|
2020-06-15 17:30:06 +00:00
|
|
|
|
|
|
|
wasm::Expression condition;
|
|
|
|
if (conditionType == "i32"_yulstring)
|
|
|
|
condition = visitReturnByValue(*_if.condition);
|
|
|
|
else if (conditionType == "i64"_yulstring)
|
|
|
|
{
|
|
|
|
vector<wasm::Expression> args;
|
|
|
|
args.emplace_back(visitReturnByValue(*_if.condition));
|
|
|
|
args.emplace_back(makeLiteral(translatedType("i64"_yulstring), 0));
|
|
|
|
|
|
|
|
// NOTE: `if` in wasm requires an i32 argument
|
|
|
|
condition = wasm::BuiltinCall{"i64.ne", std::move(args)};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
yulAssert(false, "Invalid condition type");
|
|
|
|
|
|
|
|
return wasm::If{make_unique<wasm::Expression>(move(condition)), visit(_if.body.statements), {}};
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(Switch const& _switch)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2020-05-28 19:44:52 +00:00
|
|
|
yul::Type expressionType = m_typeInfo.typeOf(*_switch.expression);
|
|
|
|
YulString eq_instruction = YulString(expressionType.str() + ".eq");
|
|
|
|
yulAssert(WasmDialect::instance().builtin(eq_instruction), "");
|
|
|
|
|
2019-04-23 19:32:58 +00:00
|
|
|
wasm::Block block;
|
|
|
|
string condition = m_nameDispenser.newName("condition"_yulstring).str();
|
2020-05-28 19:44:52 +00:00
|
|
|
m_localVariables.emplace_back(wasm::VariableDeclaration{condition, translatedType(expressionType)});
|
2019-04-23 19:32:58 +00:00
|
|
|
block.statements.emplace_back(wasm::LocalAssignment{condition, visit(*_switch.expression)});
|
|
|
|
|
|
|
|
vector<wasm::Expression>* currentBlock = &block.statements;
|
|
|
|
for (size_t i = 0; i < _switch.cases.size(); ++i)
|
|
|
|
{
|
|
|
|
Case const& c = _switch.cases.at(i);
|
|
|
|
if (c.value)
|
|
|
|
{
|
2020-05-28 19:44:52 +00:00
|
|
|
wasm::BuiltinCall comparison{eq_instruction.str(), make_vector<wasm::Expression>(
|
2019-05-17 12:13:43 +00:00
|
|
|
wasm::LocalVariable{condition},
|
|
|
|
visitReturnByValue(*c.value)
|
|
|
|
)};
|
2019-04-23 19:32:58 +00:00
|
|
|
wasm::If ifStmnt{
|
|
|
|
make_unique<wasm::Expression>(move(comparison)),
|
|
|
|
visit(c.body.statements),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
vector<wasm::Expression>* nextBlock = nullptr;
|
|
|
|
if (i != _switch.cases.size() - 1)
|
|
|
|
{
|
|
|
|
ifStmnt.elseStatements = make_unique<vector<wasm::Expression>>();
|
|
|
|
nextBlock = ifStmnt.elseStatements.get();
|
|
|
|
}
|
|
|
|
currentBlock->emplace_back(move(ifStmnt));
|
|
|
|
currentBlock = nextBlock;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
yulAssert(i == _switch.cases.size() - 1, "Default case must be last.");
|
|
|
|
*currentBlock += visit(c.body.statements);
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 14:44:28 +00:00
|
|
|
return { std::move(block) };
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(FunctionDefinition const&)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
yulAssert(false, "Should not have visited here.");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(ForLoop const& _for)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
string breakLabel = newLabel();
|
|
|
|
string continueLabel = newLabel();
|
|
|
|
m_breakContinueLabelNames.push({breakLabel, continueLabel});
|
|
|
|
|
2020-05-28 19:44:52 +00:00
|
|
|
yul::Type conditionType = m_typeInfo.typeOf(*_for.condition);
|
|
|
|
YulString eqz_instruction = YulString(conditionType.str() + ".eqz");
|
|
|
|
yulAssert(WasmDialect::instance().builtin(eqz_instruction), "");
|
|
|
|
|
2020-06-17 02:23:26 +00:00
|
|
|
std::vector<wasm::Expression> statements = visit(_for.pre.statements);
|
|
|
|
|
2019-04-18 12:39:48 +00:00
|
|
|
wasm::Loop loop;
|
2020-05-19 20:11:22 +00:00
|
|
|
loop.labelName = newLabel();
|
2020-05-20 22:59:03 +00:00
|
|
|
loop.statements.emplace_back(wasm::BranchIf{wasm::Label{breakLabel}, make_unique<wasm::Expression>(
|
2020-05-28 19:44:52 +00:00
|
|
|
wasm::BuiltinCall{eqz_instruction.str(), make_vector<wasm::Expression>(
|
2019-05-17 12:13:43 +00:00
|
|
|
visitReturnByValue(*_for.condition)
|
|
|
|
)}
|
|
|
|
)});
|
2019-04-18 12:39:48 +00:00
|
|
|
loop.statements.emplace_back(wasm::Block{continueLabel, visit(_for.body.statements)});
|
|
|
|
loop.statements += visit(_for.post.statements);
|
2020-05-20 22:59:03 +00:00
|
|
|
loop.statements.emplace_back(wasm::Branch{wasm::Label{loop.labelName}});
|
2019-04-18 12:39:48 +00:00
|
|
|
|
2020-06-17 02:23:26 +00:00
|
|
|
statements += make_vector<wasm::Expression>(move(loop));
|
|
|
|
return wasm::Block{breakLabel, move(statements)};
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(Break const&)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2020-05-20 22:59:03 +00:00
|
|
|
return wasm::Branch{wasm::Label{m_breakContinueLabelNames.top().first}};
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(Continue const&)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2020-05-20 22:59:03 +00:00
|
|
|
return wasm::Branch{wasm::Label{m_breakContinueLabelNames.top().second}};
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(Leave const&)
|
2019-10-28 14:25:02 +00:00
|
|
|
{
|
2020-05-25 21:33:59 +00:00
|
|
|
yulAssert(!m_functionBodyLabel.empty(), "");
|
|
|
|
return wasm::Branch{wasm::Label{m_functionBodyLabel}};
|
2019-10-28 14:25:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::operator()(Block const& _block)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
return wasm::Block{{}, visit(_block.statements)};
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
unique_ptr<wasm::Expression> WasmCodeTransform::visit(yul::Expression const& _expression)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
return make_unique<wasm::Expression>(std::visit(*this, _expression));
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::visitReturnByValue(yul::Expression const& _expression)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
return std::visit(*this, _expression);
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
vector<wasm::Expression> WasmCodeTransform::visit(vector<yul::Expression> const& _expressions)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
vector<wasm::Expression> ret;
|
|
|
|
for (auto const& e: _expressions)
|
|
|
|
ret.emplace_back(visitReturnByValue(e));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::Expression WasmCodeTransform::visit(yul::Statement const& _statement)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-11-19 15:42:49 +00:00
|
|
|
return std::visit(*this, _statement);
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
vector<wasm::Expression> WasmCodeTransform::visit(vector<yul::Statement> const& _statements)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
vector<wasm::Expression> ret;
|
|
|
|
for (auto const& s: _statements)
|
|
|
|
ret.emplace_back(visit(s));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
wasm::FunctionDefinition WasmCodeTransform::translateFunction(yul::FunctionDefinition const& _fun)
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
|
|
|
wasm::FunctionDefinition fun;
|
|
|
|
fun.name = _fun.name.str();
|
|
|
|
for (auto const& param: _fun.parameters)
|
2020-05-28 19:44:52 +00:00
|
|
|
fun.parameters.push_back({param.name.str(), translatedType(param.type)});
|
2019-04-18 12:39:48 +00:00
|
|
|
for (auto const& retParam: _fun.returnVariables)
|
2020-05-28 19:44:52 +00:00
|
|
|
fun.locals.emplace_back(wasm::VariableDeclaration{retParam.name.str(), translatedType(retParam.type)});
|
2020-05-28 19:20:19 +00:00
|
|
|
if (!_fun.returnVariables.empty())
|
2020-05-28 19:44:52 +00:00
|
|
|
fun.returnType = translatedType(_fun.returnVariables[0].type);
|
2019-04-18 12:39:48 +00:00
|
|
|
|
|
|
|
yulAssert(m_localVariables.empty(), "");
|
2020-05-25 21:33:59 +00:00
|
|
|
yulAssert(m_functionBodyLabel.empty(), "");
|
|
|
|
m_functionBodyLabel = newLabel();
|
|
|
|
fun.body.emplace_back(wasm::Expression(wasm::Block{
|
|
|
|
m_functionBodyLabel,
|
|
|
|
visit(_fun.body.statements)
|
|
|
|
}));
|
2019-04-18 12:39:48 +00:00
|
|
|
fun.locals += m_localVariables;
|
|
|
|
|
|
|
|
m_localVariables.clear();
|
2020-05-25 21:33:59 +00:00
|
|
|
m_functionBodyLabel = {};
|
2019-04-23 08:45:16 +00:00
|
|
|
|
|
|
|
if (!_fun.returnVariables.empty())
|
|
|
|
{
|
|
|
|
// First return variable is returned directly, the others are stored
|
|
|
|
// in globals.
|
2020-05-28 19:44:52 +00:00
|
|
|
vector<wasm::Type> typesForGlobals;
|
|
|
|
for (size_t i = 1; i < _fun.returnVariables.size(); ++i)
|
|
|
|
typesForGlobals.push_back(translatedType(_fun.returnVariables[i].type));
|
|
|
|
vector<size_t> allocatedIndices = allocateGlobals(typesForGlobals);
|
|
|
|
yulAssert(allocatedIndices.size() == _fun.returnVariables.size() - 1, "");
|
|
|
|
|
2019-04-23 08:45:16 +00:00
|
|
|
for (size_t i = 1; i < _fun.returnVariables.size(); ++i)
|
|
|
|
fun.body.emplace_back(wasm::GlobalAssignment{
|
2020-05-28 19:44:52 +00:00
|
|
|
m_globalVariables.at(allocatedIndices[i - 1]).variableName,
|
2019-04-23 08:45:16 +00:00
|
|
|
make_unique<wasm::Expression>(wasm::LocalVariable{_fun.returnVariables.at(i).name.str()})
|
|
|
|
});
|
|
|
|
fun.body.emplace_back(wasm::LocalVariable{_fun.returnVariables.front().name.str()});
|
|
|
|
}
|
2019-04-18 12:39:48 +00:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:27:24 +00:00
|
|
|
string WasmCodeTransform::newLabel()
|
2019-04-18 12:39:48 +00:00
|
|
|
{
|
2019-04-23 08:45:16 +00:00
|
|
|
return m_nameDispenser.newName("label_"_yulstring).str();
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:44:52 +00:00
|
|
|
vector<size_t> WasmCodeTransform::allocateGlobals(vector<wasm::Type> const& _typesForGlobals)
|
2019-04-23 08:45:16 +00:00
|
|
|
{
|
2020-05-28 19:44:52 +00:00
|
|
|
map<wasm::Type, size_t> availableGlobals;
|
|
|
|
for (wasm::GlobalVariableDeclaration const& global: m_globalVariables)
|
|
|
|
++availableGlobals[global.type];
|
|
|
|
|
|
|
|
map<wasm::Type, size_t> neededGlobals;
|
|
|
|
for (wasm::Type const& type: _typesForGlobals)
|
|
|
|
++neededGlobals[type];
|
|
|
|
|
|
|
|
for (auto [type, neededGlobalCount]: neededGlobals)
|
|
|
|
while (availableGlobals[type] < neededGlobalCount)
|
|
|
|
{
|
|
|
|
m_globalVariables.emplace_back(wasm::GlobalVariableDeclaration{
|
|
|
|
m_nameDispenser.newName("global_"_yulstring).str(),
|
|
|
|
type,
|
|
|
|
});
|
|
|
|
|
|
|
|
++availableGlobals[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<size_t> allocatedIndices;
|
|
|
|
map<wasm::Type, size_t> nextGlobal;
|
|
|
|
for (wasm::Type const& type: _typesForGlobals)
|
|
|
|
{
|
|
|
|
while (m_globalVariables[nextGlobal[type]].type != type)
|
|
|
|
++nextGlobal[type];
|
|
|
|
|
|
|
|
allocatedIndices.push_back(nextGlobal[type]++);
|
|
|
|
}
|
|
|
|
|
|
|
|
yulAssert(all_of(
|
|
|
|
allocatedIndices.begin(),
|
|
|
|
allocatedIndices.end(),
|
|
|
|
[this](size_t index){ return index < m_globalVariables.size(); }
|
|
|
|
), "");
|
|
|
|
yulAssert(allocatedIndices.size() == set<size_t>(allocatedIndices.begin(), allocatedIndices.end()).size(), "Indices not unique");
|
|
|
|
yulAssert(allocatedIndices.size() == _typesForGlobals.size(), "");
|
|
|
|
return allocatedIndices;
|
2019-04-18 12:39:48 +00:00
|
|
|
}
|
2020-06-10 22:54:12 +00:00
|
|
|
|
|
|
|
wasm::Type WasmCodeTransform::translatedType(yul::Type _yulType)
|
|
|
|
{
|
|
|
|
if (_yulType == "i32"_yulstring)
|
|
|
|
return wasm::Type::i32;
|
|
|
|
else if (_yulType == "i64"_yulstring)
|
|
|
|
return wasm::Type::i64;
|
|
|
|
else
|
|
|
|
yulAssert(false, "This Yul type does not have a corresponding type in Wasm.");
|
|
|
|
}
|
2020-05-28 19:44:52 +00:00
|
|
|
|
|
|
|
wasm::Literal WasmCodeTransform::makeLiteral(wasm::Type _type, u256 _value)
|
|
|
|
{
|
|
|
|
if (_type == wasm::Type::i32)
|
|
|
|
{
|
|
|
|
yulAssert(_value <= numeric_limits<uint32_t>::max(), "Literal too large: " + _value.str());
|
|
|
|
return wasm::Literal{static_cast<uint32_t>(_value)};
|
|
|
|
}
|
|
|
|
else if (_type == wasm::Type::i64)
|
|
|
|
{
|
|
|
|
yulAssert(_value <= numeric_limits<uint64_t>::max(), "Literal too large: " + _value.str());
|
|
|
|
return wasm::Literal{static_cast<uint64_t>(_value)};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
yulAssert(false, "Invalid Wasm literal type");
|
|
|
|
}
|