-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathControlFlowSideEffectsCollector.h
139 lines (113 loc) · 4.66 KB
/
ControlFlowSideEffectsCollector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/ControlFlowSideEffects.h>
#include <set>
#include <stack>
#include <optional>
#include <list>
namespace solidity::yul
{
class Dialect;
struct ControlFlowNode
{
std::vector<ControlFlowNode const*> successors;
/// Function call AST node, if present.
FunctionCall const* functionCall = nullptr;
};
/**
* The control flow of a function with entry and exit nodes.
*/
struct FunctionFlow
{
ControlFlowNode const* entry;
ControlFlowNode const* exit;
};
/**
* Requires: Disambiguator, Function Hoister.
*/
class ControlFlowBuilder: private ASTWalker
{
public:
/// Computes the control-flows of all function defined in the block.
/// Assumes the functions are hoisted to the topmost block.
explicit ControlFlowBuilder(Block const& _ast);
std::map<FunctionDefinition const*, FunctionFlow> const& functionFlows() const { return m_functionFlows; }
private:
using ASTWalker::operator();
void operator()(FunctionCall const& _functionCall) override;
void operator()(If const& _if) override;
void operator()(Switch const& _switch) override;
void operator()(FunctionDefinition const& _functionDefinition) override;
void operator()(ForLoop const& _forLoop) override;
void operator()(Break const& _break) override;
void operator()(Continue const& _continue) override;
void operator()(Leave const& _leaveStatement) override;
void newConnectedNode();
ControlFlowNode* newNode();
std::vector<std::shared_ptr<ControlFlowNode>> m_nodes;
ControlFlowNode* m_currentNode = nullptr;
ControlFlowNode const* m_leave = nullptr;
ControlFlowNode const* m_break = nullptr;
ControlFlowNode const* m_continue = nullptr;
std::map<FunctionDefinition const*, FunctionFlow> m_functionFlows;
};
/**
* Computes control-flow side-effects for user-defined functions.
* Source does not have to be disambiguated, unless you want the side-effects
* based on function names.
*/
class ControlFlowSideEffectsCollector
{
public:
explicit ControlFlowSideEffectsCollector(
Dialect const& _dialect,
Block const& _ast
);
std::map<FunctionDefinition const*, ControlFlowSideEffects> const& functionSideEffects() const
{
return m_functionSideEffects;
}
/// Returns the side effects by function name, requires unique function names.
std::map<YulName, ControlFlowSideEffects> functionSideEffectsNamed() const;
private:
/// @returns false if nothing could be processed.
bool processFunction(FunctionDefinition const& _function);
/// @returns the next pending node of the function that is not
/// a function call to a function that might not continue.
/// De-queues the node or returns nullptr if no such node is found.
ControlFlowNode const* nextProcessableNode(FunctionDefinition const& _function);
/// @returns the side-effects of either a builtin call or a user defined function
/// call (as far as already computed).
ControlFlowSideEffects const& sideEffects(FunctionCall const& _call) const;
/// Queues the given node to be processed (if not already visited)
/// and if it is a function call, records that `_functionName` calls
/// `*_node->functionCall`.
void recordReachabilityAndQueue(FunctionDefinition const& _function, ControlFlowNode const* _node);
Dialect const& m_dialect;
ControlFlowBuilder m_cfgBuilder;
/// Function references, but only for calls to user-defined functions.
std::map<FunctionCall const*, FunctionDefinition const*> m_functionReferences;
/// Side effects of user-defined functions, is being constructod.
std::map<FunctionDefinition const*, ControlFlowSideEffects> m_functionSideEffects;
/// Control flow nodes still to process, per function.
std::map<FunctionDefinition const*, std::list<ControlFlowNode const*>> m_pendingNodes;
/// Control flow nodes already processed, per function.
std::map<FunctionDefinition const*, std::set<ControlFlowNode const*>> m_processedNodes;
/// Set of reachable function calls nodes in each function (including calls to builtins).
std::map<FunctionDefinition const*, std::set<FunctionCall const*>> m_functionCalls;
};
}