Merge pull request #7071 from ethereum/checkAsmDataObject

Check availability of data objects already in analysis phase.
This commit is contained in:
chriseth 2019-07-11 11:20:07 +02:00 committed by GitHub
commit 88477bdb8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 173 additions and 76 deletions

View File

@ -18,6 +18,7 @@ Compiler Features:
Bugfixes: Bugfixes:
* View/Pure Checker: Properly detect state variable access through base class. * View/Pure Checker: Properly detect state variable access through base class.
* Yul analyzer: Check availability of data objects already in analysis phase.

View File

@ -34,6 +34,7 @@
#include <libyul/backends/evm/EVMDialect.h> #include <libyul/backends/evm/EVMDialect.h>
#include <libyul/backends/evm/EVMMetrics.h> #include <libyul/backends/evm/EVMMetrics.h>
#include <libyul/optimiser/Suite.h> #include <libyul/optimiser/Suite.h>
#include <libyul/Object.h>
#include <libyul/YulString.h> #include <libyul/YulString.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
@ -423,23 +424,19 @@ void CompilerContext::appendInlineAssembly(
{ {
bool const isCreation = m_runtimeContext != nullptr; bool const isCreation = m_runtimeContext != nullptr;
yul::GasMeter meter(dialect, isCreation, _optimiserSettings.expectedExecutionsPerDeployment); yul::GasMeter meter(dialect, isCreation, _optimiserSettings.expectedExecutionsPerDeployment);
yul::Object obj;
obj.code = parserResult;
obj.analysisInfo = make_shared<yul::AsmAnalysisInfo>(analysisInfo);
yul::OptimiserSuite::run( yul::OptimiserSuite::run(
dialect, dialect,
&meter, &meter,
*parserResult, obj,
analysisInfo,
_optimiserSettings.optimizeStackAllocation, _optimiserSettings.optimizeStackAllocation,
externallyUsedIdentifiers externallyUsedIdentifiers
); );
analysisInfo = yul::AsmAnalysisInfo{}; analysisInfo = std::move(*obj.analysisInfo);
if (!yul::AsmAnalyzer( parserResult = std::move(obj.code);
analysisInfo,
errorReporter,
boost::none,
dialect,
identifierAccess.resolve
).analyze(*parserResult))
reportError("Optimizer introduced error into inline assembly.");
#ifdef SOL_OUTPUT_ASM #ifdef SOL_OUTPUT_ASM
cout << "After optimizer: " << endl; cout << "After optimizer: " << endl;
cout << yul::AsmPrinter()(*parserResult) << endl; cout << yul::AsmPrinter()(*parserResult) << endl;

View File

@ -26,6 +26,7 @@
#include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmAnalysisInfo.h>
#include <libyul/Utilities.h> #include <libyul/Utilities.h>
#include <libyul/Exceptions.h> #include <libyul/Exceptions.h>
#include <libyul/Object.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
@ -69,17 +70,19 @@ bool AsmAnalyzer::analyze(Block const& _block)
return success && !m_errorReporter.hasErrors(); return success && !m_errorReporter.hasErrors();
} }
AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect, Block const& _ast) AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect, Object const& _object)
{ {
ErrorList errorList; ErrorList errorList;
langutil::ErrorReporter errors(errorList); langutil::ErrorReporter errors(errorList);
yul::AsmAnalysisInfo analysisInfo; AsmAnalysisInfo analysisInfo;
bool success = yul::AsmAnalyzer( bool success = yul::AsmAnalyzer(
analysisInfo, analysisInfo,
errors, errors,
Error::Type::SyntaxError, Error::Type::SyntaxError,
_dialect _dialect,
).analyze(_ast); {},
_object.dataNames()
).analyze(*_object.code);
solAssert(success && errorList.empty(), "Invalid assembly/yul code."); solAssert(success && errorList.empty(), "Invalid assembly/yul code.");
return analysisInfo; return analysisInfo;
} }
@ -383,11 +386,19 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
{ {
if (!expectExpression(arg)) if (!expectExpression(arg))
success = false; success = false;
else if (needsLiteralArguments && arg.type() != typeid(Literal)) else if (needsLiteralArguments)
m_errorReporter.typeError( {
_funCall.functionName.location, if (arg.type() != typeid(Literal))
"Function expects direct literals as arguments." m_errorReporter.typeError(
); _funCall.functionName.location,
"Function expects direct literals as arguments."
);
else if (!m_dataNames.count(boost::get<Literal>(arg).value))
m_errorReporter.typeError(
_funCall.functionName.location,
"Unknown data object \"" + boost::get<Literal>(arg).value.str() + "\"."
);
}
} }
// 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());

