From 565130994ac59689f9d1b42c6bfab2b30cd07128 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 28 Nov 2017 12:45:37 +0100 Subject: Introduce InlineAsm structs into Julia namespace. --- libjulia/AsmDataForward.h | 52 ++++++++++++++++++++++++++++++++ libjulia/backends/evm/EVMCodeTransform.h | 43 +++++++++++++------------- 2 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 libjulia/AsmDataForward.h (limited to 'libjulia') diff --git a/libjulia/AsmDataForward.h b/libjulia/AsmDataForward.h new file mode 100644 index 00000000..3806e321 --- /dev/null +++ b/libjulia/AsmDataForward.h @@ -0,0 +1,52 @@ +/* + 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 . +*/ +/** + * @date 2017 + * Pull in some identifiers from the solidity::assembly namespace. + */ + +#pragma once + +#include + +namespace dev +{ +namespace julia +{ + +using Instruction = solidity::assembly::Instruction; +using Literal = solidity::assembly::Literal; +using Label = solidity::assembly::Label; +using StackAssignment = solidity::assembly::StackAssignment; +using Identifier = solidity::assembly::Identifier; +using Assignment = solidity::assembly::Assignment; +using VariableDeclaration = solidity::assembly::VariableDeclaration; +using FunctionalInstruction = solidity::assembly::FunctionalInstruction; +using FunctionDefinition = solidity::assembly::FunctionDefinition; +using FunctionCall = solidity::assembly::FunctionCall; +using If = solidity::assembly::If; +using Case = solidity::assembly::Case; +using Switch = solidity::assembly::Switch; +using ForLoop = solidity::assembly::ForLoop; +using Block = solidity::assembly::Block; + +using TypedName = solidity::assembly::TypedName; + +using Statement = boost::variant; + +} +} diff --git a/libjulia/backends/evm/EVMCodeTransform.h b/libjulia/backends/evm/EVMCodeTransform.h index 387720a2..e4cb20ed 100644 --- a/libjulia/backends/evm/EVMCodeTransform.h +++ b/libjulia/backends/evm/EVMCodeTransform.h @@ -20,8 +20,9 @@ #include +#include + #include -#include #include #include @@ -95,38 +96,38 @@ protected: {} public: - void operator()(solidity::assembly::Instruction const& _instruction); - void operator()(solidity::assembly::Literal const& _literal); - void operator()(solidity::assembly::Identifier const& _identifier); - void operator()(solidity::assembly::FunctionalInstruction const& _instr); - void operator()(solidity::assembly::FunctionCall const&); - void operator()(solidity::assembly::Label const& _label); - void operator()(solidity::assembly::StackAssignment const& _assignment); - void operator()(solidity::assembly::Assignment const& _assignment); - void operator()(solidity::assembly::VariableDeclaration const& _varDecl); - void operator()(solidity::assembly::If const& _if); - void operator()(solidity::assembly::Switch const& _switch); - void operator()(solidity::assembly::FunctionDefinition const&); - void operator()(solidity::assembly::ForLoop const&); - void operator()(solidity::assembly::Block const& _block); + void operator()(Instruction const& _instruction); + void operator()(Literal const& _literal); + void operator()(Identifier const& _identifier); + void operator()(FunctionalInstruction const& _instr); + void operator()(FunctionCall const&); + void operator()(Label const& _label); + void operator()(StackAssignment const& _assignment); + void operator()(Assignment const& _assignment); + void operator()(VariableDeclaration const& _varDecl); + void operator()(If const& _if); + void operator()(Switch const& _switch); + void operator()(FunctionDefinition const&); + void operator()(ForLoop const&); + void operator()(Block const& _block); private: - AbstractAssembly::LabelID labelFromIdentifier(solidity::assembly::Identifier const& _identifier); + AbstractAssembly::LabelID labelFromIdentifier(Identifier const& _identifier); /// @returns the label ID corresponding to the given label, allocating a new one if /// necessary. AbstractAssembly::LabelID labelID(solidity::assembly::Scope::Label const& _label); AbstractAssembly::LabelID functionEntryID(std::string const& _name, solidity::assembly::Scope::Function const& _function); /// Generates code for an expression that is supposed to return a single value. - void visitExpression(solidity::assembly::Statement const& _expression); + void visitExpression(Statement const& _expression); - void visitStatements(std::vector const& _statements); + void visitStatements(std::vector const& _statements); /// Pops all variables declared in the block and checks that the stack height is equal /// to @a _blackStartStackHeight. - void finalizeBlock(solidity::assembly::Block const& _block, int _blockStartStackHeight); + void finalizeBlock(Block const& _block, int _blockStartStackHeight); - void generateMultiAssignment(std::vector const& _variableNames); - void generateAssignment(solidity::assembly::Identifier const& _variableName); + void generateMultiAssignment(std::vector const& _variableNames); + void generateAssignment(Identifier const& _variableName); /// Determines the stack height difference to the given variables. Throws /// if it is not yet in scope or the height difference is too large. Returns -- cgit v1.2.3 From cbb3370836df17dc1edc1497a2caa6fb930cd4dd Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 30 Nov 2017 19:12:39 +0100 Subject: Fix namespace ambiguity. --- libjulia/backends/evm/EVMAssembly.cpp | 2 +- libjulia/backends/evm/EVMCodeTransform.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'libjulia') diff --git a/libjulia/backends/evm/EVMAssembly.cpp b/libjulia/backends/evm/EVMAssembly.cpp index 1d499b20..07ad05c9 100644 --- a/libjulia/backends/evm/EVMAssembly.cpp +++ b/libjulia/backends/evm/EVMAssembly.cpp @@ -26,7 +26,7 @@ using namespace std; using namespace dev; -using namespace julia; +using namespace dev::julia; namespace { diff --git a/libjulia/backends/evm/EVMCodeTransform.cpp b/libjulia/backends/evm/EVMCodeTransform.cpp index 13d9d011..099b83fe 100644 --- a/libjulia/backends/evm/EVMCodeTransform.cpp +++ b/libjulia/backends/evm/EVMCodeTransform.cpp @@ -31,7 +31,8 @@ using namespace std; using namespace dev; using namespace dev::julia; using namespace dev::solidity; -using namespace dev::solidity::assembly; + +using Scope = dev::solidity::assembly::Scope; void CodeTransform::operator()(VariableDeclaration const& _varDecl) { -- cgit v1.2.3 From 43bb915454e210ed7c201eb58b33c7ffe8dfbdb1 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 1 Dec 2017 14:10:49 +0100 Subject: Rename arguments to paramaters and returns to returnVariables. --- libjulia/backends/evm/EVMCodeTransform.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'libjulia') diff --git a/libjulia/backends/evm/EVMCodeTransform.cpp b/libjulia/backends/evm/EVMCodeTransform.cpp index 13d9d011..efc99166 100644 --- a/libjulia/backends/evm/EVMCodeTransform.cpp +++ b/libjulia/backends/evm/EVMCodeTransform.cpp @@ -289,7 +289,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) solAssert(m_info.scopes.at(&_function.body), ""); Scope* varScope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get(); solAssert(varScope, ""); - for (auto const& v: _function.arguments | boost::adaptors::reversed) + for (auto const& v: _function.parameters | boost::adaptors::reversed) { auto& var = boost::get(varScope->identifiers.at(v.name)); m_context->variableStackHeights[&var] = height++; @@ -302,7 +302,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) if (m_evm15) { m_assembly.appendJumpTo(afterFunction, -stackHeightBefore); - m_assembly.appendBeginsub(functionEntryID(_function.name, function), _function.arguments.size()); + m_assembly.appendBeginsub(functionEntryID(_function.name, function), _function.parameters.size()); } else { @@ -311,7 +311,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) } m_stackAdjustment += localStackAdjustment; - for (auto const& v: _function.returns) + for (auto const& v: _function.returnVariables) { auto& var = boost::get(varScope->identifiers.at(v.name)); m_context->variableStackHeights[&var] = height++; @@ -341,9 +341,9 @@ void CodeTransform::operator()(FunctionDefinition const& _function) // modified parallel to the actual stack. vector stackLayout; if (!m_evm15) - stackLayout.push_back(_function.returns.size()); // Move return label to the top - stackLayout += vector(_function.arguments.size(), -1); // discard all arguments - for (size_t i = 0; i < _function.returns.size(); ++i) + stackLayout.push_back(_function.returnVariables.size()); // Move return label to the top + stackLayout += vector(_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. solAssert(stackLayout.size() <= 17, "Stack too deep"); @@ -363,9 +363,9 @@ void CodeTransform::operator()(FunctionDefinition const& _function) } if (m_evm15) - m_assembly.appendReturnsub(_function.returns.size(), stackHeightBefore); + m_assembly.appendReturnsub(_function.returnVariables.size(), stackHeightBefore); else - m_assembly.appendJump(stackHeightBefore - _function.returns.size()); + m_assembly.appendJump(stackHeightBefore - _function.returnVariables.size()); m_stackAdjustment -= localStackAdjustment; m_assembly.appendLabel(afterFunction); checkStackHeight(&_function); -- cgit v1.2.3 From 07101c13385dba6c103d9d0f51588638d94d5b81 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 1 Dec 2017 22:58:05 +0100 Subject: Rename to ASTDataForward.h --- libjulia/ASTDataForward.h | 52 ++++++++++++++++++++++++++++++++ libjulia/AsmDataForward.h | 52 -------------------------------- libjulia/backends/evm/EVMCodeTransform.h | 2 +- 3 files changed, 53 insertions(+), 53 deletions(-) create mode 100644 libjulia/ASTDataForward.h delete mode 100644 libjulia/AsmDataForward.h (limited to 'libjulia') diff --git a/libjulia/ASTDataForward.h b/libjulia/ASTDataForward.h new file mode 100644 index 00000000..3806e321 --- /dev/null +++ b/libjulia/ASTDataForward.h @@ -0,0 +1,52 @@ +/* + 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 . +*/ +/** + * @date 2017 + * Pull in some identifiers from the solidity::assembly namespace. + */ + +#pragma once + +#include + +namespace dev +{ +namespace julia +{ + +using Instruction = solidity::assembly::Instruction; +using Literal = solidity::assembly::Literal; +using Label = solidity::assembly::Label; +using StackAssignment = solidity::assembly::StackAssignment; +using Identifier = solidity::assembly::Identifier; +using Assignment = solidity::assembly::Assignment; +using VariableDeclaration = solidity::assembly::VariableDeclaration; +using FunctionalInstruction = solidity::assembly::FunctionalInstruction; +using FunctionDefinition = solidity::assembly::FunctionDefinition; +using FunctionCall = solidity::assembly::FunctionCall; +using If = solidity::assembly::If; +using Case = solidity::assembly::Case; +using Switch = solidity::assembly::Switch; +using ForLoop = solidity::assembly::ForLoop; +using Block = solidity::assembly::Block; + +using TypedName = solidity::assembly::TypedName; + +using Statement = boost::variant; + +} +} diff --git a/libjulia/AsmDataForward.h b/libjulia/AsmDataForward.h deleted file mode 100644 index 3806e321..00000000 --- a/libjulia/AsmDataForward.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - 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 . -*/ -/** - * @date 2017 - * Pull in some identifiers from the solidity::assembly namespace. - */ - -#pragma once - -#include - -namespace dev -{ -namespace julia -{ - -using Instruction = solidity::assembly::Instruction; -using Literal = solidity::assembly::Literal; -using Label = solidity::assembly::Label; -using StackAssignment = solidity::assembly::StackAssignment; -using Identifier = solidity::assembly::Identifier; -using Assignment = solidity::assembly::Assignment; -using VariableDeclaration = solidity::assembly::VariableDeclaration; -using FunctionalInstruction = solidity::assembly::FunctionalInstruction; -using FunctionDefinition = solidity::assembly::FunctionDefinition; -using FunctionCall = solidity::assembly::FunctionCall; -using If = solidity::assembly::If; -using Case = solidity::assembly::Case; -using Switch = solidity::assembly::Switch; -using ForLoop = solidity::assembly::ForLoop; -using Block = solidity::assembly::Block; - -using TypedName = solidity::assembly::TypedName; - -using Statement = boost::variant; - -} -} diff --git a/libjulia/backends/evm/EVMCodeTransform.h b/libjulia/backends/evm/EVMCodeTransform.h index e4cb20ed..577cc8ba 100644 --- a/libjulia/backends/evm/EVMCodeTransform.h +++ b/libjulia/backends/evm/EVMCodeTransform.h @@ -20,7 +20,7 @@ #include -#include +#include #include -- cgit v1.2.3 From 861210f543cea615d7779fb7b1bc133349ae971a Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 28 Nov 2017 12:45:37 +0100 Subject: Disambiguator. --- libjulia/optimiser/ASTCopier.cpp | 179 +++++++++++++++++++++++++++++++++++ libjulia/optimiser/ASTCopier.h | 101 ++++++++++++++++++++ libjulia/optimiser/Disambiguator.cpp | 85 +++++++++++++++++ libjulia/optimiser/Disambiguator.h | 70 ++++++++++++++ 4 files changed, 435 insertions(+) create mode 100644 libjulia/optimiser/ASTCopier.cpp create mode 100644 libjulia/optimiser/ASTCopier.h create mode 100644 libjulia/optimiser/Disambiguator.cpp create mode 100644 libjulia/optimiser/Disambiguator.h (limited to 'libjulia') diff --git a/libjulia/optimiser/ASTCopier.cpp b/libjulia/optimiser/ASTCopier.cpp new file mode 100644 index 00000000..2f9eade3 --- /dev/null +++ b/libjulia/optimiser/ASTCopier.cpp @@ -0,0 +1,179 @@ +/* + 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 . +*/ +/** + * Creates an independent copy of an AST, renaming identifiers to be unique. + */ + +#include + +#include + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; + + +shared_ptr ASTCopier::run() +{ + return make_shared(translate(m_block)); +} + +Statement ASTCopier::operator()(Instruction const& _instruction) +{ + return _instruction; +} + +Statement ASTCopier::operator()(VariableDeclaration const& _varDecl) +{ + return VariableDeclaration{ + _varDecl.location, + translateVector(_varDecl.variables), + translate(_varDecl.value) + }; +} + +Statement ASTCopier::operator()(Assignment const& _assignment) +{ + return Assignment{ + _assignment.location, + translateVector(_assignment.variableNames), + translate(_assignment.value) + }; +} + +Statement ASTCopier::operator()(StackAssignment const&) +{ + solAssert(false, "Invalid operation."); + return {}; +} + +Statement ASTCopier::operator()(Label const&) +{ + solAssert(false, "Invalid operation."); + return {}; +} + +Statement ASTCopier::operator()(FunctionCall const& _call) +{ + return FunctionCall{ + _call.location, + translate(_call.functionName), + translateVector(_call.arguments) + }; +} + +Statement ASTCopier::operator()(FunctionalInstruction const& _instruction) +{ + return FunctionalInstruction{ + _instruction.location, + _instruction.instruction, + translateVector(_instruction.arguments) + }; +} + +Statement ASTCopier::operator()(Identifier const& _identifier) +{ + return Identifier{_identifier.location, translateIdentifier(_identifier.name)}; +} + +Statement ASTCopier::operator()(Literal const& _literal) +{ + return translate(_literal); +} + +Statement ASTCopier::operator()(If const& _if) +{ + return If{_if.location, translate(_if.condition), translate(_if.body)}; +} + +Statement ASTCopier::operator()(Switch const& _switch) +{ + return Switch{_switch.location, translate(_switch.expression), translateVector(_switch.cases)}; +} + +Statement ASTCopier::operator()(FunctionDefinition const& _function) +{ + string translatedName = translateIdentifier(_function.name); + + enterFunction(_function); + ScopeGuard g([&]() { this->leaveFunction(_function); }); + + return FunctionDefinition{ + _function.location, + move(translatedName), + translateVector(_function.parameters), + translateVector(_function.returnVariables), + translate(_function.body) + }; +} + +Statement ASTCopier::operator()(ForLoop const& _forLoop) +{ + enterScope(_forLoop.pre); + ScopeGuard g([&]() { this->leaveScope(_forLoop.pre); }); + + return ForLoop{ + _forLoop.location, + translate(_forLoop.pre), + translate(_forLoop.condition), + translate(_forLoop.post), + translate(_forLoop.body) + }; +} + +Statement ASTCopier::operator ()(Block const& _block) +{ + return translate(_block); +} + +Statement ASTCopier::translate(Statement const& _statement) +{ + return boost::apply_visitor(*this, _statement); +} + +Block ASTCopier::translate(Block const& _block) +{ + enterScope(_block); + ScopeGuard g([&]() { this->leaveScope(_block); }); + + return Block{_block.location, translateVector(_block.statements)}; +} + +Case ASTCopier::translate(Case const& _case) +{ + return Case{_case.location, translate(_case.value), translate(_case.body)}; +} + +Identifier ASTCopier::translate(Identifier const& _identifier) +{ + return Identifier{_identifier.location, translateIdentifier(_identifier.name)}; +} + +Literal ASTCopier::translate(Literal const& _literal) +{ + return _literal; +} + +TypedName ASTCopier::translate(TypedName const& _typedName) +{ + return TypedName{_typedName.location, translateIdentifier(_typedName.name), _typedName.type}; +} + diff --git a/libjulia/optimiser/ASTCopier.h b/libjulia/optimiser/ASTCopier.h new file mode 100644 index 00000000..759dce31 --- /dev/null +++ b/libjulia/optimiser/ASTCopier.h @@ -0,0 +1,101 @@ +/* + 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 . +*/ +/** + * Creates an independent copy of an AST, renaming identifiers to be unique. + */ + +#pragma once + +#include + +#include +#include + +#include +#include + +namespace dev +{ +namespace julia +{ + +/** + * Creates a copy of a iulia AST potentially replacing identifier names. + * Base class to be extended. + */ +class ASTCopier: public boost::static_visitor +{ +public: + ASTCopier(Block const& _block): + m_block(_block) + {} + + std::shared_ptr run(); + +public: + Statement operator()(Literal const& _literal); + Statement operator()(Instruction const& _instruction); + Statement operator()(Identifier const& _identifier); + Statement operator()(FunctionalInstruction const& _instr); + Statement operator()(FunctionCall const&); + Statement operator()(Label const& _label); + Statement operator()(StackAssignment const& _assignment); + Statement operator()(Assignment const& _assignment); + Statement operator()(VariableDeclaration const& _varDecl); + Statement operator()(If const& _if); + Statement operator()(Switch const& _switch); + Statement operator()(FunctionDefinition const&); + Statement operator()(ForLoop const&); + Statement operator()(Block const& _block); + +protected: + template + std::vector translateVector(std::vector const& _values); + + template + std::shared_ptr translate(std::shared_ptr const& _v) + { + return _v ? std::make_shared(translate(*_v)) : nullptr; + } + Statement translate(Statement const& _statement); + Block translate(Block const& _block); + Case translate(Case const& _case); + Identifier translate(Identifier const& _identifier); + Literal translate(Literal const& _literal); + TypedName translate(TypedName const& _typedName); + + virtual void enterScope(Block const& _block) = 0; + virtual void leaveScope(Block const& _block) = 0; + virtual void enterFunction(FunctionDefinition const& _function) = 0; + virtual void leaveFunction(FunctionDefinition const& _function) = 0; + virtual std::string translateIdentifier(std::string const& _name) { return _name; } + + Block const& m_block; +}; + +template +std::vector ASTCopier::translateVector(std::vector const& _values) +{ + std::vector translated; + for (auto const& v: _values) + translated.emplace_back(translate(v)); + return translated; +} + + +} +} diff --git a/libjulia/optimiser/Disambiguator.cpp b/libjulia/optimiser/Disambiguator.cpp new file mode 100644 index 00000000..df2984e5 --- /dev/null +++ b/libjulia/optimiser/Disambiguator.cpp @@ -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 . +*/ +/** + * Optimiser component that makes all identifiers unique. + */ + +#include + +#include +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; +using namespace dev::solidity; + +using Scope = dev::solidity::assembly::Scope; + +string Disambiguator::translateIdentifier(string const& _originalName) +{ + solAssert(!m_scopes.empty() && m_scopes.back(), ""); + Scope::Identifier const* id = m_scopes.back()->lookup(_originalName); + solAssert(id, ""); + if (!m_translations.count(id)) + { + string translated = _originalName; + size_t suffix = 0; + while (m_usedNames.count(translated)) + { + suffix++; + translated = _originalName + "_" + std::to_string(suffix); + } + m_usedNames.insert(translated); + m_translations[id] = translated; + } + return m_translations.at(id); +} + +void Disambiguator::enterScope(Block const& _block) +{ + enterScopeInternal(*m_info.scopes.at(&_block)); +} + +void Disambiguator::leaveScope(Block const& _block) +{ + leaveScopeInternal(*m_info.scopes.at(&_block)); +} + +void Disambiguator::enterFunction(FunctionDefinition const& _function) +{ + enterScopeInternal(*m_info.scopes.at(m_info.virtualBlocks.at(&_function).get())); +} + +void Disambiguator::leaveFunction(FunctionDefinition const& _function) +{ + leaveScopeInternal(*m_info.scopes.at(m_info.virtualBlocks.at(&_function).get())); +} + +void Disambiguator::enterScopeInternal(Scope& _scope) +{ + m_scopes.push_back(&_scope); +} + +void Disambiguator::leaveScopeInternal(Scope& _scope) +{ + solAssert(!m_scopes.empty(), ""); + solAssert(m_scopes.back() == &_scope, ""); + m_scopes.pop_back(); +} diff --git a/libjulia/optimiser/Disambiguator.h b/libjulia/optimiser/Disambiguator.h new file mode 100644 index 00000000..39615cf7 --- /dev/null +++ b/libjulia/optimiser/Disambiguator.h @@ -0,0 +1,70 @@ +/* + 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 . +*/ +/** + * Optimiser component that makes all identifiers unique. + */ + +#pragma once + +#include + +#include + +#include + +#include +#include + +#include + +namespace dev +{ +namespace julia +{ +class EVMAssembly; + +/** + * Creates a copy of a iulia AST replacing all identifiers by unique names. + */ +class Disambiguator: public ASTCopier +{ +public: + Disambiguator( + Block const& _block, + solidity::assembly::AsmAnalysisInfo const& _analysisInfo + ): ASTCopier(_block), m_info(_analysisInfo) + {} + +protected: + virtual void enterScope(Block const& _block) override; + virtual void leaveScope(Block const& _block) override; + virtual void enterFunction(FunctionDefinition const& _function) override; + virtual void leaveFunction(FunctionDefinition const& _function) override; + virtual std::string translateIdentifier(std::string const& _name) override; + + void enterScopeInternal(solidity::assembly::Scope& _scope); + void leaveScopeInternal(solidity::assembly::Scope& _scope); + + solidity::assembly::AsmAnalysisInfo const& m_info; + + std::vector m_scopes; + std::map m_translations; + std::set m_usedNames; +}; + +} +} -- cgit v1.2.3 From 30d7afc2e3fd27333e463a9127011ec1f7470d3e Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 28 Nov 2017 15:00:23 +0100 Subject: Simplify disambiguator. --- libjulia/optimiser/ASTCopier.cpp | 5 ----- libjulia/optimiser/ASTCopier.h | 18 +++++------------- libjulia/optimiser/Disambiguator.h | 6 ++---- 3 files changed, 7 insertions(+), 22 deletions(-) (limited to 'libjulia') diff --git a/libjulia/optimiser/ASTCopier.cpp b/libjulia/optimiser/ASTCopier.cpp index 2f9eade3..d777d731 100644 --- a/libjulia/optimiser/ASTCopier.cpp +++ b/libjulia/optimiser/ASTCopier.cpp @@ -31,11 +31,6 @@ using namespace dev; using namespace dev::julia; -shared_ptr ASTCopier::run() -{ - return make_shared(translate(m_block)); -} - Statement ASTCopier::operator()(Instruction const& _instruction) { return _instruction; diff --git a/libjulia/optimiser/ASTCopier.h b/libjulia/optimiser/ASTCopier.h index 759dce31..0658fd50 100644 --- a/libjulia/optimiser/ASTCopier.h +++ b/libjulia/optimiser/ASTCopier.h @@ -27,6 +27,7 @@ #include #include +#include namespace dev { @@ -39,13 +40,6 @@ namespace julia */ class ASTCopier: public boost::static_visitor { -public: - ASTCopier(Block const& _block): - m_block(_block) - {} - - std::shared_ptr run(); - public: Statement operator()(Literal const& _literal); Statement operator()(Instruction const& _instruction); @@ -78,13 +72,11 @@ protected: Literal translate(Literal const& _literal); TypedName translate(TypedName const& _typedName); - virtual void enterScope(Block const& _block) = 0; - virtual void leaveScope(Block const& _block) = 0; - virtual void enterFunction(FunctionDefinition const& _function) = 0; - virtual void leaveFunction(FunctionDefinition const& _function) = 0; + virtual void enterScope(Block const&) { } + virtual void leaveScope(Block const&) { } + virtual void enterFunction(FunctionDefinition const&) { } + virtual void leaveFunction(FunctionDefinition const&) { } virtual std::string translateIdentifier(std::string const& _name) { return _name; } - - Block const& m_block; }; template diff --git a/libjulia/optimiser/Disambiguator.h b/libjulia/optimiser/Disambiguator.h index 39615cf7..cc9488d5 100644 --- a/libjulia/optimiser/Disambiguator.h +++ b/libjulia/optimiser/Disambiguator.h @@ -43,10 +43,8 @@ class EVMAssembly; class Disambiguator: public ASTCopier { public: - Disambiguator( - Block const& _block, - solidity::assembly::AsmAnalysisInfo const& _analysisInfo - ): ASTCopier(_block), m_info(_analysisInfo) + Disambiguator(solidity::assembly::AsmAnalysisInfo const& _analysisInfo): + m_info(_analysisInfo) {} protected: -- cgit v1.2.3 From c6df1cdaaa3d743b24e655a1e94eecf4ba49de3b Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 30 Nov 2017 01:19:49 +0100 Subject: Generic AST walker. --- libjulia/optimiser/ASTWalker.cpp | 149 +++++++++++++++++++++++++++++++++++ libjulia/optimiser/ASTWalker.h | 100 +++++++++++++++++++++++ libjulia/optimiser/NameCollector.cpp | 44 +++++++++++ libjulia/optimiser/NameCollector.h | 52 ++++++++++++ 4 files changed, 345 insertions(+) create mode 100644 libjulia/optimiser/ASTWalker.cpp create mode 100644 libjulia/optimiser/ASTWalker.h create mode 100644 libjulia/optimiser/NameCollector.cpp create mode 100644 libjulia/optimiser/NameCollector.h (limited to 'libjulia') diff --git a/libjulia/optimiser/ASTWalker.cpp b/libjulia/optimiser/ASTWalker.cpp new file mode 100644 index 00000000..786afbe0 --- /dev/null +++ b/libjulia/optimiser/ASTWalker.cpp @@ -0,0 +1,149 @@ +/* + 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 . +*/ +/** + * Generic AST walker. + */ + +#include + +#include + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; +using namespace dev::solidity; + + +void ASTWalker::operator()(FunctionalInstruction const& _instr) +{ + walkVector(_instr.arguments | boost::adaptors::reversed); +} + +void ASTWalker::operator()(FunctionCall const& _funCall) +{ + walkVector(_funCall.arguments | boost::adaptors::reversed); +} + +void ASTWalker::operator()(Assignment const& _assignment) +{ + for (auto const& name: _assignment.variableNames) + (*this)(name); + boost::apply_visitor(*this, *_assignment.value); +} + +void ASTWalker::operator()(VariableDeclaration const& _varDecl) +{ + if (_varDecl.value) + boost::apply_visitor(*this, *_varDecl.value); +} + +void ASTWalker::operator()(If const& _if) +{ + boost::apply_visitor(*this, *_if.condition); + (*this)(_if.body); +} + +void ASTWalker::operator()(Switch const& _switch) +{ + boost::apply_visitor(*this, *_switch.expression); + for (auto const& _case: _switch.cases) + { + if (_case.value) + (*this)(*_case.value); + (*this)(_case.body); + } +} + +void ASTWalker::operator()(FunctionDefinition const& _fun) +{ + (*this)(_fun.body); +} + +void ASTWalker::operator()(ForLoop const& _for) +{ + (*this)(_for.pre); + boost::apply_visitor(*this, *_for.condition); + (*this)(_for.post); + (*this)(_for.body); +} + +void ASTWalker::operator()(Block const& _block) +{ + walkVector(_block.statements); +} + +void ASTModifier::operator()(FunctionalInstruction& _instr) +{ + walkVector(_instr.arguments | boost::adaptors::reversed); +} + +void ASTModifier::operator()(FunctionCall& _funCall) +{ + walkVector(_funCall.arguments | boost::adaptors::reversed); +} + +void ASTModifier::operator()(Assignment& _assignment) +{ + for (auto& name: _assignment.variableNames) + (*this)(name); + boost::apply_visitor(*this, *_assignment.value); +} + +void ASTModifier::operator()(VariableDeclaration& _varDecl) +{ + if (_varDecl.value) + boost::apply_visitor(*this, *_varDecl.value); +} + +void ASTModifier::operator()(If& _if) +{ + boost::apply_visitor(*this, *_if.condition); + (*this)(_if.body); +} + +void ASTModifier::operator()(Switch& _switch) +{ + boost::apply_visitor(*this, *_switch.expression); + for (auto& _case: _switch.cases) + { + if (_case.value) + (*this)(*_case.value); + (*this)(_case.body); + } +} + +void ASTModifier::operator()(FunctionDefinition& _fun) +{ + (*this)(_fun.body); +} + +void ASTModifier::operator()(ForLoop& _for) +{ + (*this)(_for.pre); + boost::apply_visitor(*this, *_for.condition); + (*this)(_for.post); + (*this)(_for.body); +} + +void ASTModifier::operator()(Block& _block) +{ + walkVector(_block.statements); +} diff --git a/libjulia/optimiser/ASTWalker.h b/libjulia/optimiser/ASTWalker.h new file mode 100644 index 00000000..01499a50 --- /dev/null +++ b/libjulia/optimiser/ASTWalker.h @@ -0,0 +1,100 @@ +/* + 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 . +*/ +/** + * Generic AST walker. + */ + +#pragma once + +#include + +#include + +#include +#include + +#include +#include +#include + +namespace dev +{ +namespace julia +{ + +/** + * Generic AST walker. + */ +class ASTWalker: public boost::static_visitor<> +{ +public: + virtual void operator()(Literal const&) {} + virtual void operator()(Instruction const&) { solAssert(false, ""); } + virtual void operator()(Identifier const&) {} + virtual void operator()(FunctionalInstruction const& _instr); + virtual void operator()(FunctionCall const& _funCall); + virtual void operator()(Label const&) { solAssert(false, ""); } + virtual void operator()(StackAssignment const&) { solAssert(false, ""); } + virtual void operator()(Assignment const& _assignment); + virtual void operator()(VariableDeclaration const& _varDecl); + virtual void operator()(If const& _if); + virtual void operator()(Switch const& _switch); + virtual void operator()(FunctionDefinition const&); + virtual void operator()(ForLoop const&); + virtual void operator()(Block const& _block); + +protected: + template + void walkVector(T const& _statements) + { + for (auto const& st: _statements) + boost::apply_visitor(*this, st); + } +}; + +/** + * Generic AST modifier (i.e. non-const version of ASTWalker). + */ +class ASTModifier: public boost::static_visitor<> +{ +public: + virtual void operator()(Literal&) {} + virtual void operator()(Instruction&) { solAssert(false, ""); } + virtual void operator()(Identifier&) {} + virtual void operator()(FunctionalInstruction& _instr); + virtual void operator()(FunctionCall& _funCall); + virtual void operator()(Label&) { solAssert(false, ""); } + virtual void operator()(StackAssignment&) { solAssert(false, ""); } + virtual void operator()(Assignment& _assignment); + virtual void operator()(VariableDeclaration& _varDecl); + virtual void operator()(If& _if); + virtual void operator()(Switch& _switch); + virtual void operator()(FunctionDefinition&); + virtual void operator()(ForLoop&); + virtual void operator()(Block& _block); + +protected: + template + void walkVector(T&& _statements) + { + for (auto& st: _statements) + boost::apply_visitor(*this, st); + } +}; + +} +} diff --git a/libjulia/optimiser/NameCollector.cpp b/libjulia/optimiser/NameCollector.cpp new file mode 100644 index 00000000..7b4c4793 --- /dev/null +++ b/libjulia/optimiser/NameCollector.cpp @@ -0,0 +1,44 @@ +/* + 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 . +*/ +/** + * Specific AST walker that collects all defined names. + */ + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; + +void NameCollector::operator()(VariableDeclaration const& _varDecl) +{ + for (auto const& var: _varDecl.variables) + m_names.insert(var.name); +} + +void NameCollector::operator ()(FunctionDefinition const& _funDef) +{ + m_names.insert(_funDef.name); + m_functions[_funDef.name] = &_funDef; + for (auto const arg: _funDef.parameters) + m_names.insert(arg.name); + for (auto const ret: _funDef.returnVariables) + m_names.insert(ret.name); + ASTWalker::operator ()(_funDef); +} diff --git a/libjulia/optimiser/NameCollector.h b/libjulia/optimiser/NameCollector.h new file mode 100644 index 00000000..b7e38f46 --- /dev/null +++ b/libjulia/optimiser/NameCollector.h @@ -0,0 +1,52 @@ +/* + 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 . +*/ +/** + * Specific AST walker that collects all defined names. + */ + +#pragma once + +#include + +#include +#include +#include + +namespace dev +{ +namespace julia +{ + +/** + * Specific AST walker that collects all defined names. + */ +class NameCollector: public ASTWalker +{ +public: + using ASTWalker::operator (); + virtual void operator()(VariableDeclaration const& _varDecl) override; + virtual void operator()(FunctionDefinition const& _funDef) override; + + std::set const& names() const { return m_names; } + std::map const& functions() const { return m_functions; } +private: + std::set m_names; + std::map m_functions; +}; + +} +} -- cgit v1.2.3 From 745eefa36f9bc04c91cb28e81bd16f8d01a11c7c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 5 Dec 2017 13:44:20 +0000 Subject: Split Instruction and FunctionalInstruction in Julia --- libjulia/backends/evm/EVMCodeTransform.cpp | 9 +++++---- libjulia/optimiser/ASTCopier.cpp | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'libjulia') diff --git a/libjulia/backends/evm/EVMCodeTransform.cpp b/libjulia/backends/evm/EVMCodeTransform.cpp index 733e39e9..f92939be 100644 --- a/libjulia/backends/evm/EVMCodeTransform.cpp +++ b/libjulia/backends/evm/EVMCodeTransform.cpp @@ -125,11 +125,11 @@ void CodeTransform::operator()(FunctionCall const& _call) void CodeTransform::operator()(FunctionalInstruction const& _instruction) { if (m_evm15 && ( - _instruction.instruction.instruction == solidity::Instruction::JUMP || - _instruction.instruction.instruction == solidity::Instruction::JUMPI + _instruction.instruction == solidity::Instruction::JUMP || + _instruction.instruction == solidity::Instruction::JUMPI )) { - bool const isJumpI = _instruction.instruction.instruction == solidity::Instruction::JUMPI; + bool const isJumpI = _instruction.instruction == solidity::Instruction::JUMPI; if (isJumpI) { solAssert(_instruction.arguments.size() == 2, ""); @@ -150,7 +150,8 @@ void CodeTransform::operator()(FunctionalInstruction const& _instruction) { for (auto const& arg: _instruction.arguments | boost::adaptors::reversed) visitExpression(arg); - (*this)(_instruction.instruction); + m_assembly.setSourceLocation(_instruction.location); + m_assembly.appendInstruction(_instruction.instruction); } checkStackHeight(&_instruction); } diff --git a/libjulia/optimiser/ASTCopier.cpp b/libjulia/optimiser/ASTCopier.cpp index d777d731..a461f434 100644 --- a/libjulia/optimiser/ASTCopier.cpp +++ b/libjulia/optimiser/ASTCopier.cpp @@ -31,9 +31,10 @@ using namespace dev; using namespace dev::julia; -Statement ASTCopier::operator()(Instruction const& _instruction) +Statement ASTCopier::operator()(Instruction const&) { - return _instruction; + solAssert(false, "Invalid operation."); + return {}; } Statement ASTCopier::operator()(VariableDeclaration const& _varDecl) -- cgit v1.2.3 From 6769a9a503fd35ae147650634d82d1321e6b8826 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 5 Dec 2017 19:46:16 +0100 Subject: Make the modifier more flexible. --- libjulia/optimiser/ASTWalker.cpp | 10 +++++----- libjulia/optimiser/ASTWalker.h | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'libjulia') diff --git a/libjulia/optimiser/ASTWalker.cpp b/libjulia/optimiser/ASTWalker.cpp index 786afbe0..0caef04e 100644 --- a/libjulia/optimiser/ASTWalker.cpp +++ b/libjulia/optimiser/ASTWalker.cpp @@ -104,24 +104,24 @@ void ASTModifier::operator()(Assignment& _assignment) { for (auto& name: _assignment.variableNames) (*this)(name); - boost::apply_visitor(*this, *_assignment.value); + visit(*_assignment.value); } void ASTModifier::operator()(VariableDeclaration& _varDecl) { if (_varDecl.value) - boost::apply_visitor(*this, *_varDecl.value); + visit(*_varDecl.value); } void ASTModifier::operator()(If& _if) { - boost::apply_visitor(*this, *_if.condition); + visit(*_if.condition); (*this)(_if.body); } void ASTModifier::operator()(Switch& _switch) { - boost::apply_visitor(*this, *_switch.expression); + visit(*_switch.expression); for (auto& _case: _switch.cases) { if (_case.value) @@ -138,7 +138,7 @@ void ASTModifier::operator()(FunctionDefinition& _fun) void ASTModifier::operator()(ForLoop& _for) { (*this)(_for.pre); - boost::apply_visitor(*this, *_for.condition); + visit(*_for.condition); (*this)(_for.post); (*this)(_for.body); } diff --git a/libjulia/optimiser/ASTWalker.h b/libjulia/optimiser/ASTWalker.h index 01499a50..8bd867d5 100644 --- a/libjulia/optimiser/ASTWalker.h +++ b/libjulia/optimiser/ASTWalker.h @@ -92,7 +92,11 @@ protected: void walkVector(T&& _statements) { for (auto& st: _statements) - boost::apply_visitor(*this, st); + visit(st); + } + virtual void visit(Statement& _st) + { + boost::apply_visitor(*this, _st); } }; -- cgit v1.2.3 From d34054ef582be09992e79a72125800484b25159f Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 5 Dec 2017 20:06:34 +0100 Subject: copier --- libjulia/optimiser/ASTCopier.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'libjulia') diff --git a/libjulia/optimiser/ASTCopier.h b/libjulia/optimiser/ASTCopier.h index 0658fd50..5dde2ce9 100644 --- a/libjulia/optimiser/ASTCopier.h +++ b/libjulia/optimiser/ASTCopier.h @@ -56,6 +56,8 @@ public: Statement operator()(ForLoop const&); Statement operator()(Block const& _block); + virtual Statement translate(Statement const& _statement); + protected: template std::vector translateVector(std::vector const& _values); @@ -65,7 +67,6 @@ protected: { return _v ? std::make_shared(translate(*_v)) : nullptr; } - Statement translate(Statement const& _statement); Block translate(Block const& _block); Case translate(Case const& _case); Identifier translate(Identifier const& _identifier); -- cgit v1.2.3 From 07825e90fa261660625fb3731f6283db83789e8b Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 5 Dec 2017 20:07:31 +0100 Subject: substitution --- libjulia/optimiser/Substitution.cpp | 39 ++++++++++++++++++++++++++++ libjulia/optimiser/Substitution.h | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 libjulia/optimiser/Substitution.cpp create mode 100644 libjulia/optimiser/Substitution.h (limited to 'libjulia') diff --git a/libjulia/optimiser/Substitution.cpp b/libjulia/optimiser/Substitution.cpp new file mode 100644 index 00000000..a49f1f7a --- /dev/null +++ b/libjulia/optimiser/Substitution.cpp @@ -0,0 +1,39 @@ +/* + 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 . +*/ +/** + * Specific AST copier that replaces certain identifiers with expressions. + */ + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; + +Statement Substitution::translate(Statement const& _statement) +{ + if (_statement.type() == typeid(Identifier)) + { + string const& name = boost::get(_statement).name; + if (m_substitutions.count(name)) + // No recursive substitution + return ASTCopier().translate(*m_substitutions.at(name)); + } + return ASTCopier::translate(_statement); +} diff --git a/libjulia/optimiser/Substitution.h b/libjulia/optimiser/Substitution.h new file mode 100644 index 00000000..10bdf32e --- /dev/null +++ b/libjulia/optimiser/Substitution.h @@ -0,0 +1,51 @@ +/* + 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 . +*/ +/** + * Specific AST copier that replaces certain identifiers with expressions. + */ + +#pragma once + +#include + +#include +#include +#include + +namespace dev +{ +namespace julia +{ + +/** + * Specific AST copier that replaces certain identifiers with expressions. + * Only works on ASTs that are expressions. + */ +class Substitution: public ASTCopier +{ +public: + Substitution(std::map const& _substitutions): + m_substitutions(_substitutions) + {} + virtual Statement translate(Statement const& _statement) override; + +private: + std::map const& m_substitutions; +}; + +} +} -- cgit v1.2.3 From 2add4f604b86c67da9f9b1616f2374b67f755878 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 11 Dec 2017 16:10:40 +0100 Subject: Description of the optimiser and stages. --- libjulia/optimiser/README.md | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 libjulia/optimiser/README.md (limited to 'libjulia') diff --git a/libjulia/optimiser/README.md b/libjulia/optimiser/README.md new file mode 100644 index 00000000..771cb707 --- /dev/null +++ b/libjulia/optimiser/README.md @@ -0,0 +1,61 @@ +## IULIA Optimiser + +The iulia optimiser consists of several stages and components that all transform +the AST in a semantically equivalent way. The goal is to end up either with code +that is shorter or at least only marginally longer but will allow further +optimisation steps. + +The optimiser currently follows a purely greedy strategy and does not do any +backtracking. + +## Disambiguator + +The disambiguator takes an AST and returns a fresh copy where all identifiers have +names unique to the input AST. This is a prerequisite for all other optimiser stages. +One of the benefits is that identifier lookup does not need to take scopes into account +and we can basically ignore the result of the analysis phase. + +All subsequent stages have the property that all names stay unique. This means if +a new identifier needs to be introduced, a new unique name is generated. + +## Function Hoister + +The function hoister moves all function definitions to the topmost block. This is +a semantically equivalent transformation as long as it is performed after the +disambiguation stage. The reason is that moving a definition upwards cannot decrease +its visibility and it is impossible to reference variables defined in a different function. + +The benefit of this stage is that function definitions can be lookup up more easily. + +## Function Grouper + +The function grouper has to be applied after the disambiguator and the function hoister. +Its effect is that all topmost elements that are not function definitions are moved +into a single block which is the first satement of the root block. + +After this step, a program has the following normal form: + + { I F... } + +Where I is a block that does not contain any function definitions (not even recursively) +and F is a list of function definitions such that no function contains a function definition. + +## Functional Inliner + +The functional inliner depends on the disambiguator, the function hoister and function grouper. +It performs function inlining such that the result of the inlining is an expression. This can +only be done if the body of the function to be inlined has the form ``{ r := E }`` where ``r`` +is the single return value of the function, ``E`` is an expression and all arguments in the +function call are so-called movable expressions. A movable expression is either a literal, a +variable or a function call (or EVM opcode) which does not have side-effects and also does not +depend on any side-effects. + +As an example, neither ``mload`` nor ``mstore`` would be allowed. + +## Full Function Inliner + +## Variable Eliminator + +## Unused Declaration Pruner + +## Function Unifier -- cgit v1.2.3 From 54b6739962ef45319777ce2aebafdf4b91412d84 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 8 Dec 2017 14:01:22 +0100 Subject: Separate expression and statement. --- libjulia/ASTDataForward.h | 4 +- libjulia/backends/evm/EVMCodeTransform.cpp | 9 ++++- libjulia/backends/evm/EVMCodeTransform.h | 3 +- libjulia/optimiser/ASTCopier.cpp | 20 +++++++--- libjulia/optimiser/ASTCopier.h | 59 ++++++++++++++++++++++-------- libjulia/optimiser/ASTWalker.cpp | 10 +++++ libjulia/optimiser/ASTWalker.h | 6 +++ libjulia/optimiser/Substitution.cpp | 8 ++-- libjulia/optimiser/Substitution.h | 7 ++-- 9 files changed, 94 insertions(+), 32 deletions(-) (limited to 'libjulia') diff --git a/libjulia/ASTDataForward.h b/libjulia/ASTDataForward.h index 3806e321..143b9c46 100644 --- a/libjulia/ASTDataForward.h +++ b/libjulia/ASTDataForward.h @@ -42,11 +42,13 @@ using If = solidity::assembly::If; using Case = solidity::assembly::Case; using Switch = solidity::assembly::Switch; using ForLoop = solidity::assembly::ForLoop; +using ExpressionStatement = solidity::assembly::ExpressionStatement; using Block = solidity::assembly::Block; using TypedName = solidity::assembly::TypedName; -using Statement = boost::variant; +using Expression = boost::variant; +using Statement = boost::variant; } } diff --git a/libjulia/backends/evm/EVMCodeTransform.cpp b/libjulia/backends/evm/EVMCodeTransform.cpp index f92939be..0c7365fb 100644 --- a/libjulia/backends/evm/EVMCodeTransform.cpp +++ b/libjulia/backends/evm/EVMCodeTransform.cpp @@ -77,6 +77,13 @@ void CodeTransform::operator()(StackAssignment const& _assignment) checkStackHeight(&_assignment); } +void CodeTransform::operator()(ExpressionStatement const& _statement) +{ + m_assembly.setSourceLocation(_statement.location); + boost::apply_visitor(*this, _statement.expression); + checkStackHeight(&_statement); +} + void CodeTransform::operator()(Label const& _label) { m_assembly.setSourceLocation(_label.location); @@ -460,7 +467,7 @@ AbstractAssembly::LabelID CodeTransform::functionEntryID(string const& _name, Sc return m_context->functionEntryIDs[&_function]; } -void CodeTransform::visitExpression(Statement const& _expression) +void CodeTransform::visitExpression(Expression const& _expression) { int height = m_assembly.stackHeight(); boost::apply_visitor(*this, _expression); diff --git a/libjulia/backends/evm/EVMCodeTransform.h b/libjulia/backends/evm/EVMCodeTransform.h index 577cc8ba..0f2aaf95 100644 --- a/libjulia/backends/evm/EVMCodeTransform.h +++ b/libjulia/backends/evm/EVMCodeTransform.h @@ -101,6 +101,7 @@ public: void operator()(Identifier const& _identifier); void operator()(FunctionalInstruction const& _instr); void operator()(FunctionCall const&); + void operator()(ExpressionStatement const& _statement); void operator()(Label const& _label); void operator()(StackAssignment const& _assignment); void operator()(Assignment const& _assignment); @@ -118,7 +119,7 @@ private: AbstractAssembly::LabelID labelID(solidity::assembly::Scope::Label const& _label); AbstractAssembly::LabelID functionEntryID(std::string const& _name, solidity::assembly::Scope::Function const& _function); /// Generates code for an expression that is supposed to return a single value. - void visitExpression(Statement const& _expression); + void visitExpression(Expression const& _expression); void visitStatements(std::vector const& _statements); diff --git a/libjulia/optimiser/ASTCopier.cpp b/libjulia/optimiser/ASTCopier.cpp index a461f434..5c47be64 100644 --- a/libjulia/optimiser/ASTCopier.cpp +++ b/libjulia/optimiser/ASTCopier.cpp @@ -37,6 +37,11 @@ Statement ASTCopier::operator()(Instruction const&) return {}; } +Statement ASTCopier::operator()(ExpressionStatement const& _statement) +{ + return ExpressionStatement{ _statement.location, translate(_statement.expression) }; +} + Statement ASTCopier::operator()(VariableDeclaration const& _varDecl) { return VariableDeclaration{ @@ -67,7 +72,7 @@ Statement ASTCopier::operator()(Label const&) return {}; } -Statement ASTCopier::operator()(FunctionCall const& _call) +Expression ASTCopier::operator()(FunctionCall const& _call) { return FunctionCall{ _call.location, @@ -76,7 +81,7 @@ Statement ASTCopier::operator()(FunctionCall const& _call) }; } -Statement ASTCopier::operator()(FunctionalInstruction const& _instruction) +Expression ASTCopier::operator()(FunctionalInstruction const& _instruction) { return FunctionalInstruction{ _instruction.location, @@ -85,12 +90,12 @@ Statement ASTCopier::operator()(FunctionalInstruction const& _instruction) }; } -Statement ASTCopier::operator()(Identifier const& _identifier) +Expression ASTCopier::operator()(Identifier const& _identifier) { return Identifier{_identifier.location, translateIdentifier(_identifier.name)}; } -Statement ASTCopier::operator()(Literal const& _literal) +Expression ASTCopier::operator()(Literal const& _literal) { return translate(_literal); } @@ -140,9 +145,14 @@ Statement ASTCopier::operator ()(Block const& _block) return translate(_block); } +Expression ASTCopier::translate(Expression const& _expression) +{ + return _expression.apply_visitor(static_cast(*this)); +} + Statement ASTCopier::translate(Statement const& _statement) { - return boost::apply_visitor(*this, _statement); + return _statement.apply_visitor(static_cast(*this)); } Block ASTCopier::translate(Block const& _block) diff --git a/libjulia/optimiser/ASTCopier.h b/libjulia/optimiser/ASTCopier.h index 5dde2ce9..36a1ced5 100644 --- a/libjulia/optimiser/ASTCopier.h +++ b/libjulia/optimiser/ASTCopier.h @@ -34,28 +34,55 @@ namespace dev namespace julia { +class ExpressionCopier: public boost::static_visitor +{ +public: + virtual Expression operator()(Literal const& _literal) = 0; + virtual Expression operator()(Identifier const& _identifier) = 0; + virtual Expression operator()(FunctionalInstruction const& _instr) = 0; + virtual Expression operator()(FunctionCall const&) = 0; +}; + +class StatementCopier: public boost::static_visitor +{ +public: + virtual Statement operator()(ExpressionStatement const& _statement) = 0; + virtual Statement operator()(Instruction const& _instruction) = 0; + virtual Statement operator()(Label const& _label) = 0; + virtual Statement operator()(StackAssignment const& _assignment) = 0; + virtual Statement operator()(Assignment const& _assignment) = 0; + virtual Statement operator()(VariableDeclaration const& _varDecl) = 0; + virtual Statement operator()(If const& _if) = 0; + virtual Statement operator()(Switch const& _switch) = 0; + virtual Statement operator()(FunctionDefinition const&) = 0; + virtual Statement operator()(ForLoop const&) = 0; + virtual Statement operator()(Block const& _block) = 0; +}; + /** * Creates a copy of a iulia AST potentially replacing identifier names. * Base class to be extended. */ -class ASTCopier: public boost::static_visitor +class ASTCopier: public ExpressionCopier, public StatementCopier { public: - Statement operator()(Literal const& _literal); - Statement operator()(Instruction const& _instruction); - Statement operator()(Identifier const& _identifier); - Statement operator()(FunctionalInstruction const& _instr); - Statement operator()(FunctionCall const&); - Statement operator()(Label const& _label); - Statement operator()(StackAssignment const& _assignment); - Statement operator()(Assignment const& _assignment); - Statement operator()(VariableDeclaration const& _varDecl); - Statement operator()(If const& _if); - Statement operator()(Switch const& _switch); - Statement operator()(FunctionDefinition const&); - Statement operator()(ForLoop const&); - Statement operator()(Block const& _block); - + virtual Expression operator()(Literal const& _literal) override; + virtual Statement operator()(Instruction const& _instruction) override; + virtual Expression operator()(Identifier const& _identifier) override; + virtual Expression operator()(FunctionalInstruction const& _instr) override; + virtual Expression operator()(FunctionCall const&) override; + virtual Statement operator()(ExpressionStatement const& _statement) override; + virtual Statement operator()(Label const& _label) override; + virtual Statement operator()(StackAssignment const& _assignment) override; + virtual Statement operator()(Assignment const& _assignment) override; + virtual Statement operator()(VariableDeclaration const& _varDecl) override; + virtual Statement operator()(If const& _if) override; + virtual Statement operator()(Switch const& _switch) override; + virtual Statement operator()(FunctionDefinition const&) override; + virtual Statement operator()(ForLoop const&) override; + virtual Statement operator()(Block const& _block) override; + + virtual Expression translate(Expression const& _expression); virtual Statement translate(Statement const& _statement); protected: diff --git a/libjulia/optimiser/ASTWalker.cpp b/libjulia/optimiser/ASTWalker.cpp index 0caef04e..499b4bf2 100644 --- a/libjulia/optimiser/ASTWalker.cpp +++ b/libjulia/optimiser/ASTWalker.cpp @@ -42,6 +42,11 @@ void ASTWalker::operator()(FunctionCall const& _funCall) walkVector(_funCall.arguments | boost::adaptors::reversed); } +void ASTWalker::operator()(ExpressionStatement const& _statement) +{ + boost::apply_visitor(*this, _statement.expression); +} + void ASTWalker::operator()(Assignment const& _assignment) { for (auto const& name: _assignment.variableNames) @@ -100,6 +105,11 @@ void ASTModifier::operator()(FunctionCall& _funCall) walkVector(_funCall.arguments | boost::adaptors::reversed); } +void ASTModifier::operator()(ExpressionStatement& _statement) +{ + boost::apply_visitor(*this, _statement.expression); +} + void ASTModifier::operator()(Assignment& _assignment) { for (auto& name: _assignment.variableNames) diff --git a/libjulia/optimiser/ASTWalker.h b/libjulia/optimiser/ASTWalker.h index 8bd867d5..4652a353 100644 --- a/libjulia/optimiser/ASTWalker.h +++ b/libjulia/optimiser/ASTWalker.h @@ -47,6 +47,7 @@ public: virtual void operator()(Identifier const&) {} virtual void operator()(FunctionalInstruction const& _instr); virtual void operator()(FunctionCall const& _funCall); + virtual void operator()(ExpressionStatement const& _statement); virtual void operator()(Label const&) { solAssert(false, ""); } virtual void operator()(StackAssignment const&) { solAssert(false, ""); } virtual void operator()(Assignment const& _assignment); @@ -77,6 +78,7 @@ public: virtual void operator()(Identifier&) {} virtual void operator()(FunctionalInstruction& _instr); virtual void operator()(FunctionCall& _funCall); + virtual void operator()(ExpressionStatement& _statement); virtual void operator()(Label&) { solAssert(false, ""); } virtual void operator()(StackAssignment&) { solAssert(false, ""); } virtual void operator()(Assignment& _assignment); @@ -98,6 +100,10 @@ protected: { boost::apply_visitor(*this, _st); } + virtual void visit(Expression& _e) + { + boost::apply_visitor(*this, _e); + } }; } diff --git a/libjulia/optimiser/Substitution.cpp b/libjulia/optimiser/Substitution.cpp index a49f1f7a..668b6cb6 100644 --- a/libjulia/optimiser/Substitution.cpp +++ b/libjulia/optimiser/Substitution.cpp @@ -26,14 +26,14 @@ using namespace std; using namespace dev; using namespace dev::julia; -Statement Substitution::translate(Statement const& _statement) +Expression Substitution::translate(Expression const& _expression) { - if (_statement.type() == typeid(Identifier)) + if (_expression.type() == typeid(Identifier)) { - string const& name = boost::get(_statement).name; + string const& name = boost::get(_expression).name; if (m_substitutions.count(name)) // No recursive substitution return ASTCopier().translate(*m_substitutions.at(name)); } - return ASTCopier::translate(_statement); + return ASTCopier::translate(_expression); } diff --git a/libjulia/optimiser/Substitution.h b/libjulia/optimiser/Substitution.h index 10bdf32e..313a08d7 100644 --- a/libjulia/optimiser/Substitution.h +++ b/libjulia/optimiser/Substitution.h @@ -33,18 +33,17 @@ namespace julia /** * Specific AST copier that replaces certain identifiers with expressions. - * Only works on ASTs that are expressions. */ class Substitution: public ASTCopier { public: - Substitution(std::map const& _substitutions): + Substitution(std::map const& _substitutions): m_substitutions(_substitutions) {} - virtual Statement translate(Statement const& _statement) override; + virtual Expression translate(Expression const& _expression) override; private: - std::map const& m_substitutions; + std::map const& m_substitutions; }; } -- cgit v1.2.3 From a6a64eb8ede9e99e67198e662fb77eee44983989 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 4 Dec 2017 16:42:18 +0100 Subject: Function grouper. --- libjulia/optimiser/FunctionGrouper.cpp | 49 ++++++++++++++++++++++++++++++++++ libjulia/optimiser/FunctionGrouper.h | 46 +++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 libjulia/optimiser/FunctionGrouper.cpp create mode 100644 libjulia/optimiser/FunctionGrouper.h (limited to 'libjulia') diff --git a/libjulia/optimiser/FunctionGrouper.cpp b/libjulia/optimiser/FunctionGrouper.cpp new file mode 100644 index 00000000..65bf47f4 --- /dev/null +++ b/libjulia/optimiser/FunctionGrouper.cpp @@ -0,0 +1,49 @@ +/* + 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 . +*/ +/** + * Optimiser component that changes the code of a black so that all non-function definition + * instructions are moved to a block of their own followed by all function definitions. + */ + +#include + +#include + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; +using namespace dev::solidity; + + +void FunctionGrouper::operator()(Block& _block) +{ + vector reordered; + reordered.emplace_back(Block{_block.location, {}}); + + for (auto&& statement: _block.statements) + { + if (statement.type() == typeid(FunctionDefinition)) + reordered.emplace_back(std::move(statement)); + else + boost::get(reordered.front()).statements.emplace_back(std::move(statement)); + } + _block.statements = std::move(reordered); +} diff --git a/libjulia/optimiser/FunctionGrouper.h b/libjulia/optimiser/FunctionGrouper.h new file mode 100644 index 00000000..64a71318 --- /dev/null +++ b/libjulia/optimiser/FunctionGrouper.h @@ -0,0 +1,46 @@ +/* + 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 . +*/ +/** + * Optimiser component that changes the code of a black so that all non-function definition + * instructions are moved to a block of their own followed by all function definitions. + */ + +#pragma once + +#include + +namespace dev +{ +namespace julia +{ + +/** + * Moves all instructions in a block into a new block at the start of the block, followed by + * all function definitions. + * + * After this step, a block is of the form + * { { I...} F... } + * Where I are (non-function-definition) instructions and F are function definitions. + */ +class FunctionGrouper +{ +public: + void operator()(Block& _block); +}; + +} +} -- cgit v1.2.3 From 99c8ed075eccebb5d8897e7a159cea08dd88fb3e Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 4 Dec 2017 16:42:18 +0100 Subject: Function hoister. --- libjulia/optimiser/FunctionHoister.cpp | 59 ++++++++++++++++++++++++++++++++++ libjulia/optimiser/FunctionHoister.h | 52 ++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 libjulia/optimiser/FunctionHoister.cpp create mode 100644 libjulia/optimiser/FunctionHoister.h (limited to 'libjulia') diff --git a/libjulia/optimiser/FunctionHoister.cpp b/libjulia/optimiser/FunctionHoister.cpp new file mode 100644 index 00000000..9cf67f23 --- /dev/null +++ b/libjulia/optimiser/FunctionHoister.cpp @@ -0,0 +1,59 @@ +/* + 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 . +*/ +/** + * Optimiser component that changes the code so that it consists of a block starting with + * a single block followed only by function definitions and with no functions defined + * anywhere else. + */ + +#include + +#include + +#include + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; +using namespace dev::solidity; + + +void FunctionHoister::operator()(Block& _block) +{ + bool topLevel = m_isTopLevel; + m_isTopLevel = false; + for (auto&& statement: _block.statements) + { + boost::apply_visitor(*this, statement); + if (statement.type() == typeid(FunctionDefinition)) + { + m_functions.emplace_back(std::move(statement)); + statement = Block{_block.location, {}}; + } + } + auto isEmptyBlock = [](Statement const& _st) -> bool { + return _st.type() == typeid(Block) && boost::get(_st).statements.empty(); + }; + // Remove empty blocks + boost::range::remove_erase_if(_block.statements, isEmptyBlock); + if (topLevel) + _block.statements += std::move(m_functions); +} diff --git a/libjulia/optimiser/FunctionHoister.h b/libjulia/optimiser/FunctionHoister.h new file mode 100644 index 00000000..f9f8bce0 --- /dev/null +++ b/libjulia/optimiser/FunctionHoister.h @@ -0,0 +1,52 @@ +/* + 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 . +*/ +/** + * Optimiser component that changes the code so that all function definitions are at the top + * level block. + */ + +#pragma once + +#include + +#include + +namespace dev +{ +namespace julia +{ + +/** + * Moves all functions to the top-level scope. + * Applying this transformation to source code that has ambiguous identifiers might + * lead to invalid code. + * + * Prerequisites: Disambiguator + */ +class FunctionHoister: public ASTModifier +{ +public: + using ASTModifier::operator(); + virtual void operator()(Block& _block); + +private: + bool m_isTopLevel = true; + std::vector m_functions; +}; + +} +} -- cgit v1.2.3 From 7755e64872ec9ff506483bb3b59af7a76368b6dc Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 18 Dec 2017 14:56:56 +0100 Subject: Fixed typos in comment. --- libjulia/optimiser/FunctionGrouper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'libjulia') diff --git a/libjulia/optimiser/FunctionGrouper.cpp b/libjulia/optimiser/FunctionGrouper.cpp index 65bf47f4..cc40bc46 100644 --- a/libjulia/optimiser/FunctionGrouper.cpp +++ b/libjulia/optimiser/FunctionGrouper.cpp @@ -15,8 +15,8 @@ along with solidity. If not, see . */ /** - * Optimiser component that changes the code of a black so that all non-function definition - * instructions are moved to a block of their own followed by all function definitions. + * Optimiser component that changes the code of a block so that all non-function definition + * statements are moved to a block of their own followed by all function definitions. */ #include -- cgit v1.2.3 From 937b95cbe5bcef6c1324c380f37629e5a2a5811a Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 20 Dec 2017 14:12:52 +0100 Subject: Use explicit visit function for the walker. --- libjulia/optimiser/ASTWalker.cpp | 14 +++++++------- libjulia/optimiser/ASTWalker.h | 26 ++++++++++++++++++-------- 2 files changed, 25 insertions(+), 15 deletions(-) (limited to 'libjulia') diff --git a/libjulia/optimiser/ASTWalker.cpp b/libjulia/optimiser/ASTWalker.cpp index 499b4bf2..6386b29d 100644 --- a/libjulia/optimiser/ASTWalker.cpp +++ b/libjulia/optimiser/ASTWalker.cpp @@ -44,31 +44,31 @@ void ASTWalker::operator()(FunctionCall const& _funCall) void ASTWalker::operator()(ExpressionStatement const& _statement) { - boost::apply_visitor(*this, _statement.expression); + visit(_statement.expression); } void ASTWalker::operator()(Assignment const& _assignment) { for (auto const& name: _assignment.variableNames) (*this)(name); - boost::apply_visitor(*this, *_assignment.value); + visit(*_assignment.value); } void ASTWalker::operator()(VariableDeclaration const& _varDecl) { if (_varDecl.value) - boost::apply_visitor(*this, *_varDecl.value); + visit(*_varDecl.value); } void ASTWalker::operator()(If const& _if) { - boost::apply_visitor(*this, *_if.condition); + visit(*_if.condition); (*this)(_if.body); } void ASTWalker::operator()(Switch const& _switch) { - boost::apply_visitor(*this, *_switch.expression); + visit(*_switch.expression); for (auto const& _case: _switch.cases) { if (_case.value) @@ -85,7 +85,7 @@ void ASTWalker::operator()(FunctionDefinition const& _fun) void ASTWalker::operator()(ForLoop const& _for) { (*this)(_for.pre); - boost::apply_visitor(*this, *_for.condition); + visit(*_for.condition); (*this)(_for.post); (*this)(_for.body); } @@ -107,7 +107,7 @@ void ASTModifier::operator()(FunctionCall& _funCall) void ASTModifier::operator()(ExpressionStatement& _statement) { - boost::apply_visitor(*this, _statement.expression); + visit(_statement.expression); } void ASTModifier::operator()(Assignment& _assignment) diff --git a/libjulia/optimiser/ASTWalker.h b/libjulia/optimiser/ASTWalker.h index 4652a353..dbf8194b 100644 --- a/libjulia/optimiser/ASTWalker.h +++ b/libjulia/optimiser/ASTWalker.h @@ -58,12 +58,21 @@ 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); + } + protected: template void walkVector(T const& _statements) { for (auto const& st: _statements) - boost::apply_visitor(*this, st); + visit(st); } }; @@ -89,13 +98,6 @@ public: virtual void operator()(ForLoop&); virtual void operator()(Block& _block); -protected: - template - void walkVector(T&& _statements) - { - for (auto& st: _statements) - visit(st); - } virtual void visit(Statement& _st) { boost::apply_visitor(*this, _st); @@ -104,6 +106,14 @@ protected: { boost::apply_visitor(*this, _e); } + +protected: + template + void walkVector(T&& _statements) + { + for (auto& st: _statements) + visit(st); + } }; } -- cgit v1.2.3 From 95cf9266abafc07bb4d7b33dff6932c191c36970 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 22 Nov 2017 11:11:48 +0100 Subject: Movability. --- libjulia/optimiser/Disambiguator.h | 1 - libjulia/optimiser/Semantics.cpp | 60 ++++++++++++++++++++++++++++++++++++ libjulia/optimiser/Semantics.h | 62 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 libjulia/optimiser/Semantics.cpp create mode 100644 libjulia/optimiser/Semantics.h (limited to 'libjulia') diff --git a/libjulia/optimiser/Disambiguator.h b/libjulia/optimiser/Disambiguator.h index cc9488d5..18ffd157 100644 --- a/libjulia/optimiser/Disambiguator.h +++ b/libjulia/optimiser/Disambiguator.h @@ -35,7 +35,6 @@ namespace dev { namespace julia { -class EVMAssembly; /** * Creates a copy of a iulia AST replacing all identifiers by unique names. diff --git a/libjulia/optimiser/Semantics.cpp b/libjulia/optimiser/Semantics.cpp new file mode 100644 index 00000000..92728c46 --- /dev/null +++ b/libjulia/optimiser/Semantics.cpp @@ -0,0 +1,60 @@ +/*( + 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 . +*/ +/** + * Specific AST walkers that collect semantical facts. + */ + +#include + +#include + +#include + +#include + +using namespace std; +using namespace dev; +using namespace dev::julia; + +MovableChecker::MovableChecker(Expression const& _expression) +{ + visit(_expression); +} + +void MovableChecker::operator()(Identifier const& _identifier) +{ + ASTWalker::operator()(_identifier); + m_variableReferences.insert(_identifier.name); +} + +void MovableChecker::operator()(FunctionalInstruction const& _instr) +{ + if (!eth::SemanticInformation::movable(_instr.instruction)) + m_movable = false; + else + ASTWalker::operator()(_instr); +} + +void MovableChecker::operator()(FunctionCall const&) +{ + m_movable = false; +} + +void MovableChecker::visit(Statement const&) +{ + solAssert(false, "Movability for statement requested."); +} diff --git a/libjulia/optimiser/Semantics.h b/libjulia/optimiser/Semantics.h new file mode 100644 index 00000000..6df5f01a --- /dev/null +++ b/libjulia/optimiser/Semantics.h @@ -0,0 +1,62 @@ +/* + 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 . +*/ +/** + * Specific AST walkers that collect semantical facts. + */ + +#pragma once + +#include + +#include +#include +#include + +namespace dev +{ +namespace julia +{ + +/** + * Specific AST walker that determines whether an expression is movable. + */ +class MovableChecker: public ASTWalker +{ +public: + MovableChecker() = default; + explicit MovableChecker(Expression const& _expression); + + virtual void operator()(Identifier const& _identifier) override; + virtual void operator()(FunctionalInstruction const& _functionalInstruction) override; + virtual void operator()(FunctionCall const& _functionCall) override; + + /// Disallow visiting anything apart from Expressions (this throws). + virtual void visit(Statement const&) override; + using ASTWalker::visit; + + bool movable() const { return m_movable; } + std::set const& referencedVariables() const { return m_variableReferences; } + +private: + /// Which variables the current expression references. + std::set m_variableReferences; + /// Is the current expression movable or not. + bool m_movable = true; +}; + +} +} -- cgit v1.2.3