mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Take special functions that require literals into account.
This commit is contained in:
parent
9f5d34af7d
commit
5b73c2ae3b
@ -299,11 +299,14 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
|
|||||||
bool success = true;
|
bool success = true;
|
||||||
size_t parameters = 0;
|
size_t parameters = 0;
|
||||||
size_t returns = 0;
|
size_t returns = 0;
|
||||||
|
bool needsLiteralArguments = false;
|
||||||
if (BuiltinFunction const* f = m_dialect->builtin(_funCall.functionName.name))
|
if (BuiltinFunction const* f = m_dialect->builtin(_funCall.functionName.name))
|
||||||
{
|
{
|
||||||
// TODO: compare types, too
|
// TODO: compare types, too
|
||||||
parameters = f->parameters.size();
|
parameters = f->parameters.size();
|
||||||
returns = f->returns.size();
|
returns = f->returns.size();
|
||||||
|
if (f->literalArguments)
|
||||||
|
needsLiteralArguments = true;
|
||||||
}
|
}
|
||||||
else if (!m_currentScope->lookup(_funCall.functionName.name, Scope::Visitor(
|
else if (!m_currentScope->lookup(_funCall.functionName.name, Scope::Visitor(
|
||||||
[&](Scope::Variable const&)
|
[&](Scope::Variable const&)
|
||||||
@ -347,8 +350,15 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (auto const& arg: _funCall.arguments | boost::adaptors::reversed)
|
for (auto const& arg: _funCall.arguments | boost::adaptors::reversed)
|
||||||
|
{
|
||||||
if (!expectExpression(arg))
|
if (!expectExpression(arg))
|
||||||
success = false;
|
success = false;
|
||||||
|
else if (needsLiteralArguments && arg.type() != typeid(Literal))
|
||||||
|
m_errorReporter.typeError(
|
||||||
|
_funCall.functionName.location,
|
||||||
|
"Function expects direct literals as arguments."
|
||||||
|
);
|
||||||
|
}
|
||||||
// Use argument size instead of parameter count to avoid misleading errors.
|
// Use argument size instead of parameter count to avoid misleading errors.
|
||||||
m_stackHeight += int(returns) - int(_funCall.arguments.size());
|
m_stackHeight += int(returns) - int(_funCall.arguments.size());
|
||||||
m_info.stackHeightInfo[&_funCall] = m_stackHeight;
|
m_info.stackHeightInfo[&_funCall] = m_stackHeight;
|
||||||
|
@ -44,7 +44,13 @@ struct BuiltinFunction
|
|||||||
YulString name;
|
YulString name;
|
||||||
std::vector<Type> parameters;
|
std::vector<Type> parameters;
|
||||||
std::vector<Type> returns;
|
std::vector<Type> returns;
|
||||||
bool movable;
|
/// If true, calls to this function can be freely moved and copied (as long as their
|
||||||
|
/// arguments are either variables or also movable) without altering the semantics.
|
||||||
|
/// This means the function cannot depend on storage or memory, cannot have any side-effects,
|
||||||
|
/// but it can depend on state that is constant across an EVM-call.
|
||||||
|
bool movable = false;
|
||||||
|
/// If true, can only accept literals as arguments and they cannot be moved to voriables.
|
||||||
|
bool literalArguments = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Dialect: boost::noncopyable
|
struct Dialect: boost::noncopyable
|
||||||
|
@ -44,7 +44,7 @@ EVMDialect::EVMDialect(AsmFlavour _flavour, bool _objectAccess):
|
|||||||
if (!m_objectAccess)
|
if (!m_objectAccess)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
addFunction("datasize", 1, 1, true, [this](
|
addFunction("datasize", 1, 1, true, true, [this](
|
||||||
FunctionCall const& _call,
|
FunctionCall const& _call,
|
||||||
AbstractAssembly& _assembly,
|
AbstractAssembly& _assembly,
|
||||||
std::function<void()>
|
std::function<void()>
|
||||||
@ -58,7 +58,7 @@ EVMDialect::EVMDialect(AsmFlavour _flavour, bool _objectAccess):
|
|||||||
else
|
else
|
||||||
_assembly.appendDataSize(m_subIDs.at(dataName));
|
_assembly.appendDataSize(m_subIDs.at(dataName));
|
||||||
});
|
});
|
||||||
addFunction("dataoffset", 1, 1, true, [this](
|
addFunction("dataoffset", 1, 1, true, true, [this](
|
||||||
FunctionCall const& _call,
|
FunctionCall const& _call,
|
||||||
AbstractAssembly& _assembly,
|
AbstractAssembly& _assembly,
|
||||||
std::function<void()>
|
std::function<void()>
|
||||||
@ -72,7 +72,7 @@ EVMDialect::EVMDialect(AsmFlavour _flavour, bool _objectAccess):
|
|||||||
else
|
else
|
||||||
_assembly.appendDataOffset(m_subIDs.at(dataName));
|
_assembly.appendDataOffset(m_subIDs.at(dataName));
|
||||||
});
|
});
|
||||||
addFunction("datacopy", 3, 0, false, [](
|
addFunction("datacopy", 3, 0, false, false, [](
|
||||||
FunctionCall const&,
|
FunctionCall const&,
|
||||||
AbstractAssembly& _assembly,
|
AbstractAssembly& _assembly,
|
||||||
std::function<void()> _visitArguments
|
std::function<void()> _visitArguments
|
||||||
@ -128,6 +128,7 @@ void EVMDialect::addFunction(
|
|||||||
size_t _params,
|
size_t _params,
|
||||||
size_t _returns,
|
size_t _returns,
|
||||||
bool _movable,
|
bool _movable,
|
||||||
|
bool _literalArguments,
|
||||||
std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode
|
std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@ -137,5 +138,6 @@ void EVMDialect::addFunction(
|
|||||||
f.parameters.resize(_params);
|
f.parameters.resize(_params);
|
||||||
f.returns.resize(_returns);
|
f.returns.resize(_returns);
|
||||||
f.movable = _movable;
|
f.movable = _movable;
|
||||||
|
f.literalArguments = _literalArguments;
|
||||||
f.generateCode = std::move(_generateCode);
|
f.generateCode = std::move(_generateCode);
|
||||||
}
|
}
|
||||||
|
@ -72,6 +72,7 @@ private:
|
|||||||
size_t _params,
|
size_t _params,
|
||||||
size_t _returns,
|
size_t _returns,
|
||||||
bool _movable,
|
bool _movable,
|
||||||
|
bool _literalArguments,
|
||||||
std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode
|
std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include <libyul/optimiser/SyntacticalEquality.h>
|
#include <libyul/optimiser/SyntacticalEquality.h>
|
||||||
#include <libyul/Exceptions.h>
|
#include <libyul/Exceptions.h>
|
||||||
#include <libyul/AsmData.h>
|
#include <libyul/AsmData.h>
|
||||||
|
#include <libyul/Dialect.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace dev;
|
using namespace dev;
|
||||||
@ -32,11 +33,23 @@ using namespace yul;
|
|||||||
|
|
||||||
void CommonSubexpressionEliminator::visit(Expression& _e)
|
void CommonSubexpressionEliminator::visit(Expression& _e)
|
||||||
{
|
{
|
||||||
|
bool descend = true;
|
||||||
|
// If this is a function call to a function that requires literal arguments,
|
||||||
|
// do not try to simplify there.
|
||||||
|
if (_e.type() == typeid(FunctionCall))
|
||||||
|
if (BuiltinFunction const* builtin = m_dialect.builtin(boost::get<FunctionCall>(_e).functionName.name))
|
||||||
|
if (builtin->literalArguments)
|
||||||
|
// We should not modify function arguments that have to be literals
|
||||||
|
// Note that replacing the function call entirely is fine,
|
||||||
|
// if the function call is movable.
|
||||||
|
descend = false;
|
||||||
|
|
||||||
// We visit the inner expression first to first simplify inner expressions,
|
// We visit the inner expression first to first simplify inner expressions,
|
||||||
// which hopefully allows more matches.
|
// which hopefully allows more matches.
|
||||||
// Note that the DataFlowAnalyzer itself only has code for visiting Statements,
|
// Note that the DataFlowAnalyzer itself only has code for visiting Statements,
|
||||||
// so this basically invokes the AST walker directly and thus post-visiting
|
// so this basically invokes the AST walker directly and thus post-visiting
|
||||||
// is also fine with regards to data flow analysis.
|
// is also fine with regards to data flow analysis.
|
||||||
|
if (descend)
|
||||||
DataFlowAnalyzer::visit(_e);
|
DataFlowAnalyzer::visit(_e);
|
||||||
|
|
||||||
if (_e.type() == typeid(Identifier))
|
if (_e.type() == typeid(Identifier))
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
namespace yul
|
namespace yul
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct Dialect;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optimisation stage that replaces expressions known to be the current value of a variable
|
* Optimisation stage that replaces expressions known to be the current value of a variable
|
||||||
* in scope by a reference to that variable.
|
* in scope by a reference to that variable.
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
#include <libyul/optimiser/ASTWalker.h>
|
#include <libyul/optimiser/ASTWalker.h>
|
||||||
|
|
||||||
#include <libyul/AsmData.h>
|
#include <libyul/AsmData.h>
|
||||||
|
#include <libyul/Dialect.h>
|
||||||
|
|
||||||
#include <libdevcore/CommonData.h>
|
#include <libdevcore/CommonData.h>
|
||||||
|
|
||||||
@ -43,6 +44,11 @@ void ExpressionSplitter::operator()(FunctionalInstruction& _instruction)
|
|||||||
|
|
||||||
void ExpressionSplitter::operator()(FunctionCall& _funCall)
|
void ExpressionSplitter::operator()(FunctionCall& _funCall)
|
||||||
{
|
{
|
||||||
|
if (BuiltinFunction const* builtin = m_dialect.builtin(_funCall.functionName.name))
|
||||||
|
if (builtin->literalArguments)
|
||||||
|
// We cannot outline function arguments that have to be literals
|
||||||
|
return;
|
||||||
|
|
||||||
for (auto& arg: _funCall.arguments | boost::adaptors::reversed)
|
for (auto& arg: _funCall.arguments | boost::adaptors::reversed)
|
||||||
outlineExpression(arg);
|
outlineExpression(arg);
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ namespace yul
|
|||||||
{
|
{
|
||||||
|
|
||||||
class NameCollector;
|
class NameCollector;
|
||||||
|
struct Dialect;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,8 +58,8 @@ class NameCollector;
|
|||||||
class ExpressionSplitter: public ASTModifier
|
class ExpressionSplitter: public ASTModifier
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit ExpressionSplitter(NameDispenser& _nameDispenser):
|
explicit ExpressionSplitter(Dialect const& _dialect, NameDispenser& _nameDispenser):
|
||||||
m_nameDispenser(_nameDispenser)
|
m_dialect(_dialect), m_nameDispenser(_nameDispenser)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
void operator()(FunctionalInstruction&) override;
|
void operator()(FunctionalInstruction&) override;
|
||||||
@ -77,6 +78,7 @@ private:
|
|||||||
/// List of statements that should go in front of the currently visited AST element,
|
/// List of statements that should go in front of the currently visited AST element,
|
||||||
/// at the statement level.
|
/// at the statement level.
|
||||||
std::vector<Statement> m_statementsToPrefix;
|
std::vector<Statement> m_statementsToPrefix;
|
||||||
|
Dialect const& m_dialect;
|
||||||
NameDispenser& m_nameDispenser;
|
NameDispenser& m_nameDispenser;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ void OptimiserSuite::run(
|
|||||||
|
|
||||||
for (size_t i = 0; i < 4; i++)
|
for (size_t i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
ExpressionSplitter{dispenser}(ast);
|
ExpressionSplitter{_dialect, dispenser}(ast);
|
||||||
SSATransform::run(ast, dispenser);
|
SSATransform::run(ast, dispenser);
|
||||||
RedundantAssignEliminator::run(_dialect, ast);
|
RedundantAssignEliminator::run(_dialect, ast);
|
||||||
RedundantAssignEliminator::run(_dialect, ast);
|
RedundantAssignEliminator::run(_dialect, ast);
|
||||||
@ -90,7 +90,7 @@ void OptimiserSuite::run(
|
|||||||
ExpressionInliner(_dialect, ast).run();
|
ExpressionInliner(_dialect, ast).run();
|
||||||
UnusedPruner::runUntilStabilised(_dialect, ast);
|
UnusedPruner::runUntilStabilised(_dialect, ast);
|
||||||
|
|
||||||
ExpressionSplitter{dispenser}(ast);
|
ExpressionSplitter{_dialect, dispenser}(ast);
|
||||||
SSATransform::run(ast, dispenser);
|
SSATransform::run(ast, dispenser);
|
||||||
RedundantAssignEliminator::run(_dialect, ast);
|
RedundantAssignEliminator::run(_dialect, ast);
|
||||||
RedundantAssignEliminator::run(_dialect, ast);
|
RedundantAssignEliminator::run(_dialect, ast);
|
||||||
|
@ -41,7 +41,6 @@ public:
|
|||||||
Dialect const& _dialect,
|
Dialect const& _dialect,
|
||||||
Block& _ast,
|
Block& _ast,
|
||||||
AsmAnalysisInfo const& _analysisInfo,
|
AsmAnalysisInfo const& _analysisInfo,
|
||||||
|
|
||||||
std::set<YulString> const& _externallyUsedIdentifiers = {}
|
std::set<YulString> const& _externallyUsedIdentifiers = {}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -331,7 +331,7 @@ BOOST_AUTO_TEST_CASE(builtins_analysis)
|
|||||||
{
|
{
|
||||||
return _name == "builtin"_yulstring ? &f : nullptr;
|
return _name == "builtin"_yulstring ? &f : nullptr;
|
||||||
}
|
}
|
||||||
BuiltinFunction f{"builtin"_yulstring, vector<Type>(2), vector<Type>(3), false};
|
BuiltinFunction f{"builtin"_yulstring, vector<Type>(2), vector<Type>(3), false, false};
|
||||||
};
|
};
|
||||||
|
|
||||||
shared_ptr<Dialect> dialect = make_shared<SimpleDialect>();
|
shared_ptr<Dialect> dialect = make_shared<SimpleDialect>();
|
||||||
|
@ -122,7 +122,7 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
|
|||||||
else if (m_optimizerStep == "expressionSplitter")
|
else if (m_optimizerStep == "expressionSplitter")
|
||||||
{
|
{
|
||||||
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
||||||
ExpressionSplitter{nameDispenser}(*m_ast);
|
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
|
||||||
}
|
}
|
||||||
else if (m_optimizerStep == "expressionJoiner")
|
else if (m_optimizerStep == "expressionJoiner")
|
||||||
{
|
{
|
||||||
@ -133,7 +133,7 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
|
|||||||
{
|
{
|
||||||
disambiguate();
|
disambiguate();
|
||||||
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
||||||
ExpressionSplitter{nameDispenser}(*m_ast);
|
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
|
||||||
ExpressionJoiner::run(*m_ast);
|
ExpressionJoiner::run(*m_ast);
|
||||||
ExpressionJoiner::run(*m_ast);
|
ExpressionJoiner::run(*m_ast);
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
|
|||||||
(FunctionHoister{})(*m_ast);
|
(FunctionHoister{})(*m_ast);
|
||||||
(FunctionGrouper{})(*m_ast);
|
(FunctionGrouper{})(*m_ast);
|
||||||
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
||||||
ExpressionSplitter{nameDispenser}(*m_ast);
|
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
|
||||||
FullInliner(*m_ast, nameDispenser).run();
|
FullInliner(*m_ast, nameDispenser).run();
|
||||||
ExpressionJoiner::run(*m_ast);
|
ExpressionJoiner::run(*m_ast);
|
||||||
}
|
}
|
||||||
@ -182,7 +182,7 @@ bool YulOptimizerTest::run(ostream& _stream, string const& _linePrefix, bool con
|
|||||||
{
|
{
|
||||||
disambiguate();
|
disambiguate();
|
||||||
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
NameDispenser nameDispenser{*m_dialect, *m_ast};
|
||||||
ExpressionSplitter{nameDispenser}(*m_ast);
|
ExpressionSplitter{*m_dialect, nameDispenser}(*m_ast);
|
||||||
CommonSubexpressionEliminator{*m_dialect}(*m_ast);
|
CommonSubexpressionEliminator{*m_dialect}(*m_ast);
|
||||||
ExpressionSimplifier::run(*m_dialect, *m_ast);
|
ExpressionSimplifier::run(*m_dialect, *m_ast);
|
||||||
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
|
UnusedPruner::runUntilStabilised(*m_dialect, *m_ast);
|
||||||
@ -260,7 +260,7 @@ void YulOptimizerTest::printIndented(ostream& _stream, string const& _output, st
|
|||||||
|
|
||||||
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
|
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
|
||||||
{
|
{
|
||||||
m_dialect = m_yul ? yul::Dialect::yul() : yul::EVMDialect::strictAssemblyForEVM();
|
m_dialect = m_yul ? yul::Dialect::yul() : yul::EVMDialect::strictAssemblyForEVMObjects();
|
||||||
ErrorList errors;
|
ErrorList errors;
|
||||||
ErrorReporter errorReporter(errors);
|
ErrorReporter errorReporter(errors);
|
||||||
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source, ""));
|
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source, ""));
|
||||||
|
@ -149,7 +149,7 @@ public:
|
|||||||
(VarDeclInitializer{})(*m_ast);
|
(VarDeclInitializer{})(*m_ast);
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
ExpressionSplitter{*m_nameDispenser}(*m_ast);
|
ExpressionSplitter{*m_dialect, *m_nameDispenser}(*m_ast);
|
||||||
break;
|
break;
|
||||||
case 'j':
|
case 'j':
|
||||||
ExpressionJoiner::run(*m_ast);
|
ExpressionJoiner::run(*m_ast);
|
||||||
|
Loading…
Reference in New Issue
Block a user