View File

@ -61,13 +61,15 @@ public:
langutil::ErrorReporter& _errorReporter, langutil::ErrorReporter& _errorReporter,
boost::optional<langutil::Error::Type> _errorTypeForLoose, boost::optional<langutil::Error::Type> _errorTypeForLoose,
Dialect const& _dialect, Dialect const& _dialect,
ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver() ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver(),
std::set<YulString> const& _dataNames = {}
): ):
m_resolver(_resolver), m_resolver(_resolver),
m_info(_analysisInfo), m_info(_analysisInfo),
m_errorReporter(_errorReporter), m_errorReporter(_errorReporter),
m_dialect(_dialect), m_dialect(_dialect),
m_errorTypeForLoose(_errorTypeForLoose) m_errorTypeForLoose(_errorTypeForLoose),
m_dataNames(_dataNames)
{ {
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&m_dialect)) if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&m_dialect))
m_evmVersion = evmDialect->evmVersion(); m_evmVersion = evmDialect->evmVersion();
@ -75,7 +77,9 @@ public:
bool analyze(Block const& _block); bool analyze(Block const& _block);
static AsmAnalysisInfo analyzeStrictAssertCorrect(Dialect const& _dialect, Block const& _ast); /// Performs analysis on the outermost code of the given object and returns the analysis info.
/// Asserts on failure.
static AsmAnalysisInfo analyzeStrictAssertCorrect(Dialect const& _dialect, Object const& _object);
bool operator()(Instruction const&); bool operator()(Instruction const&);
bool operator()(Literal const& _literal); bool operator()(Literal const& _literal);
@ -124,6 +128,8 @@ private:
langutil::EVMVersion m_evmVersion; langutil::EVMVersion m_evmVersion;
Dialect const& m_dialect; Dialect const& m_dialect;
boost::optional<langutil::Error::Type> m_errorTypeForLoose; boost::optional<langutil::Error::Type> m_errorTypeForLoose;
/// Names of data objects to be referenced by builtin functions with literal arguments.
std::set<YulString> m_dataNames;
ForLoop const* m_currentForLoop = nullptr; ForLoop const* m_currentForLoop = nullptr;
}; };

View File

@ -113,7 +113,15 @@ bool AssemblyStack::analyzeParsed(Object& _object)
{ {
solAssert(_object.code, ""); solAssert(_object.code, "");
_object.analysisInfo = make_shared<AsmAnalysisInfo>(); _object.analysisInfo = make_shared<AsmAnalysisInfo>();
AsmAnalyzer analyzer(*_object.analysisInfo, m_errorReporter, boost::none, languageToDialect(m_language, m_evmVersion));
AsmAnalyzer analyzer(
*_object.analysisInfo,
m_errorReporter,
boost::none,
languageToDialect(m_language, m_evmVersion),
{},
_object.dataNames()
);
bool success = analyzer.analyze(*_object.code); bool success = analyzer.analyze(*_object.code);
for (auto& subNode: _object.subObjects) for (auto& subNode: _object.subObjects)
if (auto subObject = dynamic_cast<Object*>(subNode.get())) if (auto subObject = dynamic_cast<Object*>(subNode.get()))
@ -153,8 +161,7 @@ void AssemblyStack::optimize(Object& _object, bool _isCreation)
OptimiserSuite::run( OptimiserSuite::run(
dialect, dialect,
meter.get(), meter.get(),
*_object.code, _object,
*_object.analysisInfo,
m_optimiserSettings.optimizeStackAllocation m_optimiserSettings.optimizeStackAllocation
); );
} }

View File

