2018-02-02 14:23:44 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Base class to perform data flow analysis during AST walks.
|
|
|
|
* Tracks assignments and is used as base class for both Rematerialiser and
|
|
|
|
* Common Subexpression Eliminator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-10-15 09:52:35 +00:00
|
|
|
#include <libyul/optimiser/ASTWalker.h>
|
2018-10-29 14:12:02 +00:00
|
|
|
#include <libyul/YulString.h>
|
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2018-10-15 09:58:51 +00:00
|
|
|
namespace yul
|
2018-02-02 14:23:44 +00:00
|
|
|
{
|
2018-12-20 17:55:32 +00:00
|
|
|
struct Dialect;
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class to perform data flow analysis during AST walks.
|
|
|
|
* Tracks assignments and is used as base class for both Rematerialiser and
|
|
|
|
* Common Subexpression Eliminator.
|
|
|
|
*
|
2018-12-11 23:49:36 +00:00
|
|
|
* A special zero constant expression is used for the default value of variables.
|
|
|
|
*
|
2018-02-02 14:23:44 +00:00
|
|
|
* Prerequisite: Disambiguator
|
|
|
|
*/
|
|
|
|
class DataFlowAnalyzer: public ASTModifier
|
|
|
|
{
|
|
|
|
public:
|
2018-12-20 17:55:32 +00:00
|
|
|
explicit DataFlowAnalyzer(Dialect const& _dialect): m_dialect(_dialect) {}
|
|
|
|
|
2018-02-02 14:23:44 +00:00
|
|
|
using ASTModifier::operator();
|
2018-11-16 01:09:04 +00:00
|
|
|
void operator()(Assignment& _assignment) override;
|
|
|
|
void operator()(VariableDeclaration& _varDecl) override;
|
|
|
|
void operator()(If& _if) override;
|
|
|
|
void operator()(Switch& _switch) override;
|
|
|
|
void operator()(FunctionDefinition&) override;
|
|
|
|
void operator()(ForLoop&) override;
|
|
|
|
void operator()(Block& _block) override;
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Registers the assignment.
|
2018-10-29 14:12:02 +00:00
|
|
|
void handleAssignment(std::set<YulString> const& _names, Expression* _value);
|
2018-02-02 14:23:44 +00:00
|
|
|
|
2018-10-18 12:45:11 +00:00
|
|
|
/// Creates a new inner scope.
|
|
|
|
void pushScope(bool _functionScope);
|
|
|
|
|
|
|
|
/// Removes the innermost scope and clears all variables in it.
|
|
|
|
void popScope();
|
|
|
|
|
2018-07-10 07:18:59 +00:00
|
|
|
/// Clears information about the values assigned to the given variables,
|
2018-02-02 14:23:44 +00:00
|
|
|
/// for example at points where control flow is merged.
|
2018-10-29 14:12:02 +00:00
|
|
|
void clearValues(std::set<YulString> _names);
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
/// Returns true iff the variable is in scope.
|
2018-10-29 14:12:02 +00:00
|
|
|
bool inScope(YulString _variableName) const;
|
2018-02-02 14:23:44 +00:00
|
|
|
|
|
|
|
/// Current values of variables, always movable.
|
2018-10-29 14:12:02 +00:00
|
|
|
std::map<YulString, Expression const*> m_value;
|
2018-02-02 14:23:44 +00:00
|
|
|
/// m_references[a].contains(b) <=> the current expression assigned to a references b
|
2018-10-29 14:12:02 +00:00
|
|
|
std::map<YulString, std::set<YulString>> m_references;
|
2018-02-02 14:23:44 +00:00
|
|
|
/// m_referencedBy[b].contains(a) <=> the current expression assigned to a references b
|
2018-10-29 14:12:02 +00:00
|
|
|
std::map<YulString, std::set<YulString>> m_referencedBy;
|
2018-02-05 17:02:32 +00:00
|
|
|
|
|
|
|
struct Scope
|
|
|
|
{
|
|
|
|
explicit Scope(bool _isFunction): isFunction(_isFunction) {}
|
2018-10-29 14:12:02 +00:00
|
|
|
std::set<YulString> variables;
|
2018-02-05 17:02:32 +00:00
|
|
|
bool isFunction;
|
|
|
|
};
|
|
|
|
/// List of scopes.
|
|
|
|
std::vector<Scope> m_variableScopes;
|
2018-12-20 17:55:32 +00:00
|
|
|
Dialect const& m_dialect;
|
2018-02-02 14:23:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|