mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Enable translation of i32 Yul variables/literals to i32 wasm variables/literals
- Until now they were being translated to i64
This commit is contained in:
parent
50e8d6850f
commit
d9ca02b47a
@ -20,6 +20,8 @@
|
|||||||
|
|
||||||
#include <libyul/backends/wasm/WasmCodeTransform.h>
|
#include <libyul/backends/wasm/WasmCodeTransform.h>
|
||||||
|
|
||||||
|
#include <libyul/backends/wasm/WasmDialect.h>
|
||||||
|
|
||||||
#include <libyul/optimiser/NameCollector.h>
|
#include <libyul/optimiser/NameCollector.h>
|
||||||
|
|
||||||
#include <libyul/AsmData.h>
|
#include <libyul/AsmData.h>
|
||||||
@ -40,7 +42,8 @@ wasm::Module WasmCodeTransform::run(Dialect const& _dialect, yul::Block const& _
|
|||||||
{
|
{
|
||||||
wasm::Module module;
|
wasm::Module module;
|
||||||
|
|
||||||
WasmCodeTransform transform(_dialect, _ast);
|
TypeInfo typeInfo(_dialect, _ast);
|
||||||
|
WasmCodeTransform transform(_dialect, _ast, typeInfo);
|
||||||
|
|
||||||
for (auto const& statement: _ast.statements)
|
for (auto const& statement: _ast.statements)
|
||||||
{
|
{
|
||||||
@ -70,14 +73,18 @@ wasm::Expression WasmCodeTransform::generateMultiAssignment(
|
|||||||
if (_variableNames.size() == 1)
|
if (_variableNames.size() == 1)
|
||||||
return { std::move(assignment) };
|
return { std::move(assignment) };
|
||||||
|
|
||||||
allocateGlobals(_variableNames.size() - 1);
|
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, "");
|
||||||
|
|
||||||
wasm::Block block;
|
wasm::Block block;
|
||||||
block.statements.emplace_back(move(assignment));
|
block.statements.emplace_back(move(assignment));
|
||||||
for (size_t i = 1; i < _variableNames.size(); ++i)
|
for (size_t i = 1; i < _variableNames.size(); ++i)
|
||||||
block.statements.emplace_back(wasm::LocalAssignment{
|
block.statements.emplace_back(wasm::LocalAssignment{
|
||||||
move(_variableNames.at(i)),
|
move(_variableNames.at(i)),
|
||||||
make_unique<wasm::Expression>(wasm::GlobalVariable{m_globalVariables.at(i - 1).variableName})
|
make_unique<wasm::Expression>(wasm::GlobalVariable{m_globalVariables.at(allocatedIndices[i - 1]).variableName})
|
||||||
});
|
});
|
||||||
return { std::move(block) };
|
return { std::move(block) };
|
||||||
}
|
}
|
||||||
@ -88,7 +95,7 @@ wasm::Expression WasmCodeTransform::operator()(VariableDeclaration const& _varDe
|
|||||||
for (auto const& var: _varDecl.variables)
|
for (auto const& var: _varDecl.variables)
|
||||||
{
|
{
|
||||||
variableNames.emplace_back(var.name.str());
|
variableNames.emplace_back(var.name.str());
|
||||||
m_localVariables.emplace_back(wasm::VariableDeclaration{variableNames.back(), wasm::Type::i64});
|
m_localVariables.emplace_back(wasm::VariableDeclaration{variableNames.back(), translatedType(var.type)});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_varDecl.value)
|
if (_varDecl.value)
|
||||||
@ -165,20 +172,21 @@ wasm::Expression WasmCodeTransform::operator()(Identifier const& _identifier)
|
|||||||
|
|
||||||
wasm::Expression WasmCodeTransform::operator()(Literal const& _literal)
|
wasm::Expression WasmCodeTransform::operator()(Literal const& _literal)
|
||||||
{
|
{
|
||||||
u256 value = valueOfLiteral(_literal);
|
return makeLiteral(translatedType(_literal.type), valueOfLiteral(_literal));
|
||||||
yulAssert(value <= numeric_limits<uint64_t>::max(), "Literal too large: " + value.str());
|
|
||||||
return wasm::Literal{static_cast<uint64_t>(value)};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wasm::Expression WasmCodeTransform::operator()(If const& _if)
|
wasm::Expression WasmCodeTransform::operator()(If const& _if)
|
||||||
{
|
{
|
||||||
// TODO converting i64 to i32 might not always be needed.
|
yul::Type conditionType = m_typeInfo.typeOf(*_if.condition);
|
||||||
|
YulString ne_instruction = YulString(conditionType.str() + ".ne");
|
||||||
|
yulAssert(WasmDialect::instance().builtin(ne_instruction), "");
|
||||||
|
|
||||||
|
// TODO converting i64 to i32 might not always be needed.
|
||||||
vector<wasm::Expression> args;
|
vector<wasm::Expression> args;
|
||||||
args.emplace_back(visitReturnByValue(*_if.condition));
|
args.emplace_back(visitReturnByValue(*_if.condition));
|
||||||
args.emplace_back(wasm::Literal{static_cast<uint64_t>(0)});
|
args.emplace_back(makeLiteral(translatedType(conditionType), 0));
|
||||||
return wasm::If{
|
return wasm::If{
|
||||||
make_unique<wasm::Expression>(wasm::BuiltinCall{"i64.ne", std::move(args)}),
|
make_unique<wasm::Expression>(wasm::BuiltinCall{ne_instruction.str(), std::move(args)}),
|
||||||
visit(_if.body.statements),
|
visit(_if.body.statements),
|
||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
@ -186,9 +194,13 @@ wasm::Expression WasmCodeTransform::operator()(If const& _if)
|
|||||||
|
|
||||||
wasm::Expression WasmCodeTransform::operator()(Switch const& _switch)
|
wasm::Expression WasmCodeTransform::operator()(Switch const& _switch)
|
||||||
{
|
{
|
||||||
|
yul::Type expressionType = m_typeInfo.typeOf(*_switch.expression);
|
||||||
|
YulString eq_instruction = YulString(expressionType.str() + ".eq");
|
||||||
|
yulAssert(WasmDialect::instance().builtin(eq_instruction), "");
|
||||||
|
|
||||||
wasm::Block block;
|
wasm::Block block;
|
||||||
string condition = m_nameDispenser.newName("condition"_yulstring).str();
|
string condition = m_nameDispenser.newName("condition"_yulstring).str();
|
||||||
m_localVariables.emplace_back(wasm::VariableDeclaration{condition, wasm::Type::i64});
|
m_localVariables.emplace_back(wasm::VariableDeclaration{condition, translatedType(expressionType)});
|
||||||
block.statements.emplace_back(wasm::LocalAssignment{condition, visit(*_switch.expression)});
|
block.statements.emplace_back(wasm::LocalAssignment{condition, visit(*_switch.expression)});
|
||||||
|
|
||||||
vector<wasm::Expression>* currentBlock = &block.statements;
|
vector<wasm::Expression>* currentBlock = &block.statements;
|
||||||
@ -197,7 +209,7 @@ wasm::Expression WasmCodeTransform::operator()(Switch const& _switch)
|
|||||||
Case const& c = _switch.cases.at(i);
|
Case const& c = _switch.cases.at(i);
|
||||||
if (c.value)
|
if (c.value)
|
||||||
{
|
{
|
||||||
wasm::BuiltinCall comparison{"i64.eq", make_vector<wasm::Expression>(
|
wasm::BuiltinCall comparison{eq_instruction.str(), make_vector<wasm::Expression>(
|
||||||
wasm::LocalVariable{condition},
|
wasm::LocalVariable{condition},
|
||||||
visitReturnByValue(*c.value)
|
visitReturnByValue(*c.value)
|
||||||
)};
|
)};
|
||||||
@ -236,11 +248,15 @@ wasm::Expression WasmCodeTransform::operator()(ForLoop const& _for)
|
|||||||
string continueLabel = newLabel();
|
string continueLabel = newLabel();
|
||||||
m_breakContinueLabelNames.push({breakLabel, continueLabel});
|
m_breakContinueLabelNames.push({breakLabel, continueLabel});
|
||||||
|
|
||||||
|
yul::Type conditionType = m_typeInfo.typeOf(*_for.condition);
|
||||||
|
YulString eqz_instruction = YulString(conditionType.str() + ".eqz");
|
||||||
|
yulAssert(WasmDialect::instance().builtin(eqz_instruction), "");
|
||||||
|
|
||||||
wasm::Loop loop;
|
wasm::Loop loop;
|
||||||
loop.labelName = newLabel();
|
loop.labelName = newLabel();
|
||||||
loop.statements = visit(_for.pre.statements);
|
loop.statements = visit(_for.pre.statements);
|
||||||
loop.statements.emplace_back(wasm::BranchIf{wasm::Label{breakLabel}, make_unique<wasm::Expression>(
|
loop.statements.emplace_back(wasm::BranchIf{wasm::Label{breakLabel}, make_unique<wasm::Expression>(
|
||||||
wasm::BuiltinCall{"i64.eqz", make_vector<wasm::Expression>(
|
wasm::BuiltinCall{eqz_instruction.str(), make_vector<wasm::Expression>(
|
||||||
visitReturnByValue(*_for.condition)
|
visitReturnByValue(*_for.condition)
|
||||||
)}
|
)}
|
||||||
)});
|
)});
|
||||||
@ -308,11 +324,11 @@ wasm::FunctionDefinition WasmCodeTransform::translateFunction(yul::FunctionDefin
|
|||||||
wasm::FunctionDefinition fun;
|
wasm::FunctionDefinition fun;
|
||||||
fun.name = _fun.name.str();
|
fun.name = _fun.name.str();
|
||||||
for (auto const& param: _fun.parameters)
|
for (auto const& param: _fun.parameters)
|
||||||
fun.parameters.push_back({param.name.str(), wasm::Type::i64});
|
fun.parameters.push_back({param.name.str(), translatedType(param.type)});
|
||||||
for (auto const& retParam: _fun.returnVariables)
|
for (auto const& retParam: _fun.returnVariables)
|
||||||
fun.locals.emplace_back(wasm::VariableDeclaration{retParam.name.str(), wasm::Type::i64});
|
fun.locals.emplace_back(wasm::VariableDeclaration{retParam.name.str(), translatedType(retParam.type)});
|
||||||
if (!_fun.returnVariables.empty())
|
if (!_fun.returnVariables.empty())
|
||||||
fun.returnType = wasm::Type::i64;
|
fun.returnType = translatedType(_fun.returnVariables[0].type);
|
||||||
|
|
||||||
yulAssert(m_localVariables.empty(), "");
|
yulAssert(m_localVariables.empty(), "");
|
||||||
yulAssert(m_functionBodyLabel.empty(), "");
|
yulAssert(m_functionBodyLabel.empty(), "");
|
||||||
@ -330,10 +346,15 @@ wasm::FunctionDefinition WasmCodeTransform::translateFunction(yul::FunctionDefin
|
|||||||
{
|
{
|
||||||
// First return variable is returned directly, the others are stored
|
// First return variable is returned directly, the others are stored
|
||||||
// in globals.
|
// in globals.
|
||||||
allocateGlobals(_fun.returnVariables.size() - 1);
|
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, "");
|
||||||
|
|
||||||
for (size_t i = 1; i < _fun.returnVariables.size(); ++i)
|
for (size_t i = 1; i < _fun.returnVariables.size(); ++i)
|
||||||
fun.body.emplace_back(wasm::GlobalAssignment{
|
fun.body.emplace_back(wasm::GlobalAssignment{
|
||||||
m_globalVariables.at(i - 1).variableName,
|
m_globalVariables.at(allocatedIndices[i - 1]).variableName,
|
||||||
make_unique<wasm::Expression>(wasm::LocalVariable{_fun.returnVariables.at(i).name.str()})
|
make_unique<wasm::Expression>(wasm::LocalVariable{_fun.returnVariables.at(i).name.str()})
|
||||||
});
|
});
|
||||||
fun.body.emplace_back(wasm::LocalVariable{_fun.returnVariables.front().name.str()});
|
fun.body.emplace_back(wasm::LocalVariable{_fun.returnVariables.front().name.str()});
|
||||||
@ -346,13 +367,45 @@ string WasmCodeTransform::newLabel()
|
|||||||
return m_nameDispenser.newName("label_"_yulstring).str();
|
return m_nameDispenser.newName("label_"_yulstring).str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WasmCodeTransform::allocateGlobals(size_t _amount)
|
vector<size_t> WasmCodeTransform::allocateGlobals(vector<wasm::Type> const& _typesForGlobals)
|
||||||
{
|
{
|
||||||
while (m_globalVariables.size() < _amount)
|
map<wasm::Type, size_t> availableGlobals;
|
||||||
m_globalVariables.emplace_back(wasm::GlobalVariableDeclaration{
|
for (wasm::GlobalVariableDeclaration const& global: m_globalVariables)
|
||||||
m_nameDispenser.newName("global_"_yulstring).str(),
|
++availableGlobals[global.type];
|
||||||
wasm::Type::i64
|
|
||||||
});
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
wasm::Type WasmCodeTransform::translatedType(yul::Type _yulType)
|
wasm::Type WasmCodeTransform::translatedType(yul::Type _yulType)
|
||||||
@ -364,3 +417,19 @@ wasm::Type WasmCodeTransform::translatedType(yul::Type _yulType)
|
|||||||
else
|
else
|
||||||
yulAssert(false, "This Yul type does not have a corresponding type in Wasm.");
|
yulAssert(false, "This Yul type does not have a corresponding type in Wasm.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
@ -24,6 +24,9 @@
|
|||||||
#include <libyul/AsmDataForward.h>
|
#include <libyul/AsmDataForward.h>
|
||||||
#include <libyul/Dialect.h>
|
#include <libyul/Dialect.h>
|
||||||
#include <libyul/optimiser/NameDispenser.h>
|
#include <libyul/optimiser/NameDispenser.h>
|
||||||
|
#include <libyul/optimiser/TypeInfo.h>
|
||||||
|
|
||||||
|
#include <libsolutil/Common.h>
|
||||||
|
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <map>
|
#include <map>
|
||||||
@ -56,10 +59,12 @@ public:
|
|||||||
private:
|
private:
|
||||||
WasmCodeTransform(
|
WasmCodeTransform(
|
||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
Block const& _ast
|
Block const& _ast,
|
||||||
|
TypeInfo& _typeInfo
|
||||||
):
|
):
|
||||||
m_dialect(_dialect),
|
m_dialect(_dialect),
|
||||||
m_nameDispenser(_dialect, _ast)
|
m_nameDispenser(_dialect, _ast),
|
||||||
|
m_typeInfo(_typeInfo)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
std::unique_ptr<wasm::Expression> visit(yul::Expression const& _expression);
|
std::unique_ptr<wasm::Expression> visit(yul::Expression const& _expression);
|
||||||
@ -80,10 +85,12 @@ private:
|
|||||||
wasm::FunctionDefinition translateFunction(yul::FunctionDefinition const& _funDef);
|
wasm::FunctionDefinition translateFunction(yul::FunctionDefinition const& _funDef);
|
||||||
|
|
||||||
std::string newLabel();
|
std::string newLabel();
|
||||||
/// Makes sure that there are at least @a _amount global variables.
|
/// Selects a subset of global variables matching specified sequence of variable types.
|
||||||
void allocateGlobals(size_t _amount);
|
/// Defines more global variables of a given type if there's not enough.
|
||||||
|
std::vector<size_t> allocateGlobals(std::vector<wasm::Type> const& _typesForGlobals);
|
||||||
|
|
||||||
static wasm::Type translatedType(yul::Type _yulType);
|
static wasm::Type translatedType(yul::Type _yulType);
|
||||||
|
static wasm::Literal makeLiteral(wasm::Type _type, u256 _value);
|
||||||
|
|
||||||
Dialect const& m_dialect;
|
Dialect const& m_dialect;
|
||||||
NameDispenser m_nameDispenser;
|
NameDispenser m_nameDispenser;
|
||||||
@ -93,6 +100,7 @@ private:
|
|||||||
std::map<YulString, wasm::FunctionImport> m_functionsToImport;
|
std::map<YulString, wasm::FunctionImport> m_functionsToImport;
|
||||||
std::string m_functionBodyLabel;
|
std::string m_functionBodyLabel;
|
||||||
std::stack<std::pair<std::string, std::string>> m_breakContinueLabelNames;
|
std::stack<std::pair<std::string, std::string>> m_breakContinueLabelNames;
|
||||||
|
TypeInfo& m_typeInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user