diff options
Diffstat (limited to 'libyul')
-rw-r--r-- | libyul/AsmAnalysis.cpp | 16 | ||||
-rw-r--r-- | libyul/AsmAnalysis.h | 4 | ||||
-rw-r--r-- | libyul/AsmParser.cpp | 34 | ||||
-rw-r--r-- | libyul/AsmParser.h | 4 | ||||
-rw-r--r-- | libyul/CMakeLists.txt | 1 | ||||
-rw-r--r-- | libyul/Dialect.cpp | 53 | ||||
-rw-r--r-- | libyul/Dialect.h | 37 | ||||
-rw-r--r-- | libyul/Exceptions.h | 1 | ||||
-rw-r--r-- | libyul/ObjectParser.h | 4 | ||||
-rw-r--r-- | libyul/backends/evm/EVMCodeTransform.cpp | 272 | ||||
-rw-r--r-- | libyul/backends/evm/EVMCodeTransform.h | 97 | ||||
-rw-r--r-- | libyul/backends/evm/EVMDialect.cpp | 141 | ||||
-rw-r--r-- | libyul/backends/evm/EVMDialect.h | 85 | ||||
-rw-r--r-- | libyul/backends/evm/EVMObjectCompiler.cpp | 21 | ||||
-rw-r--r-- | libyul/backends/evm/EVMObjectCompiler.h | 11 | ||||
-rw-r--r-- | libyul/optimiser/ASTWalker.cpp | 20 | ||||
-rw-r--r-- | libyul/optimiser/ASTWalker.h | 20 |
17 files changed, 629 insertions, 192 deletions
diff --git a/libyul/AsmAnalysis.cpp b/libyul/AsmAnalysis.cpp index 1be1cf1a..821da005 100644 --- a/libyul/AsmAnalysis.cpp +++ b/libyul/AsmAnalysis.cpp @@ -101,7 +101,7 @@ bool AsmAnalyzer::operator()(Literal const& _literal) } else if (_literal.kind == LiteralKind::Boolean) { - solAssert(m_dialect.flavour == AsmFlavour::Yul, ""); + solAssert(m_dialect->flavour == AsmFlavour::Yul, ""); solAssert(_literal.value == YulString{string("true")} || _literal.value == YulString{string("false")}, ""); } m_info.stackHeightInfo[&_literal] = m_stackHeight; @@ -164,7 +164,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier) bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr) { - solAssert(m_dialect.flavour != AsmFlavour::Yul, ""); + solAssert(m_dialect->flavour != AsmFlavour::Yul, ""); bool success = true; for (auto const& arg: _instr.arguments | boost::adaptors::reversed) if (!expectExpression(arg)) @@ -182,9 +182,9 @@ bool AsmAnalyzer::operator()(ExpressionStatement const& _statement) { int initialStackHeight = m_stackHeight; bool success = boost::apply_visitor(*this, _statement.expression); - if (m_stackHeight != initialStackHeight && (m_dialect.flavour != AsmFlavour::Loose || m_errorTypeForLoose)) + if (m_stackHeight != initialStackHeight && (m_dialect->flavour != AsmFlavour::Loose || m_errorTypeForLoose)) { - Error::Type errorType = m_dialect.flavour == AsmFlavour::Loose ? *m_errorTypeForLoose : Error::Type::TypeError; + Error::Type errorType = m_dialect->flavour == AsmFlavour::Loose ? *m_errorTypeForLoose : Error::Type::TypeError; string msg = "Top-level expressions are not supposed to return values (this expression returns " + to_string(m_stackHeight - initialStackHeight) + @@ -299,7 +299,7 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall) bool success = true; size_t parameters = 0; size_t returns = 0; - if (BuiltinFunction const* f = m_dialect.builtins->query(_funCall.functionName.name)) + if (BuiltinFunction const* f = m_dialect->builtin(_funCall.functionName.name)) { // TODO: compare types, too parameters = f->parameters.size(); @@ -569,7 +569,7 @@ Scope& AsmAnalyzer::scope(Block const* _block) } void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location) { - if (m_dialect.flavour != AsmFlavour::Yul) + if (m_dialect->flavour != AsmFlavour::Yul) return; if (!builtinTypes.count(type)) @@ -629,7 +629,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio if (_instr == solidity::Instruction::JUMP || _instr == solidity::Instruction::JUMPI || _instr == solidity::Instruction::JUMPDEST) { - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) solAssert(m_errorTypeForLoose && *m_errorTypeForLoose != Error::Type::Warning, ""); m_errorReporter.error( @@ -644,7 +644,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio void AsmAnalyzer::checkLooseFeature(SourceLocation const& _location, string const& _description) { - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) solAssert(false, _description); else if (m_errorTypeForLoose) m_errorReporter.error(*m_errorTypeForLoose, _location, _description); diff --git a/libyul/AsmAnalysis.h b/libyul/AsmAnalysis.h index ec2b8868..21cc1142 100644 --- a/libyul/AsmAnalysis.h +++ b/libyul/AsmAnalysis.h @@ -59,7 +59,7 @@ public: langutil::ErrorReporter& _errorReporter, dev::solidity::EVMVersion _evmVersion, boost::optional<langutil::Error::Type> _errorTypeForLoose, - Dialect _dialect = Dialect::looseAssemblyForEVM(), + std::shared_ptr<Dialect> _dialect, ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver() ): m_resolver(_resolver), @@ -115,7 +115,7 @@ private: AsmAnalysisInfo& m_info; langutil::ErrorReporter& m_errorReporter; dev::solidity::EVMVersion m_evmVersion; - Dialect m_dialect = Dialect::looseAssemblyForEVM(); + std::shared_ptr<Dialect> m_dialect; boost::optional<langutil::Error::Type> m_errorTypeForLoose; }; diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index 33bb42f9..c7302063 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -107,14 +107,14 @@ Statement Parser::parseStatement() return parseForLoop(); case Token::Assign: { - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) break; StackAssignment assignment = createWithLocation<StackAssignment>(); advance(); expectToken(Token::Colon); assignment.variableName.location = location(); assignment.variableName.name = YulString(currentLiteral()); - if (m_dialect.builtins->query(assignment.variableName.name)) + if (m_dialect->builtin(assignment.variableName.name)) fatalParserError("Identifier expected, got builtin symbol."); else if (instructions().count(assignment.variableName.name.str())) fatalParserError("Identifier expected, got instruction name."); @@ -176,9 +176,9 @@ Statement Parser::parseStatement() if (currentToken() == Token::Assign && peekNextToken() != Token::Colon) { Assignment assignment = createWithLocation<Assignment>(identifier.location); - if (m_dialect.builtins->query(identifier.name)) + if (m_dialect->builtin(identifier.name)) fatalParserError("Cannot assign to builtin function \"" + identifier.name.str() + "\"."); - else if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(identifier.name.str())) + else if (m_dialect->flavour != AsmFlavour::Yul && instructions().count(identifier.name.str())) fatalParserError("Cannot use instruction names for identifier names."); advance(); assignment.variableNames.emplace_back(identifier); @@ -189,7 +189,7 @@ Statement Parser::parseStatement() else { // label - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) fatalParserError("Labels are not supported."); Label label = createWithLocation<Label>(identifier.location); label.name = identifier.name; @@ -197,7 +197,7 @@ Statement Parser::parseStatement() } } default: - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) fatalParserError("Call or assignment expected."); break; } @@ -273,7 +273,7 @@ Expression Parser::parseExpression() instructionNames().at(instr.instruction) + "\" not allowed in this context." ); - if (m_dialect.flavour != AsmFlavour::Loose && currentToken() != Token::LParen) + if (m_dialect->flavour != AsmFlavour::Loose && currentToken() != Token::LParen) fatalParserError( "Non-functional instructions are not allowed in this context." ); @@ -293,7 +293,7 @@ Expression Parser::parseExpression() else if (operation.type() == typeid(Instruction)) { // Instructions not taking arguments are allowed as expressions. - solAssert(m_dialect.flavour == AsmFlavour::Loose, ""); + solAssert(m_dialect->flavour == AsmFlavour::Loose, ""); Instruction& instr = boost::get<Instruction>(operation); return FunctionalInstruction{std::move(instr.location), instr.instruction, {}}; } @@ -362,9 +362,9 @@ Parser::ElementaryOperation Parser::parseElementaryOperation() else literal = YulString{currentLiteral()}; // first search the set of builtins, then the instructions. - if (m_dialect.builtins->query(literal)) + if (m_dialect->builtin(literal)) ret = Identifier{location(), literal}; - else if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(literal.str())) + else if (m_dialect->flavour != AsmFlavour::Yul && instructions().count(literal.str())) { dev::solidity::Instruction const& instr = instructions().at(literal.str()); ret = Instruction{location(), instr}; @@ -405,7 +405,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation() {} }; advance(); - if (m_dialect.flavour == AsmFlavour::Yul) + if (m_dialect->flavour == AsmFlavour::Yul) { expectToken(Token::Colon); literal.location.end = endPosition(); @@ -418,7 +418,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation() } default: fatalParserError( - m_dialect.flavour == AsmFlavour::Yul ? + m_dialect->flavour == AsmFlavour::Yul ? "Literal or identifier expected." : "Literal, identifier or instruction expected." ); @@ -488,7 +488,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp) RecursionGuard recursionGuard(*this); if (_initialOp.type() == typeid(Instruction)) { - solAssert(m_dialect.flavour != AsmFlavour::Yul, "Instructions are invalid in Yul"); + solAssert(m_dialect->flavour != AsmFlavour::Yul, "Instructions are invalid in Yul"); Instruction& instruction = boost::get<Instruction>(_initialOp); FunctionalInstruction ret; ret.instruction = instruction.instruction; @@ -559,7 +559,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp) } else fatalParserError( - m_dialect.flavour == AsmFlavour::Yul ? + m_dialect->flavour == AsmFlavour::Yul ? "Function name expected." : "Assembly instruction or function name required in front of \"(\")" ); @@ -572,7 +572,7 @@ TypedName Parser::parseTypedName() RecursionGuard recursionGuard(*this); TypedName typedName = createWithLocation<TypedName>(); typedName.name = expectAsmIdentifier(); - if (m_dialect.flavour == AsmFlavour::Yul) + if (m_dialect->flavour == AsmFlavour::Yul) { expectToken(Token::Colon); typedName.location.end = endPosition(); @@ -584,7 +584,7 @@ TypedName Parser::parseTypedName() YulString Parser::expectAsmIdentifier() { YulString name = YulString{currentLiteral()}; - if (m_dialect.flavour == AsmFlavour::Yul) + if (m_dialect->flavour == AsmFlavour::Yul) { switch (currentToken()) { @@ -598,7 +598,7 @@ YulString Parser::expectAsmIdentifier() break; } } - else if (m_dialect.builtins->query(name)) + else if (m_dialect->builtin(name)) fatalParserError("Cannot use builtin function name \"" + name.str() + "\" as identifier name."); else if (instructions().count(name.str())) fatalParserError("Cannot use instruction names for identifier names."); diff --git a/libyul/AsmParser.h b/libyul/AsmParser.h index b40a717c..a02f0b4d 100644 --- a/libyul/AsmParser.h +++ b/libyul/AsmParser.h @@ -38,7 +38,7 @@ namespace yul class Parser: public langutil::ParserBase { public: - explicit Parser(langutil::ErrorReporter& _errorReporter, Dialect _dialect = Dialect::looseAssemblyForEVM()): + explicit Parser(langutil::ErrorReporter& _errorReporter, std::shared_ptr<Dialect> _dialect): ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {} /// Parses an inline assembly block starting with `{` and ending with `}`. @@ -86,7 +86,7 @@ protected: static bool isValidNumberLiteral(std::string const& _literal); private: - Dialect m_dialect = Dialect::looseAssemblyForEVM(); + std::shared_ptr<Dialect> m_dialect; }; } diff --git a/libyul/CMakeLists.txt b/libyul/CMakeLists.txt index 7d1ec6ba..74a5703c 100644 --- a/libyul/CMakeLists.txt +++ b/libyul/CMakeLists.txt @@ -10,6 +10,7 @@ add_library(yul ObjectParser.cpp backends/evm/EVMAssembly.cpp backends/evm/EVMCodeTransform.cpp + backends/evm/EVMDialect.cpp backends/evm/EVMObjectCompiler.cpp optimiser/ASTCopier.cpp optimiser/ASTWalker.cpp diff --git a/libyul/Dialect.cpp b/libyul/Dialect.cpp index 99718787..f6985c17 100644 --- a/libyul/Dialect.cpp +++ b/libyul/Dialect.cpp @@ -20,57 +20,10 @@ #include <libyul/Dialect.h> +#include <libyul/Object.h> +#include <libyul/backends/evm/AbstractAssembly.h> + #include <map> using namespace yul; using namespace std; - -namespace -{ - -void addFunction( - map<YulString, BuiltinFunction>& _repository, - string const& _name, - size_t _params, - size_t _returns, - bool _movable -) -{ - _repository[YulString{_name}] = BuiltinFunction{ - YulString{_name}, - vector<Type>(_params), - vector<Type>(_returns), - _movable - }; -} - -class GenericBuiltins: public Builtins -{ -public: - GenericBuiltins(map<YulString, BuiltinFunction> const& _functions): m_functions(_functions) {} - BuiltinFunction const* query(YulString _name) const - { - auto it = m_functions.find(_name); - if (it != end(m_functions)) - return &it->second; - else - return nullptr; - } -private: - map<YulString, BuiltinFunction> const& m_functions; -}; - -} - -Dialect Dialect::strictAssemblyForEVMObjects() -{ - static map<YulString, BuiltinFunction> functions; - if (functions.empty()) - { - addFunction(functions, "datasize", 1, 1, true); - addFunction(functions, "dataoffset", 1, 1, true); - addFunction(functions, "datacopy", 3, 0, false); - } - // The EVM instructions will be moved to builtins at some point. - return Dialect{AsmFlavour::Strict, std::make_shared<GenericBuiltins>(functions)}; -} diff --git a/libyul/Dialect.h b/libyul/Dialect.h index b78f1aaf..2def566c 100644 --- a/libyul/Dialect.h +++ b/libyul/Dialect.h @@ -22,7 +22,9 @@ #include <libyul/YulString.h> -#include <memory> +#include <boost/noncopyable.hpp> + +#include <vector> namespace yul { @@ -45,38 +47,19 @@ struct BuiltinFunction bool movable; }; -/** - * Class to query for builtin functions and their semantics. - */ -struct Builtins +struct Dialect: boost::noncopyable { - virtual ~Builtins() = default; + AsmFlavour const flavour = AsmFlavour::Loose; /// @returns the builtin function of the given name or a nullptr if it is not a builtin function. - virtual BuiltinFunction const* query(YulString /*_name*/) const { return nullptr; } -}; + virtual BuiltinFunction const* builtin(YulString /*_name*/) const { return nullptr; } -struct Dialect -{ - AsmFlavour flavour = AsmFlavour::Loose; - std::shared_ptr<Builtins> builtins; + Dialect(AsmFlavour _flavour): flavour(_flavour) {} + virtual ~Dialect() {} - Dialect(AsmFlavour _flavour, std::shared_ptr<Builtins> _builtins): - flavour(_flavour), builtins(std::move(_builtins)) - {} - static Dialect looseAssemblyForEVM() - { - return Dialect{AsmFlavour::Loose, std::make_shared<Builtins>()}; - } - static Dialect strictAssemblyForEVM() - { - // The EVM instructions will be moved to builtins at some point. - return Dialect{AsmFlavour::Strict, std::make_shared<Builtins>()}; - } - static Dialect strictAssemblyForEVMObjects(); - static Dialect yul() + static std::shared_ptr<Dialect> yul() { // Will have to add builtins later. - return Dialect{AsmFlavour::Yul, std::make_shared<Builtins>()}; + return std::make_shared<Dialect>(AsmFlavour::Yul); } }; diff --git a/libyul/Exceptions.h b/libyul/Exceptions.h index e10e53ef..d1f1fe96 100644 --- a/libyul/Exceptions.h +++ b/libyul/Exceptions.h @@ -28,6 +28,7 @@ namespace yul struct YulException: virtual dev::Exception {}; struct OptimizerException: virtual YulException {}; +struct CodegenException: virtual YulException {}; struct YulAssertion: virtual YulException {}; /// Assertion that throws an YulAssertion containing the given description if it is not met. diff --git a/libyul/ObjectParser.h b/libyul/ObjectParser.h index 59efb8ab..e39f24cd 100644 --- a/libyul/ObjectParser.h +++ b/libyul/ObjectParser.h @@ -47,7 +47,7 @@ class ObjectParser: public langutil::ParserBase public: explicit ObjectParser( langutil::ErrorReporter& _errorReporter, - Dialect _dialect = Dialect::looseAssemblyForEVM() + std::shared_ptr<Dialect> _dialect ): ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {} @@ -67,7 +67,7 @@ private: YulString parseUniqueName(Object const* _containingObject); void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject); - Dialect m_dialect; + std::shared_ptr<Dialect> m_dialect; }; } diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp index 12abd754..bd18985c 100644 --- a/libyul/backends/evm/EVMCodeTransform.cpp +++ b/libyul/backends/evm/EVMCodeTransform.cpp @@ -20,6 +20,7 @@ #include <libyul/backends/evm/EVMCodeTransform.h> +#include <libyul/optimiser/NameCollector.h> #include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmData.h> @@ -32,6 +33,144 @@ using namespace dev; using namespace yul; using namespace dev::solidity; +void VariableReferenceCounter::operator()(Identifier const& _identifier) +{ + increaseRefIfFound(_identifier.name); +} + +void VariableReferenceCounter::operator()(FunctionDefinition const& _function) +{ + Scope* originalScope = m_scope; + + solAssert(m_info.virtualBlocks.at(&_function), ""); + m_scope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get(); + solAssert(m_scope, "Variable scope does not exist."); + + for (auto const& v: _function.returnVariables) + increaseRefIfFound(v.name); + + VariableReferenceCounter{m_context, m_info}(_function.body); + + m_scope = originalScope; +} + +void VariableReferenceCounter::operator()(ForLoop const& _forLoop) +{ + Scope* originalScope = m_scope; + // Special scoping rules. + m_scope = m_info.scopes.at(&_forLoop.pre).get(); + + walkVector(_forLoop.pre.statements); + visit(*_forLoop.condition); + (*this)(_forLoop.body); + (*this)(_forLoop.post); + + m_scope = originalScope; +} + + +void VariableReferenceCounter::operator()(Block const& _block) +{ + Scope* originalScope = m_scope; + m_scope = m_info.scopes.at(&_block).get(); + + ASTWalker::operator()(_block); + + m_scope = originalScope; +} + +void VariableReferenceCounter::increaseRefIfFound(YulString _variableName) +{ + m_scope->lookup(_variableName, Scope::Visitor( + [=](Scope::Variable const& _var) + { + ++m_context.variableReferences[&_var]; + }, + [=](Scope::Label const&) { }, + [=](Scope::Function const&) { } + )); +} + + +CodeTransform::CodeTransform( + AbstractAssembly& _assembly, + AsmAnalysisInfo& _analysisInfo, + Block const& _block, + bool _allowStackOpt, + EVMDialect const& _dialect, + bool _evm15, + ExternalIdentifierAccess const& _identifierAccess, + bool _useNamedLabelsForFunctions, + int _stackAdjustment, + shared_ptr<Context> _context +): + m_assembly(_assembly), + m_info(_analysisInfo), + m_dialect(_dialect), + m_allowStackOpt(_allowStackOpt), + m_evm15(_evm15), + m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions), + m_identifierAccess(_identifierAccess), + m_stackAdjustment(_stackAdjustment), + m_context(_context) +{ + if (!m_context) + { + // initialize + m_context = make_shared<Context>(); + if (m_allowStackOpt) + VariableReferenceCounter{*m_context, m_info}(_block); + } +} + +void CodeTransform::decreaseReference(YulString, Scope::Variable const& _var) +{ + if (!m_allowStackOpt) + return; + + unsigned& ref = m_context->variableReferences.at(&_var); + solAssert(ref >= 1, ""); + --ref; + if (ref == 0) + m_variablesScheduledForDeletion.insert(&_var); +} + +bool CodeTransform::unreferenced(Scope::Variable const& _var) const +{ + return !m_context->variableReferences.count(&_var) || m_context->variableReferences[&_var] == 0; +} + +void CodeTransform::freeUnusedVariables() +{ + if (!m_allowStackOpt) + return; + + for (auto const& identifier: m_scope->identifiers) + if (identifier.second.type() == typeid(Scope::Variable)) + { + Scope::Variable const& var = boost::get<Scope::Variable>(identifier.second); + if (m_variablesScheduledForDeletion.count(&var)) + deleteVariable(var); + } + + while (m_unusedStackSlots.count(m_assembly.stackHeight() - 1)) + { + solAssert(m_unusedStackSlots.erase(m_assembly.stackHeight() - 1), ""); + m_assembly.appendInstruction(solidity::Instruction::POP); + --m_stackAdjustment; + } +} + +void CodeTransform::deleteVariable(Scope::Variable const& _var) +{ + solAssert(m_allowStackOpt, ""); + solAssert(m_context->variableStackHeights.count(&_var) > 0, ""); + m_unusedStackSlots.insert(m_context->variableStackHeights[&_var]); + m_context->variableStackHeights.erase(&_var); + m_context->variableReferences.erase(&_var); + m_variablesScheduledForDeletion.erase(&_var); +} + void CodeTransform::operator()(VariableDeclaration const& _varDecl) { solAssert(m_scope, ""); @@ -49,10 +188,40 @@ void CodeTransform::operator()(VariableDeclaration const& _varDecl) while (variablesLeft--) m_assembly.appendConstant(u256(0)); } - for (auto const& variable: _varDecl.variables) + + bool atTopOfStack = true; + for (int varIndex = numVariables - 1; varIndex >= 0; --varIndex) { - auto& var = boost::get<Scope::Variable>(m_scope->identifiers.at(variable.name)); - m_context->variableStackHeights[&var] = height++; + auto& var = boost::get<Scope::Variable>(m_scope->identifiers.at(_varDecl.variables[varIndex].name)); + m_context->variableStackHeights[&var] = height + varIndex; + if (!m_allowStackOpt) + continue; + + if (unreferenced(var)) + { + if (atTopOfStack) + { + m_context->variableStackHeights.erase(&var); + m_assembly.setSourceLocation(_varDecl.location); + m_assembly.appendInstruction(solidity::Instruction::POP); + --m_stackAdjustment; + } + else + m_variablesScheduledForDeletion.insert(&var); + } + else if (m_unusedStackSlots.empty()) + atTopOfStack = false; + else + { + int slot = *m_unusedStackSlots.begin(); + m_unusedStackSlots.erase(m_unusedStackSlots.begin()); + m_context->variableStackHeights[&var] = slot; + m_assembly.setSourceLocation(_varDecl.location); + if (int heightDiff = variableHeightDiff(var, true)) + m_assembly.appendInstruction(solidity::swapInstruction(heightDiff - 1)); + m_assembly.appendInstruction(solidity::Instruction::POP); + --m_stackAdjustment; + } } checkStackHeight(&_varDecl); } @@ -70,6 +239,7 @@ void CodeTransform::operator()(Assignment const& _assignment) void CodeTransform::operator()(StackAssignment const& _assignment) { + solAssert(!m_allowStackOpt, ""); m_assembly.setSourceLocation(_assignment.location); generateAssignment(_assignment.variableName); checkStackHeight(&_assignment); @@ -84,6 +254,7 @@ void CodeTransform::operator()(ExpressionStatement const& _statement) void CodeTransform::operator()(Label const& _label) { + solAssert(!m_allowStackOpt, ""); m_assembly.setSourceLocation(_label.location); solAssert(m_scope, ""); solAssert(m_scope->identifiers.count(_label.name), ""); @@ -96,35 +267,46 @@ void CodeTransform::operator()(FunctionCall const& _call) { solAssert(m_scope, ""); - m_assembly.setSourceLocation(_call.location); - EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0 - if (!m_evm15) + if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name)) { - returnLabel = m_assembly.newLabelId(); - m_assembly.appendLabelReference(returnLabel); - m_stackAdjustment++; + builtin->generateCode(_call, m_assembly, [&]() { + for (auto const& arg: _call.arguments | boost::adaptors::reversed) + visitExpression(arg); + m_assembly.setSourceLocation(_call.location); + }); } - - Scope::Function* function = nullptr; - solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor( - [=](Scope::Variable&) { solAssert(false, "Expected function name."); }, - [=](Scope::Label&) { solAssert(false, "Expected function name."); }, - [&](Scope::Function& _function) { function = &_function; } - )), "Function name not found."); - solAssert(function, ""); - solAssert(function->arguments.size() == _call.arguments.size(), ""); - for (auto const& arg: _call.arguments | boost::adaptors::reversed) - visitExpression(arg); - m_assembly.setSourceLocation(_call.location); - if (m_evm15) - m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size()); else { - m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1); - m_assembly.appendLabel(returnLabel); - m_stackAdjustment--; + m_assembly.setSourceLocation(_call.location); + EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0 + if (!m_evm15) + { + returnLabel = m_assembly.newLabelId(); + m_assembly.appendLabelReference(returnLabel); + m_stackAdjustment++; + } + + Scope::Function* function = nullptr; + solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor( + [=](Scope::Variable&) { solAssert(false, "Expected function name."); }, + [=](Scope::Label&) { solAssert(false, "Expected function name."); }, + [&](Scope::Function& _function) { function = &_function; } + )), "Function name not found."); + solAssert(function, ""); + solAssert(function->arguments.size() == _call.arguments.size(), ""); + for (auto const& arg: _call.arguments | boost::adaptors::reversed) + visitExpression(arg); + m_assembly.setSourceLocation(_call.location); + if (m_evm15) + m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size()); + else + { + m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1); + m_assembly.appendLabel(returnLabel); + m_stackAdjustment--; + } + checkStackHeight(&_call); } - checkStackHeight(&_call); } void CodeTransform::operator()(FunctionalInstruction const& _instruction) @@ -169,11 +351,14 @@ void CodeTransform::operator()(Identifier const& _identifier) if (m_scope->lookup(_identifier.name, Scope::NonconstVisitor( [=](Scope::Variable& _var) { + // TODO: opportunity for optimization: Do not DUP if this is the last reference + // to the top most element of the stack if (int heightDiff = variableHeightDiff(_var, false)) m_assembly.appendInstruction(solidity::dupInstruction(heightDiff)); else // Store something to balance the stack m_assembly.appendConstant(u256(0)); + decreaseReference(_identifier.name, _var); }, [=](Scope::Label& _label) { @@ -217,6 +402,7 @@ void CodeTransform::operator()(Literal const& _literal) void CodeTransform::operator()(yul::Instruction const& _instruction) { + solAssert(!m_allowStackOpt, ""); solAssert(!m_evm15 || _instruction.instruction != solidity::Instruction::JUMP, "Bare JUMP instruction used for EVM1.5"); solAssert(!m_evm15 || _instruction.instruction != solidity::Instruction::JUMPI, "Bare JUMPI instruction used for EVM1.5"); m_assembly.setSourceLocation(_instruction.location); @@ -329,7 +515,9 @@ void CodeTransform::operator()(FunctionDefinition const& _function) CodeTransform( m_assembly, m_info, - m_yul, + _function.body, + m_allowStackOpt, + m_dialect, m_evm15, m_identifierAccess, m_useNamedLabelsForFunctions, @@ -350,6 +538,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) if (!m_evm15) stackLayout.push_back(_function.returnVariables.size()); // Move return label to the top stackLayout += vector<int>(_function.parameters.size(), -1); // discard all arguments + for (size_t i = 0; i < _function.returnVariables.size(); ++i) stackLayout.push_back(i); // Move return values down, but keep order. @@ -475,20 +664,37 @@ void CodeTransform::visitExpression(Expression const& _expression) void CodeTransform::visitStatements(vector<Statement> const& _statements) { for (auto const& statement: _statements) + { + freeUnusedVariables(); boost::apply_visitor(*this, statement); + } + freeUnusedVariables(); } void CodeTransform::finalizeBlock(Block const& _block, int blockStartStackHeight) { m_assembly.setSourceLocation(_block.location); + freeUnusedVariables(); + // pop variables solAssert(m_info.scopes.at(&_block).get() == m_scope, ""); - for (size_t i = 0; i < m_scope->numberOfVariables(); ++i) - m_assembly.appendInstruction(solidity::Instruction::POP); + for (auto const& id: m_scope->identifiers) + if (id.second.type() == typeid(Scope::Variable)) + { + Scope::Variable const& var = boost::get<Scope::Variable>(id.second); + if (m_allowStackOpt) + { + solAssert(!m_context->variableStackHeights.count(&var), ""); + solAssert(!m_context->variableReferences.count(&var), ""); + m_stackAdjustment++; + } + else + m_assembly.appendInstruction(solidity::Instruction::POP); + } int deposit = m_assembly.stackHeight() - blockStartStackHeight; - solAssert(deposit == 0, "Invalid stack height at end of block."); + solAssert(deposit == 0, "Invalid stack height at end of block: " + to_string(deposit)); checkStackHeight(&_block); } @@ -502,13 +708,13 @@ void CodeTransform::generateMultiAssignment(vector<Identifier> const& _variableN void CodeTransform::generateAssignment(Identifier const& _variableName) { solAssert(m_scope, ""); - auto var = m_scope->lookup(_variableName.name); - if (var) + if (auto var = m_scope->lookup(_variableName.name)) { Scope::Variable const& _var = boost::get<Scope::Variable>(*var); if (int heightDiff = variableHeightDiff(_var, true)) m_assembly.appendInstruction(solidity::swapInstruction(heightDiff - 1)); m_assembly.appendInstruction(solidity::Instruction::POP); + decreaseReference(_variableName.name, _var); } else { diff --git a/libyul/backends/evm/EVMCodeTransform.h b/libyul/backends/evm/EVMCodeTransform.h index d559f85a..28ef4e45 100644 --- a/libyul/backends/evm/EVMCodeTransform.h +++ b/libyul/backends/evm/EVMCodeTransform.h @@ -20,8 +20,9 @@ #include <libyul/backends/evm/EVMAssembly.h> +#include <libyul/backends/evm/EVMDialect.h> +#include <libyul/optimiser/ASTWalker.h> #include <libyul/AsmDataForward.h> - #include <libyul/AsmScope.h> #include <boost/variant.hpp> @@ -37,6 +38,46 @@ namespace yul struct AsmAnalysisInfo; class EVMAssembly; +struct CodeTransformContext +{ + std::map<Scope::Label const*, AbstractAssembly::LabelID> labelIDs; + std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs; + std::map<Scope::Variable const*, int> variableStackHeights; + std::map<Scope::Variable const*, unsigned> variableReferences; +}; + +/** + * Counts the number of references to a variable. This includes actual (read) references + * but also assignments to the variable. It does not include the declaration itself or + * function parameters, but it does include function return parameters. + * + * This component can handle multiple variables of the same name. + * + * Can only be applied to strict assembly. + */ +class VariableReferenceCounter: public yul::ASTWalker +{ +public: + explicit VariableReferenceCounter( + CodeTransformContext& _context, + AsmAnalysisInfo const& _assemblyInfo + ): m_context(_context), m_info(_assemblyInfo) + {} + +public: + void operator()(Identifier const& _identifier); + void operator()(FunctionDefinition const&); + void operator()(ForLoop const&); + void operator()(Block const& _block); + +private: + void increaseRefIfFound(YulString _variableName); + + CodeTransformContext& m_context; + AsmAnalysisInfo const& m_info; + Scope* m_scope = nullptr; +}; + class CodeTransform: public boost::static_visitor<> { public: @@ -45,50 +86,51 @@ public: CodeTransform( AbstractAssembly& _assembly, AsmAnalysisInfo& _analysisInfo, - bool _yul = false, + Block const& _block, + EVMDialect const& _dialect, + bool _allowStackOpt = false, bool _evm15 = false, ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(), bool _useNamedLabelsForFunctions = false ): CodeTransform( _assembly, _analysisInfo, - _yul, + _block, + _allowStackOpt, + _dialect, _evm15, _identifierAccess, _useNamedLabelsForFunctions, _assembly.stackHeight(), - std::make_shared<Context>() + nullptr ) { } protected: - struct Context - { - std::map<Scope::Label const*, AbstractAssembly::LabelID> labelIDs; - std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs; - std::map<Scope::Variable const*, int> variableStackHeights; - }; + using Context = CodeTransformContext; CodeTransform( AbstractAssembly& _assembly, AsmAnalysisInfo& _analysisInfo, - bool _yul, + Block const& _block, + bool _allowStackOpt, + EVMDialect const& _dialect, bool _evm15, ExternalIdentifierAccess const& _identifierAccess, bool _useNamedLabelsForFunctions, int _stackAdjustment, std::shared_ptr<Context> _context - ): - m_assembly(_assembly), - m_info(_analysisInfo), - m_yul(_yul), - m_evm15(_evm15), - m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions), - m_identifierAccess(_identifierAccess), - m_stackAdjustment(_stackAdjustment), - m_context(_context) - {} + ); + + void decreaseReference(YulString _name, Scope::Variable const& _var); + bool unreferenced(Scope::Variable const& _var) const; + /// Marks slots of variables that are not used anymore + /// and were defined in the current scope for reuse. + /// Also POPs unused topmost stack slots. + void freeUnusedVariables(); + /// Marks the stack slot of @a _var to be reused. + void deleteVariable(Scope::Variable const& _var); public: void operator()(Instruction const& _instruction); @@ -137,9 +179,10 @@ private: AbstractAssembly& m_assembly; AsmAnalysisInfo& m_info; Scope* m_scope = nullptr; - bool m_yul = false; - bool m_evm15 = false; - bool m_useNamedLabelsForFunctions = false; + EVMDialect const& m_dialect; + bool const m_allowStackOpt = true; + bool const m_evm15 = false; + bool const m_useNamedLabelsForFunctions = false; ExternalIdentifierAccess m_identifierAccess; /// Adjustment between the stack height as determined during the analysis phase /// and the stack height in the assembly. This is caused by an initial stack being present @@ -147,6 +190,12 @@ private: /// (EVM 1.0 or 1.5). int m_stackAdjustment = 0; std::shared_ptr<Context> m_context; + + /// Set of variables whose reference counter has reached zero, + /// and whose stack slot will be marked as unused once we reach + /// statement level in the scope where the variable was defined. + std::set<Scope::Variable const*> m_variablesScheduledForDeletion; + std::set<int> m_unusedStackSlots; }; } diff --git a/libyul/backends/evm/EVMDialect.cpp b/libyul/backends/evm/EVMDialect.cpp new file mode 100644 index 00000000..33ee19d4 --- /dev/null +++ b/libyul/backends/evm/EVMDialect.cpp @@ -0,0 +1,141 @@ +/* + 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/>. +*/ +/** + * Yul dialects for EVM. + */ + +#include <libyul/backends/evm/EVMDialect.h> + +#include <libyul/AsmAnalysisInfo.h> +#include <libyul/AsmData.h> +#include <libyul/Object.h> +#include <libyul/backends/evm/AbstractAssembly.h> + +#include <liblangutil/Exceptions.h> + +#include <libyul/Exceptions.h> + +#include <boost/range/adaptor/reversed.hpp> + +using namespace std; +using namespace dev; +using namespace yul; +using namespace dev::solidity; + + +EVMDialect::EVMDialect(AsmFlavour _flavour, bool _objectAccess): + Dialect(_flavour), m_objectAccess(_objectAccess) +{ + // The EVM instructions will be moved to builtins at some point. + if (!m_objectAccess) + return; + + addFunction("datasize", 1, 1, true, [this]( + FunctionCall const& _call, + AbstractAssembly& _assembly, + std::function<void()> + ) { + yulAssert(m_currentObject, "No object available."); + yulAssert(_call.arguments.size() == 1, ""); + Expression const& arg = _call.arguments.front(); + YulString dataName = boost::get<Literal>(arg).value; + if (m_currentObject->name == dataName) + _assembly.appendAssemblySize(); + else + _assembly.appendDataSize(m_subIDs.at(dataName)); + }); + addFunction("dataoffset", 1, 1, true, [this]( + FunctionCall const& _call, + AbstractAssembly& _assembly, + std::function<void()> + ) { + yulAssert(m_currentObject, "No object available."); + yulAssert(_call.arguments.size() == 1, ""); + Expression const& arg = _call.arguments.front(); + YulString dataName = boost::get<Literal>(arg).value; + if (m_currentObject->name == dataName) + _assembly.appendConstant(0); + else + _assembly.appendDataOffset(m_subIDs.at(dataName)); + }); + addFunction("datacopy", 3, 0, false, []( + FunctionCall const&, + AbstractAssembly& _assembly, + std::function<void()> _visitArguments + ) { + _visitArguments(); + _assembly.appendInstruction(solidity::Instruction::CODECOPY); + }); +} + +BuiltinFunctionForEVM const* EVMDialect::builtin(YulString _name) const +{ + auto it = m_functions.find(_name); + if (it != m_functions.end()) + return &it->second; + else + return nullptr; +} + +shared_ptr<EVMDialect> EVMDialect::looseAssemblyForEVM() +{ + return make_shared<EVMDialect>(AsmFlavour::Loose, false); +} + +shared_ptr<EVMDialect> EVMDialect::strictAssemblyForEVM() +{ + return make_shared<EVMDialect>(AsmFlavour::Strict, false); +} + +shared_ptr<EVMDialect> EVMDialect::strictAssemblyForEVMObjects() +{ + return make_shared<EVMDialect>(AsmFlavour::Strict, true); +} + +shared_ptr<yul::EVMDialect> EVMDialect::yulForEVM() +{ + return make_shared<EVMDialect>(AsmFlavour::Yul, false); +} + +void EVMDialect::setSubIDs(map<YulString, AbstractAssembly::SubID> _subIDs) +{ + yulAssert(m_objectAccess, "Sub IDs set with dialect that does not support object access."); + m_subIDs = std::move(_subIDs); +} + +void EVMDialect::setCurrentObject(Object const* _object) +{ + yulAssert(m_objectAccess, "Current object set with dialect that does not support object access."); + m_currentObject = _object; +} + +void EVMDialect::addFunction( + string _name, + size_t _params, + size_t _returns, + bool _movable, + std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode +) +{ + YulString name{std::move(_name)}; + BuiltinFunctionForEVM& f = m_functions[name]; + f.name = name; + f.parameters.resize(_params); + f.returns.resize(_returns); + f.movable = _movable; + f.generateCode = std::move(_generateCode); +} diff --git a/libyul/backends/evm/EVMDialect.h b/libyul/backends/evm/EVMDialect.h new file mode 100644 index 00000000..feb00b03 --- /dev/null +++ b/libyul/backends/evm/EVMDialect.h @@ -0,0 +1,85 @@ +/* + 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/>. +*/ +/** + * Yul dialects for EVM. + */ + +#pragma once + +#include <libyul/Dialect.h> + +#include <libyul/backends/evm/AbstractAssembly.h> + +#include <map> + +namespace yul +{ + +class YulString; +using Type = YulString; +struct FunctionCall; +struct Object; + +struct BuiltinFunctionForEVM: BuiltinFunction +{ + /// Function to generate code for the given function call and append it to the abstract + /// assembly. The third parameter is called to visit (and generate code for) the arguments + /// from right to left. + std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> generateCode; +}; + +/** + * Yul dialect for EVM as a backend. + * The main difference is that the builtin functions take an AbstractAssembly for the + * code generation. + */ +struct EVMDialect: public Dialect +{ + EVMDialect(AsmFlavour _flavour, bool _objectAccess); + + /// @returns the builtin function of the given name or a nullptr if it is not a builtin function. + BuiltinFunctionForEVM const* builtin(YulString _name) const override; + + static std::shared_ptr<EVMDialect> looseAssemblyForEVM(); + static std::shared_ptr<EVMDialect> strictAssemblyForEVM(); + static std::shared_ptr<EVMDialect> strictAssemblyForEVMObjects(); + static std::shared_ptr<EVMDialect> yulForEVM(); + + bool providesObjectAccess() const { return m_objectAccess; } + + /// Sets the mapping of current sub assembly IDs. Used during code generation. + void setSubIDs(std::map<YulString, AbstractAssembly::SubID> _subIDs); + /// Sets the current object. Used during code generation. + void setCurrentObject(Object const* _object); + +private: + void addFunction( + std::string _name, + size_t _params, + size_t _returns, + bool _movable, + std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode + ); + + bool m_objectAccess; + Object const* m_currentObject = nullptr; + /// Mapping from named objects to abstract assembly sub IDs. + std::map<YulString, AbstractAssembly::SubID> m_subIDs; + std::map<YulString, BuiltinFunctionForEVM> m_functions; +}; + +} diff --git a/libyul/backends/evm/EVMObjectCompiler.cpp b/libyul/backends/evm/EVMObjectCompiler.cpp index e7e8ad99..3f7634b2 100644 --- a/libyul/backends/evm/EVMObjectCompiler.cpp +++ b/libyul/backends/evm/EVMObjectCompiler.cpp @@ -21,19 +21,21 @@ #include <libyul/backends/evm/EVMObjectCompiler.h> #include <libyul/backends/evm/EVMCodeTransform.h> +#include <libyul/backends/evm/EVMDialect.h> + #include <libyul/Object.h> #include <libyul/Exceptions.h> using namespace yul; using namespace std; -void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, bool _yul, bool _evm15) +void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize) { - EVMObjectCompiler compiler(_assembly, _yul, _evm15); - compiler.run(_object); + EVMObjectCompiler compiler(_assembly, _dialect, _evm15); + compiler.run(_object, _optimize); } -void EVMObjectCompiler::run(Object& _object) +void EVMObjectCompiler::run(Object& _object, bool _optimize) { map<YulString, AbstractAssembly::SubID> subIDs; @@ -42,7 +44,7 @@ void EVMObjectCompiler::run(Object& _object) { auto subAssemblyAndID = m_assembly.createSubAssembly(); subIDs[subObject->name] = subAssemblyAndID.second; - compile(*subObject, *subAssemblyAndID.first, m_yul, m_evm15); + compile(*subObject, *subAssemblyAndID.first, m_dialect, m_evm15, _optimize); } else { @@ -50,6 +52,13 @@ void EVMObjectCompiler::run(Object& _object) subIDs[data.name] = m_assembly.appendData(data.data); } + if (m_dialect.providesObjectAccess()) + { + m_dialect.setSubIDs(std::move(subIDs)); + m_dialect.setCurrentObject(&_object); + } + yulAssert(_object.analysisInfo, "No analysis info."); - CodeTransform{m_assembly, *_object.analysisInfo, m_yul, m_evm15}(*_object.code); + yulAssert(_object.code, "No code."); + CodeTransform{m_assembly, *_object.analysisInfo, *_object.code, m_dialect, _optimize, m_evm15}(*_object.code); } diff --git a/libyul/backends/evm/EVMObjectCompiler.h b/libyul/backends/evm/EVMObjectCompiler.h index c7172e47..057521a8 100644 --- a/libyul/backends/evm/EVMObjectCompiler.h +++ b/libyul/backends/evm/EVMObjectCompiler.h @@ -23,20 +23,21 @@ namespace yul { struct Object; class AbstractAssembly; +struct EVMDialect; class EVMObjectCompiler { public: - static void compile(Object& _object, AbstractAssembly& _assembly, bool _yul, bool _evm15); + static void compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize); private: - EVMObjectCompiler(AbstractAssembly& _assembly, bool _yul, bool _evm15): - m_assembly(_assembly), m_yul(_yul), m_evm15(_evm15) + EVMObjectCompiler(AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15): + m_assembly(_assembly), m_dialect(_dialect), m_evm15(_evm15) {} - void run(Object& _object); + void run(Object& _object, bool _optimize); AbstractAssembly& m_assembly; - bool m_yul = false; + EVMDialect& m_dialect; bool m_evm15 = false; }; diff --git a/libyul/optimiser/ASTWalker.cpp b/libyul/optimiser/ASTWalker.cpp index 0d568007..6adcb2e1 100644 --- a/libyul/optimiser/ASTWalker.cpp +++ b/libyul/optimiser/ASTWalker.cpp @@ -93,6 +93,16 @@ void ASTWalker::operator()(Block const& _block) walkVector(_block.statements); } +void ASTWalker::visit(Statement const& _st) +{ + boost::apply_visitor(*this, _st); +} + +void ASTWalker::visit(Expression const& _e) +{ + boost::apply_visitor(*this, _e); +} + void ASTModifier::operator()(FunctionalInstruction& _instr) { walkVector(_instr.arguments | boost::adaptors::reversed); @@ -155,3 +165,13 @@ void ASTModifier::operator()(Block& _block) { walkVector(_block.statements); } + +void ASTModifier::visit(Statement& _st) +{ + boost::apply_visitor(*this, _st); +} + +void ASTModifier::visit(Expression& _e) +{ + boost::apply_visitor(*this, _e); +} diff --git a/libyul/optimiser/ASTWalker.h b/libyul/optimiser/ASTWalker.h index b59b405e..272725f9 100644 --- a/libyul/optimiser/ASTWalker.h +++ b/libyul/optimiser/ASTWalker.h @@ -58,14 +58,8 @@ public: virtual void operator()(ForLoop const&); virtual void operator()(Block const& _block); - virtual void visit(Statement const& _st) - { - boost::apply_visitor(*this, _st); - } - virtual void visit(Expression const& _e) - { - boost::apply_visitor(*this, _e); - } + virtual void visit(Statement const& _st); + virtual void visit(Expression const& _e); protected: template <class T> @@ -99,14 +93,8 @@ public: virtual void operator()(ForLoop&); virtual void operator()(Block& _block); - virtual void visit(Statement& _st) - { - boost::apply_visitor(*this, _st); - } - virtual void visit(Expression& _e) - { - boost::apply_visitor(*this, _e); - } + virtual void visit(Statement& _st); + virtual void visit(Expression& _e); protected: template <class T> |