Use bool type in word size transform.

This commit is contained in:
chriseth 2020-02-20 16:17:09 +01:00
parent 6b272faec0
commit bddbcbe6a4
7 changed files with 52 additions and 35 deletions

View File

@ -1235,7 +1235,7 @@ Object EVMToEwasmTranslator::run(Object const& _object)
MainFunction{}(ast); MainFunction{}(ast);
ForLoopConditionIntoBody::run(context, ast); ForLoopConditionIntoBody::run(context, ast);
ExpressionSplitter::run(context, ast); ExpressionSplitter::run(context, ast);
WordSizeTransform::run(m_dialect, WasmDialect::instance().defaultType, ast, nameDispenser); WordSizeTransform::run(m_dialect, WasmDialect::instance(), ast, nameDispenser);
NameDisplacer{nameDispenser, m_polyfillFunctions}(ast); NameDisplacer{nameDispenser, m_polyfillFunctions}(ast);
for (auto const& st: m_polyfill->statements) for (auto const& st: m_polyfill->statements)

View File

@ -45,7 +45,7 @@ void WordSizeTransform::operator()(FunctionCall& _fc)
if (fun->literalArguments) if (fun->literalArguments)
{ {
for (Expression& arg: _fc.arguments) for (Expression& arg: _fc.arguments)
get<Literal>(arg).type = m_defaultType; get<Literal>(arg).type = m_targetDialect.defaultType;
return; return;
} }
@ -106,12 +106,17 @@ void WordSizeTransform::operator()(Block& _block)
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
ret.push_back(VariableDeclaration{ ret.push_back(VariableDeclaration{
varDecl.location, varDecl.location,
{TypedName{varDecl.location, newLhs[i], m_defaultType}}, {TypedName{varDecl.location, newLhs[i], m_targetDialect.defaultType}},
make_unique<Expression>(Literal{locationOf(*varDecl.value), LiteralKind::Number, "0"_yulstring, m_defaultType}) make_unique<Expression>(Literal{
locationOf(*varDecl.value),
LiteralKind::Number,
"0"_yulstring,
m_targetDialect.defaultType
})
}); });
ret.push_back(VariableDeclaration{ ret.push_back(VariableDeclaration{
varDecl.location, varDecl.location,
{TypedName{varDecl.location, newLhs[3], m_defaultType}}, {TypedName{varDecl.location, newLhs[3], m_targetDialect.defaultType}},
std::move(varDecl.value) std::move(varDecl.value)
}); });
return {std::move(ret)}; return {std::move(ret)};
@ -133,7 +138,7 @@ void WordSizeTransform::operator()(Block& _block)
ret.push_back( ret.push_back(
VariableDeclaration{ VariableDeclaration{
varDecl.location, varDecl.location,
{TypedName{varDecl.location, newLhs[i], m_defaultType}}, {TypedName{varDecl.location, newLhs[i], m_targetDialect.defaultType}},
std::move(newRhs[i]) std::move(newRhs[i])
} }
); );
@ -163,7 +168,12 @@ void WordSizeTransform::operator()(Block& _block)
ret.push_back(Assignment{ ret.push_back(Assignment{
assignment.location, assignment.location,
{Identifier{assignment.location, newLhs[i]}}, {Identifier{assignment.location, newLhs[i]}},
make_unique<Expression>(Literal{locationOf(*assignment.value), LiteralKind::Number, "0"_yulstring, m_defaultType}) make_unique<Expression>(Literal{
locationOf(*assignment.value),
LiteralKind::Number,
"0"_yulstring,
m_targetDialect.defaultType
})
}); });
ret.push_back(Assignment{ ret.push_back(Assignment{
assignment.location, assignment.location,
@ -209,14 +219,25 @@ void WordSizeTransform::operator()(Block& _block)
void WordSizeTransform::run( void WordSizeTransform::run(
Dialect const& _inputDialect, Dialect const& _inputDialect,
YulString _targetDefaultType, Dialect const& _targetDialect,
Block& _ast, Block& _ast,
NameDispenser& _nameDispenser NameDispenser& _nameDispenser
) )
{ {
// Free the name `or_bool`. // Free the name `or_bool`.
NameDisplacer{_nameDispenser, {"or_bool"_yulstring}}(_ast); NameDisplacer{_nameDispenser, {"or_bool"_yulstring}}(_ast);
WordSizeTransform{_inputDialect, _nameDispenser, _targetDefaultType}(_ast); WordSizeTransform{_inputDialect, _targetDialect, _nameDispenser}(_ast);
}
WordSizeTransform::WordSizeTransform(
Dialect const& _inputDialect,
Dialect const& _targetDialect,
NameDispenser& _nameDispenser
):
m_inputDialect(_inputDialect),
m_targetDialect(_targetDialect),
m_nameDispenser(_nameDispenser)
{
} }
void WordSizeTransform::rewriteVarDeclList(TypedNameList& _nameList) void WordSizeTransform::rewriteVarDeclList(TypedNameList& _nameList)
@ -227,7 +248,7 @@ void WordSizeTransform::rewriteVarDeclList(TypedNameList& _nameList)
{ {
TypedNameList ret; TypedNameList ret;
for (auto newName: generateU64IdentifierNames(_n.name)) for (auto newName: generateU64IdentifierNames(_n.name))
ret.emplace_back(TypedName{_n.location, newName, m_defaultType}); ret.emplace_back(TypedName{_n.location, newName, m_targetDialect.defaultType});
return ret; return ret;
} }
); );
@ -291,7 +312,7 @@ vector<Statement> WordSizeTransform::handleSwitchInternal(
for (auto& c: cases) for (auto& c: cases)
{ {
Literal label{_location, LiteralKind::Number, YulString(c.first.str()), m_defaultType}; Literal label{_location, LiteralKind::Number, YulString(c.first.str()), m_targetDialect.defaultType};
ret.cases.emplace_back(Case{ ret.cases.emplace_back(Case{
c.second.front().location, c.second.front().location,
make_unique<Literal>(std::move(label)), make_unique<Literal>(std::move(label)),
@ -312,7 +333,7 @@ vector<Statement> WordSizeTransform::handleSwitchInternal(
Assignment{ Assignment{
_location, _location,
{{_location, _runDefaultFlag}}, {{_location, _runDefaultFlag}},
make_unique<Expression>(Literal{_location, LiteralKind::Number, "1"_yulstring, m_defaultType}) make_unique<Expression>(Literal{_location, LiteralKind::Boolean, "true"_yulstring, m_targetDialect.boolType})
} }
)} )}
}); });
@ -337,7 +358,7 @@ std::vector<Statement> WordSizeTransform::handleSwitch(Switch& _switch)
_switch.cases.pop_back(); _switch.cases.pop_back();
ret.emplace_back(VariableDeclaration{ ret.emplace_back(VariableDeclaration{
_switch.location, _switch.location,
{TypedName{_switch.location, runDefaultFlag, m_defaultType}}, {TypedName{_switch.location, runDefaultFlag, m_targetDialect.boolType}},
{} {}
}); });
} }
@ -392,7 +413,7 @@ array<unique_ptr<Expression>, 4> WordSizeTransform::expandValue(Expression const
lit.location, lit.location,
LiteralKind::Number, LiteralKind::Number,
YulString(currentVal.str()), YulString(currentVal.str()),
m_defaultType m_targetDialect.defaultType
} }
); );
} }

