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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
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/>.
*/
/**
* Optimiser component that performs function inlining for arbitrary functions.
*/
#pragma once
#include <libjulia/ASTDataForward.h>
#include <libjulia/optimiser/ASTCopier.h>
#include <libjulia/optimiser/ASTWalker.h>
#include <libjulia/optimiser/NameDispenser.h>
#include <libsolidity/interface/Exceptions.h>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
#include <set>
namespace dev
{
namespace julia
{
class NameCollector;
/**
* Optimiser component that modifies an AST in place, inlining arbitrary functions.
*
* Code of the form
*
* function f(a, b) -> c { ... }
* h(g(x(...), f(arg1(...), arg2(...)), y(...)), z(...))
*
* is transformed into
*
* function f(a, b) -> c { ... }
*
* let z1 := z(...) let y1 := y(...) let a2 := arg2(...) let a1 := arg1(...)
* let c1 := 0
* { code of f, with replacements: a -> a1, b -> a2, c -> c1, d -> d1 }
* h(g(x(...), c1, y1), z1)
*
* No temporary variable is created for expressions that are "movable"
* (i.e. they are "pure", have no side-effects and also do not depend on other code
* that might have side-effects).
*
* This component can only be used on sources with unique names and with hoisted functions,
* i.e. the root node has to be a block that itself contains a single block followed by all
* function definitions.
*/
class FullInliner: public ASTModifier
{
public:
explicit FullInliner(Block& _ast);
void run();
/// Perform inlining operations inside the given function.
void handleFunction(FunctionDefinition& _function);
FunctionDefinition& function(std::string _name) { return *m_functions.at(_name); }
private:
/// The AST to be modified. The root block itself will not be modified, because
/// we store pointers to functions.
Block& m_ast;
std::map<std::string, FunctionDefinition*> m_functions;
std::set<FunctionDefinition*> m_functionsToVisit;
NameDispenser m_nameDispenser;
};
/**
* Class that walks the AST of a block that does not contain function definitions and perform
* the actual code modifications.
*/
class InlineModifier: public ASTModifier
{
public:
InlineModifier(FullInliner& _driver, NameDispenser& _nameDispenser, std::string _functionName):
m_currentFunction(std::move(_functionName)),
m_driver(_driver),
m_nameDispenser(_nameDispenser)
{ }
~InlineModifier()
{
solAssert(m_statementsToPrefix.empty(), "");
}
virtual void operator()(FunctionalInstruction&) override;
virtual void operator()(FunctionCall&) override;
virtual void operator()(ForLoop&) override;
virtual void operator()(Block& _block) override;
using ASTModifier::visit;
virtual void visit(Expression& _expression) override;
virtual void visit(Statement& _st) override;
private:
/// Visits a list of expressions (usually an argument list to a function call) and tries
/// to inline them. If one of them is inlined, all right of it have to be moved to the front
/// (to keep the order of evaluation). If @a _moveToFront is true, all elements are moved
/// to the front. @a _nameHints and @_types are used for the newly created variables, but
/// both can be empty.
void visitArguments(
std::vector<Expression>& _arguments,
std::vector<std::string> const& _nameHints = {},
std::vector<std::string> const& _types = {},
bool _moveToFront = false
);
/// Visits an expression, but saves and restores the current statements to prefix and returns
/// the statements that should be prefixed for @a _expression.
std::vector<Statement> visitRecursively(Expression& _expression);
std::string newName(std::string const& _prefix);
/// @returns an expression returning nothing.
Expression noop(SourceLocation const& _location);
/// List of statements that should go in front of the currently visited AST element,
/// at the statement level.
std::vector<Statement> m_statementsToPrefix;
std::string m_currentFunction;
FullInliner& m_driver;
NameDispenser& m_nameDispenser;
};
/**
* Creates a copy of a block that is supposed to be the body of a function.
* Applies replacements to referenced variables and creates new names for
* variable declarations.
*/
class BodyCopier: public ASTCopier
{
public:
BodyCopier(
NameDispenser& _nameDispenser,
std::string const& _varNamePrefix,
std::map<std::string, std::string> const& _variableReplacements
):
m_nameDispenser(_nameDispenser),
m_varNamePrefix(_varNamePrefix),
m_variableReplacements(_variableReplacements)
{}
using ASTCopier::operator ();
virtual Statement operator()(VariableDeclaration const& _varDecl) override;
virtual Statement operator()(FunctionDefinition const& _funDef) override;
virtual std::string translateIdentifier(std::string const& _name) override;
NameDispenser& m_nameDispenser;
std::string const& m_varNamePrefix;
std::map<std::string, std::string> m_variableReplacements;
};
}
}
|