@ -34,7 +34,7 @@ using namespace dev;
map<YulString, int> CompilabilityChecker::run( map<YulString, int> CompilabilityChecker::run(
Dialect const& _dialect, Dialect const& _dialect,
Block const& _ast, Object const& _object,
bool _optimizeStackAllocation bool _optimizeStackAllocation
) )
{ {
@ -46,16 +46,26 @@ map<YulString, int> CompilabilityChecker::run(
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect)) if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect))
{ {
NoOutputEVMDialect noOutputDialect(*evmDialect); NoOutputEVMDialect noOutputDialect(*evmDialect);
BuiltinContext builtinContext;
yul::AsmAnalysisInfo analysisInfo = yul::AsmAnalysisInfo analysisInfo =
yul::AsmAnalyzer::analyzeStrictAssertCorrect(noOutputDialect, _ast); yul::AsmAnalyzer::analyzeStrictAssertCorrect(noOutputDialect, _object);
BuiltinContext builtinContext;
builtinContext.currentObject = &_object;
for (auto name: _object.dataNames())
builtinContext.subIDs[name] = 1;
NoOutputAssembly assembly; NoOutputAssembly assembly;
CodeTransform transform(assembly, analysisInfo, _ast, noOutputDialect, builtinContext, _optimizeStackAllocation); CodeTransform transform(
assembly,
analysisInfo,
*_object.code,
noOutputDialect,
builtinContext,
_optimizeStackAllocation
);
try try
{ {
transform(_ast); transform(*_object.code);
} }
catch (StackTooDeepError const&) catch (StackTooDeepError const&)
{ {

View File

@ -22,6 +22,7 @@
#include <libyul/Dialect.h> #include <libyul/Dialect.h>
#include <libyul/AsmDataForward.h> #include <libyul/AsmDataForward.h>
#include <libyul/Object.h>
#include <map> #include <map>
#include <memory> #include <memory>
@ -33,15 +34,18 @@ namespace yul
* Component that checks whether all variables are reachable on the stack and * Component that checks whether all variables are reachable on the stack and
* returns a mapping from function name to the largest stack difference found * returns a mapping from function name to the largest stack difference found
* in that function (no entry present if that function is compilable). * in that function (no entry present if that function is compilable).
*
* This only works properly if the outermost block is compilable and * This only works properly if the outermost block is compilable and
* functions are not nested. Otherwise, it might miss reporting some functions. * functions are not nested. Otherwise, it might miss reporting some functions.
*
* Only checks the code of the object itself, does not descend into sub-objects.
*/ */
class CompilabilityChecker class CompilabilityChecker
{ {
public: public:
static std::map<YulString, int> run( static std::map<YulString, int> run(
Dialect const& _dialect, Dialect const& _dialect,
Block const& _ast, Object const& _object,
bool _optimizeStackAllocation bool _optimizeStackAllocation
); );
}; };

View File

@ -59,3 +59,14 @@ string Object::toString(bool _yul) const
return "object \"" + name.str() + "\" {\n" + indent(inner) + "\n}"; return "object \"" + name.str() + "\" {\n" + indent(inner) + "\n}";
} }
set<YulString> Object::dataNames() const
{
set<YulString> names;
names.insert(name);
for (auto const& subObject: subIndexByName)
names.insert(subObject.first);
// The empty name is not valid
names.erase(YulString{});
return names;
}

View File

@ -26,6 +26,7 @@
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <memory> #include <memory>
#include <set>
namespace yul namespace yul
{ {
@ -63,6 +64,10 @@ public:
/// @returns a (parseable) string representation. Includes types if @a _yul is set. /// @returns a (parseable) string representation. Includes types if @a _yul is set.
std::string toString(bool _yul) const override; std::string toString(bool _yul) const override;
/// @returns the set of names of data objects accessible from within the code of
/// this object.
std::set<YulString> dataNames() const;
std::shared_ptr<Block> code; std::shared_ptr<Block> code;
std::vector<std::shared_ptr<ObjectNode>> subObjects; std::vector<std::shared_ptr<ObjectNode>> subObjects;
std::map<YulString, size_t> subIndexByName; std::map<YulString, size_t> subIndexByName;

View File

@ -553,7 +553,7 @@ Object EVMToEWasmTranslator::run(Object const& _object)
ErrorList errors; ErrorList errors;
ErrorReporter errorReporter(errors); ErrorReporter errorReporter(errors);
AsmAnalyzer analyzer(*ret.analysisInfo, errorReporter, boost::none, WasmDialect::instance()); AsmAnalyzer analyzer(*ret.analysisInfo, errorReporter, boost::none, WasmDialect::instance(), {}, _object.dataNames());
if (!analyzer.analyze(*ret.code)) if (!analyzer.analyze(*ret.code))
{ {
// TODO the errors here are "wrong" because they have invalid source references! // TODO the errors here are "wrong" because they have invalid source references!

View File

@ -155,19 +155,20 @@ void eliminateVariables(
bool StackCompressor::run( bool StackCompressor::run(
Dialect const& _dialect, Dialect const& _dialect,
Block& _ast, Object& _object,
bool _optimizeStackAllocation, bool _optimizeStackAllocation,
size_t _maxIterations size_t _maxIterations
) )
{ {
yulAssert( yulAssert(
_ast.statements.size() > 0 && _ast.statements.at(0).type() == typeid(Block), _object.code &&
_object.code->statements.size() > 0 && _object.code->statements.at(0).type() == typeid(Block),
"Need to run the function grouper before the stack compressor." "Need to run the function grouper before the stack compressor."
); );
bool allowMSizeOptimzation = !SideEffectsCollector(_dialect, _ast).containsMSize(); bool allowMSizeOptimzation = !SideEffectsCollector(_dialect, *_object.code).containsMSize();
for (size_t iterations = 0; iterations < _maxIterations; iterations++) for (size_t iterations = 0; iterations < _maxIterations; iterations++)
{ {
map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _ast, _optimizeStackAllocation); map<YulString, int> stackSurplus = CompilabilityChecker::run(_dialect, _object, _optimizeStackAllocation);
if (stackSurplus.empty()) if (stackSurplus.empty())
return true; return true;
@ -176,15 +177,15 @@ bool StackCompressor::run(
yulAssert(stackSurplus.at({}) > 0, "Invalid surplus value."); yulAssert(stackSurplus.at({}) > 0, "Invalid surplus value.");
eliminateVariables( eliminateVariables(
_dialect, _dialect,
boost::get<Block>(_ast.statements.at(0)), boost::get<Block>(_object.code->statements.at(0)),
stackSurplus.at({}), stackSurplus.at({}),
allowMSizeOptimzation allowMSizeOptimzation
); );
} }
for (size_t i = 1; i < _ast.statements.size(); ++i) for (size_t i = 1; i < _object.code->statements.size(); ++i)
{ {
FunctionDefinition& fun = boost::get<FunctionDefinition>(_ast.statements[i]); FunctionDefinition& fun = boost::get<FunctionDefinition>(_object.code->statements[i]);
if (!stackSurplus.count(fun.name)) if (!stackSurplus.count(fun.name))
continue; continue;

View File

@ -27,13 +27,15 @@ namespace yul
{ {
struct Dialect; struct Dialect;
struct Block; struct Object;
struct FunctionDefinition; struct FunctionDefinition;
/** /**
* Optimisation stage that aggressively rematerializes certain variables in a function to free * Optimisation stage that aggressively rematerializes certain variables in a function to free
* space on the stack until it is compilable. * space on the stack until it is compilable.
* *
* Only runs on the code of the object itself, does not descend into sub-objects.
*
* Prerequisite: Disambiguator, Function Grouper * Prerequisite: Disambiguator, Function Grouper
*/ */
class StackCompressor class StackCompressor
@ -43,7 +45,7 @@ public:
/// @returns true if it was successful. /// @returns true if it was successful.
static bool run( static bool run(
Dialect const& _dialect, Dialect const& _dialect,
Block& _ast, Object& _object,
bool _optimizeStackAllocation, bool _optimizeStackAllocation,
size_t _maxIterations size_t _maxIterations
); );

View File

@ -49,6 +49,7 @@
#include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h> #include <libyul/AsmData.h>
#include <libyul/AsmPrinter.h> #include <libyul/AsmPrinter.h>
#include <libyul/Object.h>
#include <libyul/backends/wasm/WasmDialect.h> #include <libyul/backends/wasm/WasmDialect.h>
#include <libyul/backends/evm/NoOutputAssembly.h> #include <libyul/backends/evm/NoOutputAssembly.h>
@ -62,8 +63,7 @@ using namespace yul;
void OptimiserSuite::run( void OptimiserSuite::run(
Dialect const& _dialect, Dialect const& _dialect,
GasMeter const* _meter, GasMeter const* _meter,
Block& _ast, Object& _object,
AsmAnalysisInfo const& _analysisInfo,
bool _optimizeStackAllocation, bool _optimizeStackAllocation,
set<YulString> const& _externallyUsedIdentifiers set<YulString> const& _externallyUsedIdentifiers
) )
@ -71,7 +71,12 @@ void OptimiserSuite::run(
set<YulString> reservedIdentifiers = _externallyUsedIdentifiers; set<YulString> reservedIdentifiers = _externallyUsedIdentifiers;
reservedIdentifiers += _dialect.fixedFunctionNames(); reservedIdentifiers += _dialect.fixedFunctionNames();
Block ast = boost::get<Block>(Disambiguator(_dialect, _analysisInfo, reservedIdentifiers)(_ast)); *_object.code = boost::get<Block>(Disambiguator(
_dialect,
*_object.analysisInfo,
reservedIdentifiers
)(*_object.code));
Block& ast = *_object.code;
VarDeclInitializer{}(ast); VarDeclInitializer{}(ast);
FunctionHoister{}(ast); FunctionHoister{}(ast);
@ -204,7 +209,12 @@ void OptimiserSuite::run(
FunctionGrouper{}(ast); FunctionGrouper{}(ast);
// We ignore the return value because we will get a much better error // We ignore the return value because we will get a much better error
// message once we perform code generation. // message once we perform code generation.
StackCompressor::run(_dialect, ast, _optimizeStackAllocation, stackCompressorMaxIterations); StackCompressor::run(
_dialect,
_object,
_optimizeStackAllocation,
stackCompressorMaxIterations
);
BlockFlattener{}(ast); BlockFlattener{}(ast);
DeadCodeEliminator{_dialect}(ast); DeadCodeEliminator{_dialect}(ast);
ControlFlowSimplifier{_dialect}(ast); ControlFlowSimplifier{_dialect}(ast);
@ -224,7 +234,6 @@ void OptimiserSuite::run(
ast.statements.erase(ast.statements.begin()); ast.statements.erase(ast.statements.begin());
} }
VarNameCleaner{ast, _dialect, reservedIdentifiers}(ast); VarNameCleaner{ast, _dialect, reservedIdentifiers}(ast);
yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, ast);
_ast = std::move(ast); *_object.analysisInfo = AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, _object);
} }

View File

@ -32,9 +32,11 @@ namespace yul
struct AsmAnalysisInfo; struct AsmAnalysisInfo;
struct Dialect; struct Dialect;
class GasMeter; class GasMeter;
struct Object;
/** /**
* Optimiser suite that combines all steps and also provides the settings for the heuristics * Optimiser suite that combines all steps and also provides the settings for the heuristics.
* Only optimizes the code of the provided object, does not descend into the sub-objects.
*/ */
class OptimiserSuite class OptimiserSuite
{ {
@ -42,8 +44,7 @@ public:
static void run( static void run(
Dialect const& _dialect, Dialect const& _dialect,
GasMeter const* _meter, GasMeter const* _meter,
Block& _ast, Object& _object,
AsmAnalysisInfo const& _analysisInfo,
bool _optimizeStackAllocation, bool _optimizeStackAllocation,
std::set<YulString> const& _externallyUsedIdentifiers = {} std::set<YulString> const& _externallyUsedIdentifiers = {}
); );

View File

@ -37,9 +37,10 @@ namespace
{ {
string check(string const& _input) string check(string const& _input)
{ {
shared_ptr<Block> ast = yul::test::parse(_input, false).first; Object obj;
BOOST_REQUIRE(ast); std::tie(obj.code, obj.analysisInfo) = yul::test::parse(_input, false);
map<YulString, int> functions = CompilabilityChecker::run(EVMDialect::strictAssemblyForEVM(dev::test::Options::get().evmVersion()), *ast, true); BOOST_REQUIRE(obj.code);
map<YulString, int> functions = CompilabilityChecker::run(EVMDialect::strictAssemblyForEVM(dev::test::Options::get().evmVersion()), obj, true);
string out; string out;
for (auto const& function: functions) for (auto const& function: functions)
out += function.first.str() + ": " + to_string(function.second) + " "; out += function.first.str() + ": " + to_string(function.second) + " ";

View File

@ -278,6 +278,19 @@ BOOST_AUTO_TEST_CASE(args_to_datacopy_are_arbitrary)
BOOST_CHECK(successParse(code)); BOOST_CHECK(successParse(code));
} }
BOOST_AUTO_TEST_CASE(non_existing_objects)
{
BOOST_CHECK(successParse(
"object \"main\" { code { pop(datasize(\"main\")) } }"
));
CHECK_ERROR(
"object \"main\" { code { pop(datasize(\"abc\")) } }",
TypeError,
"Unknown data object"
);
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

View File

@ -574,6 +574,7 @@ BOOST_AUTO_TEST_CASE(builtins_analysis)
CHECK_ERROR_DIALECT("{ let a, b := builtin(1, 2) }", DeclarationError, "Variable count mismatch: 2 variables and 3 values.", dialect); CHECK_ERROR_DIALECT("{ let a, b := builtin(1, 2) }", DeclarationError, "Variable count mismatch: 2 variables and 3 values.", dialect);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }

View File

@ -305,7 +305,10 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
disambiguate(); disambiguate();
(FunctionGrouper{})(*m_ast); (FunctionGrouper{})(*m_ast);
size_t maxIterations = 16; size_t maxIterations = 16;
StackCompressor::run(*m_dialect, *m_ast, true, maxIterations); Object obj;
obj.code = m_ast;
StackCompressor::run(*m_dialect, obj, true, maxIterations);
m_ast = obj.code;
(BlockFlattener{})(*m_ast); (BlockFlattener{})(*m_ast);
} }
else if (m_optimizerStep == "wordSizeTransform") else if (m_optimizerStep == "wordSizeTransform")
@ -318,7 +321,10 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
else if (m_optimizerStep == "fullSuite") else if (m_optimizerStep == "fullSuite")
{ {
GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200); GasMeter meter(dynamic_cast<EVMDialect const&>(*m_dialect), false, 200);
OptimiserSuite::run(*m_dialect, &meter, *m_ast, *m_analysisInfo, true); yul::Object obj;
obj.code = m_ast;
obj.analysisInfo = m_analysisInfo;
OptimiserSuite::run(*m_dialect, &meter, obj, true);
} }
else else
{ {

View File

@ -1,15 +1,18 @@
{ object "main" {
// Arguments to ``datasize`` and ``dataoffset`` need to be code {
// literals. We cannot simplify their arguments, but we can // Arguments to ``datasize`` and ``dataoffset`` need to be
// simplify them as a full expression. // literals. We cannot simplify their arguments, but we can
// ``datacopy`` does not have this restriction. // simplify them as a full expression.
let r := "abc" // ``datacopy`` does not have this restriction.
let a := datasize("abc") let r := "abc"
let x := dataoffset("abc") let a := datasize("abc")
// should be replaced by a let x := dataoffset("abc")
let y := datasize("abc") // should be replaced by a
datacopy("abc", x, y) let y := datasize("abc")
mstore(a, x) datacopy("abc", x, y)
mstore(a, x)
}
data "abc" "Hello, World!"
} }
// ==== // ====
// step: commonSubexpressionEliminator // step: commonSubexpressionEliminator

View File

@ -1,10 +1,13 @@
{ object "main" {
// We should never split arguments to ``dataoffset`` code {
// or ``datasize`` because they need to be literals // We should never split arguments to ``dataoffset``
let x := dataoffset("abc") // or ``datasize`` because they need to be literals
let y := datasize("abc") let x := dataoffset("abc")
// datacopy is fine, though let y := datasize("abc")
datacopy(mload(0), mload(1), mload(2)) // datacopy is fine, though
datacopy(mload(0), mload(1), mload(2))
}
data "abc" "Hello, World!"
} }
// ==== // ====
// step: expressionSplitter // step: expressionSplitter

View File

@ -27,6 +27,7 @@
#include <libyul/AsmData.h> #include <libyul/AsmData.h>
#include <libyul/AsmParser.h> #include <libyul/AsmParser.h>
#include <libyul/AsmPrinter.h> #include <libyul/AsmPrinter.h>
#include <libyul/Object.h>
#include <liblangutil/SourceReferenceFormatter.h> #include <liblangutil/SourceReferenceFormatter.h>
#include <libyul/optimiser/BlockFlattener.h> #include <libyul/optimiser/BlockFlattener.h>
@ -207,8 +208,12 @@ public:
SSAReverser::run(*m_ast); SSAReverser::run(*m_ast);
break; break;
case 'p': case 'p':
StackCompressor::run(m_dialect, *m_ast, true, 16); {
Object obj;
obj.code = m_ast;
StackCompressor::run(m_dialect, obj, true, 16);
break; break;
}
default: default:
cout << "Unknown option." << endl; cout << "Unknown option." << endl;
} }