View File

@ -69,7 +69,7 @@ public:
static void run( static void run(
Dialect const& _inputDialect, Dialect const& _inputDialect,
YulString _targetDefaultType, Dialect const& _targetDialect,
Block& _ast, Block& _ast,
NameDispenser& _nameDispenser NameDispenser& _nameDispenser
); );
@ -77,13 +77,9 @@ public:
private: private:
explicit WordSizeTransform( explicit WordSizeTransform(
Dialect const& _inputDialect, Dialect const& _inputDialect,
NameDispenser& _nameDispenser, Dialect const& _targetDialect,
YulString _defaultType NameDispenser& _nameDispenser
): );
m_inputDialect(_inputDialect),
m_nameDispenser(_nameDispenser),
m_defaultType(_defaultType)
{ }
void rewriteVarDeclList(std::vector<TypedName>&); void rewriteVarDeclList(std::vector<TypedName>&);
void rewriteIdentifierList(std::vector<Identifier>&); void rewriteIdentifierList(std::vector<Identifier>&);
@ -103,8 +99,8 @@ private:
std::vector<Expression> expandValueToVector(Expression const& _e); std::vector<Expression> expandValueToVector(Expression const& _e);
Dialect const& m_inputDialect; Dialect const& m_inputDialect;
Dialect const& m_targetDialect;
NameDispenser& m_nameDispenser; NameDispenser& m_nameDispenser;
YulString m_defaultType;
/// maps original u256 variable's name to corresponding u64 variables' names /// maps original u256 variable's name to corresponding u64 variables' names
std::map<YulString, std::array<YulString, 4>> m_variableMapping; std::map<YulString, std::array<YulString, 4>> m_variableMapping;
}; };

View File

@ -359,7 +359,7 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
{ {
disambiguate(); disambiguate();
ExpressionSplitter::run(*m_context, *m_ast); ExpressionSplitter::run(*m_context, *m_ast);
WordSizeTransform::run(*m_dialect, ""_yulstring, *m_ast, *m_nameDispenser); WordSizeTransform::run(*m_dialect, *m_dialect, *m_ast, *m_nameDispenser);
} }
else if (m_optimizerStep == "fullSuite") else if (m_optimizerStep == "fullSuite")
{ {

View File

@ -67,13 +67,13 @@
// let _10_3 := 3 // let _10_3 := 3
// sstore(_10_0, _10_1, _10_2, _10_3, _9_0, _9_1, _9_2, _9_3) // sstore(_10_0, _10_1, _10_2, _10_3, _9_0, _9_1, _9_2, _9_3)
// } // }
// default { run_default := 1 } // default { run_default := true }
// } // }
// default { run_default := 1 } // default { run_default := true }
// } // }
// default { run_default := 1 } // default { run_default := true }
// } // }
// default { run_default := 1 } // default { run_default := true }
// if run_default // if run_default
// { // {
// let _11_0 := 0 // let _11_0 := 0

View File

@ -45,9 +45,9 @@
// let _8_3 := 2 // let _8_3 := 2
// sstore(_8_0, _8_1, _8_2, _8_3, _7_0, _7_1, _7_2, _7_3) // sstore(_8_0, _8_1, _8_2, _8_3, _7_0, _7_1, _7_2, _7_3)
// } // }
// default { run_default := 1 } // default { run_default := true }
// } // }
// default { run_default := 1 } // default { run_default := true }
// } // }
// case 536870912 { // case 536870912 {
// switch _2_2 // switch _2_2
@ -75,13 +75,13 @@
// let _10_3 := 3 // let _10_3 := 3
// sstore(_10_0, _10_1, _10_2, _10_3, _9_0, _9_1, _9_2, _9_3) // sstore(_10_0, _10_1, _10_2, _10_3, _9_0, _9_1, _9_2, _9_3)
// } // }
// default { run_default := 1 } // default { run_default := true }
// } // }
// default { run_default := 1 } // default { run_default := true }
// } // }
// default { run_default := 1 } // default { run_default := true }
// } // }
// default { run_default := 1 } // default { run_default := true }
// if run_default // if run_default
// { // {
// let _11_0 := 0 // let _11_0 := 0

View File

@ -13,7 +13,7 @@
// let _2_0, _2_1, _2_2, _2_3 := calldataload(_1_0, _1_1, _1_2, _1_3) // let _2_0, _2_1, _2_2, _2_3 := calldataload(_1_0, _1_1, _1_2, _1_3)
// let run_default // let run_default
// switch _2_0 // switch _2_0
// default { run_default := 1 } // default { run_default := true }
// if run_default // if run_default
// { // {
// let _3_0 := 0 // let _3_0 := 0