2021-06-11 17:17:41 +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/>.
|
|
|
|
*/
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
/**
|
|
|
|
* Transformation of a Yul AST into a control flow graph.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libyul/backends/evm/ControlFlowGraphBuilder.h>
|
|
|
|
#include <libyul/AST.h>
|
|
|
|
#include <libyul/Exceptions.h>
|
|
|
|
#include <libyul/Utilities.h>
|
2021-10-13 15:52:08 +00:00
|
|
|
#include <libyul/ControlFlowSideEffectsCollector.h>
|
2021-06-11 17:17:41 +00:00
|
|
|
|
|
|
|
#include <libsolutil/cxx20.h>
|
|
|
|
#include <libsolutil/Visitor.h>
|
|
|
|
#include <libsolutil/Algorithms.h>
|
|
|
|
|
|
|
|
#include <range/v3/action/push_back.hpp>
|
|
|
|
#include <range/v3/action/erase.hpp>
|
|
|
|
#include <range/v3/range/conversion.hpp>
|
|
|
|
#include <range/v3/view/concat.hpp>
|
|
|
|
#include <range/v3/view/drop_last.hpp>
|
|
|
|
#include <range/v3/view/enumerate.hpp>
|
|
|
|
#include <range/v3/view/filter.hpp>
|
|
|
|
#include <range/v3/view/iota.hpp>
|
|
|
|
#include <range/v3/view/map.hpp>
|
|
|
|
#include <range/v3/view/reverse.hpp>
|
|
|
|
#include <range/v3/view/single.hpp>
|
|
|
|
#include <range/v3/view/take_last.hpp>
|
|
|
|
#include <range/v3/view/transform.hpp>
|
|
|
|
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::yul;
|
|
|
|
|
2021-08-19 11:41:20 +00:00
|
|
|
namespace
|
|
|
|
{
|
2022-01-14 12:37:40 +00:00
|
|
|
/// Removes edges to blocks that are not reachable.
|
2021-08-19 11:41:20 +00:00
|
|
|
void cleanUnreachable(CFG& _cfg)
|
2021-06-11 17:17:41 +00:00
|
|
|
{
|
|
|
|
// Determine which blocks are reachable from the entry.
|
2021-08-19 11:41:20 +00:00
|
|
|
util::BreadthFirstSearch<CFG::BasicBlock*> reachabilityCheck{{_cfg.entry}};
|
|
|
|
for (auto const& functionInfo: _cfg.functionInfo | ranges::views::values)
|
2021-06-11 17:17:41 +00:00
|
|
|
reachabilityCheck.verticesToTraverse.emplace_back(functionInfo.entry);
|
|
|
|
|
|
|
|
reachabilityCheck.run([&](CFG::BasicBlock* _node, auto&& _addChild) {
|
|
|
|
visit(util::GenericVisitor{
|
|
|
|
[&](CFG::BasicBlock::Jump const& _jump) {
|
|
|
|
_addChild(_jump.target);
|
|
|
|
},
|
|
|
|
[&](CFG::BasicBlock::ConditionalJump const& _jump) {
|
|
|
|
_addChild(_jump.zero);
|
|
|
|
_addChild(_jump.nonZero);
|
|
|
|
},
|
|
|
|
[](CFG::BasicBlock::FunctionReturn const&) {},
|
|
|
|
[](CFG::BasicBlock::Terminated const&) {},
|
|
|
|
[](CFG::BasicBlock::MainExit const&) {}
|
|
|
|
}, _node->exit);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove all entries from unreachable nodes from the graph.
|
|
|
|
for (CFG::BasicBlock* node: reachabilityCheck.visited)
|
|
|
|
cxx20::erase_if(node->entries, [&](CFG::BasicBlock* entry) -> bool {
|
|
|
|
return !reachabilityCheck.visited.count(entry);
|
|
|
|
});
|
2021-08-19 11:41:20 +00:00
|
|
|
}
|
2022-01-14 12:37:40 +00:00
|
|
|
|
|
|
|
/// Sets the ``recursive`` member to ``true`` for all recursive function calls.
|
2021-08-19 11:41:20 +00:00
|
|
|
void markRecursiveCalls(CFG& _cfg)
|
|
|
|
{
|
2023-08-23 02:24:09 +00:00
|
|
|
std::map<CFG::BasicBlock*, std::vector<CFG::FunctionCall*>> callsPerBlock;
|
2021-08-19 11:41:20 +00:00
|
|
|
auto const& findCalls = [&](CFG::BasicBlock* _block)
|
|
|
|
{
|
|
|
|
if (auto* calls = util::valueOrNullptr(callsPerBlock, _block))
|
|
|
|
return *calls;
|
2023-08-23 02:24:09 +00:00
|
|
|
std::vector<CFG::FunctionCall*>& calls = callsPerBlock[_block];
|
2021-08-19 11:41:20 +00:00
|
|
|
util::BreadthFirstSearch<CFG::BasicBlock*>{{_block}}.run([&](CFG::BasicBlock* _block, auto _addChild) {
|
|
|
|
for (auto& operation: _block->operations)
|
2023-08-23 02:24:09 +00:00
|
|
|
if (auto* functionCall = std::get_if<CFG::FunctionCall>(&operation.operation))
|
2021-08-19 11:41:20 +00:00
|
|
|
calls.emplace_back(functionCall);
|
|
|
|
std::visit(util::GenericVisitor{
|
|
|
|
[&](CFG::BasicBlock::MainExit const&) {},
|
|
|
|
[&](CFG::BasicBlock::Jump const& _jump)
|
|
|
|
{
|
|
|
|
_addChild(_jump.target);
|
|
|
|
},
|
|
|
|
[&](CFG::BasicBlock::ConditionalJump const& _conditionalJump)
|
|
|
|
{
|
|
|
|
_addChild(_conditionalJump.zero);
|
|
|
|
_addChild(_conditionalJump.nonZero);
|
|
|
|
},
|
|
|
|
[&](CFG::BasicBlock::FunctionReturn const&) {},
|
|
|
|
[&](CFG::BasicBlock::Terminated const&) {},
|
|
|
|
}, _block->exit);
|
|
|
|
});
|
|
|
|
return calls;
|
|
|
|
};
|
|
|
|
for (auto& functionInfo: _cfg.functionInfo | ranges::views::values)
|
|
|
|
for (CFG::FunctionCall* call: findCalls(functionInfo.entry))
|
|
|
|
{
|
|
|
|
util::BreadthFirstSearch<CFG::FunctionCall*> breadthFirstSearch{{call}};
|
|
|
|
breadthFirstSearch.run([&](CFG::FunctionCall* _call, auto _addChild) {
|
|
|
|
auto& calledFunctionInfo = _cfg.functionInfo.at(&_call->function.get());
|
|
|
|
if (&calledFunctionInfo == &functionInfo)
|
|
|
|
{
|
|
|
|
call->recursive = true;
|
|
|
|
breadthFirstSearch.abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (CFG::FunctionCall* nestedCall: findCalls(_cfg.functionInfo.at(&_call->function.get()).entry))
|
|
|
|
_addChild(nestedCall);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 12:37:40 +00:00
|
|
|
|
|
|
|
/// Marks each cut-vertex in the CFG, i.e. each block that begins a disconnected sub-graph of the CFG.
|
|
|
|
/// Entering such a block means that control flow will never return to a previously visited block.
|
|
|
|
void markStartsOfSubGraphs(CFG& _cfg)
|
|
|
|
{
|
2023-08-23 02:24:09 +00:00
|
|
|
std::vector<CFG::BasicBlock*> entries;
|
2022-01-14 12:37:40 +00:00
|
|
|
entries.emplace_back(_cfg.entry);
|
|
|
|
for (auto&& functionInfo: _cfg.functionInfo | ranges::views::values)
|
|
|
|
entries.emplace_back(functionInfo.entry);
|
|
|
|
for (auto& entry: entries)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Detect bridges following Algorithm 1 in https://arxiv.org/pdf/2108.07346.pdf
|
|
|
|
* and mark the bridge targets as starts of sub-graphs.
|
|
|
|
*/
|
2023-08-23 02:24:09 +00:00
|
|
|
std::set<CFG::BasicBlock*> visited;
|
|
|
|
std::map<CFG::BasicBlock*, size_t> disc;
|
|
|
|
std::map<CFG::BasicBlock*, size_t> low;
|
|
|
|
std::map<CFG::BasicBlock*, CFG::BasicBlock*> parent;
|
2022-01-14 12:37:40 +00:00
|
|
|
size_t time = 0;
|
|
|
|
auto dfs = [&](CFG::BasicBlock* _u, auto _recurse) -> void {
|
|
|
|
visited.insert(_u);
|
|
|
|
disc[_u] = low[_u] = time;
|
|
|
|
time++;
|
|
|
|
|
2023-08-23 02:24:09 +00:00
|
|
|
std::vector<CFG::BasicBlock*> children = _u->entries;
|
2022-01-14 12:37:40 +00:00
|
|
|
visit(util::GenericVisitor{
|
|
|
|
[&](CFG::BasicBlock::Jump const& _jump) {
|
|
|
|
children.emplace_back(_jump.target);
|
|
|
|
},
|
|
|
|
[&](CFG::BasicBlock::ConditionalJump const& _jump) {
|
|
|
|
children.emplace_back(_jump.zero);
|
|
|
|
children.emplace_back(_jump.nonZero);
|
|
|
|
},
|
|
|
|
[&](CFG::BasicBlock::FunctionReturn const&) {},
|
|
|
|
[&](CFG::BasicBlock::Terminated const&) { _u->isStartOfSubGraph = true; },
|
|
|
|
[&](CFG::BasicBlock::MainExit const&) { _u->isStartOfSubGraph = true; }
|
|
|
|
}, _u->exit);
|
|
|
|
yulAssert(!util::contains(children, _u));
|
|
|
|
|
|
|
|
for (CFG::BasicBlock* v: children)
|
|
|
|
if (!visited.count(v))
|
|
|
|
{
|
|
|
|
parent[v] = _u;
|
|
|
|
_recurse(v, _recurse);
|
2023-08-23 02:24:09 +00:00
|
|
|
low[_u] = std::min(low[_u], low[v]);
|
2022-01-14 12:37:40 +00:00
|
|
|
if (low[v] > disc[_u])
|
|
|
|
{
|
|
|
|
// _u <-> v is a cut edge in the undirected graph
|
|
|
|
bool edgeVtoU = util::contains(_u->entries, v);
|
|
|
|
bool edgeUtoV = util::contains(v->entries, _u);
|
|
|
|
if (edgeVtoU && !edgeUtoV)
|
|
|
|
// Cut edge v -> _u
|
|
|
|
_u->isStartOfSubGraph = true;
|
|
|
|
else if (edgeUtoV && !edgeVtoU)
|
|
|
|
// Cut edge _u -> v
|
|
|
|
v->isStartOfSubGraph = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (v != parent[_u])
|
2023-08-23 02:24:09 +00:00
|
|
|
low[_u] = std::min(low[_u], disc[v]);
|
2022-01-14 12:37:40 +00:00
|
|
|
};
|
|
|
|
dfs(entry, dfs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Marks each block that needs to maintain a clean stack. That is each block that has an outgoing
|
|
|
|
/// path to a function return.
|
|
|
|
void markNeedsCleanStack(CFG& _cfg)
|
|
|
|
{
|
|
|
|
for (auto& functionInfo: _cfg.functionInfo | ranges::views::values)
|
|
|
|
for (CFG::BasicBlock* exit: functionInfo.exits)
|
|
|
|
util::BreadthFirstSearch<CFG::BasicBlock*>{{exit}}.run([&](CFG::BasicBlock* _block, auto _addChild) {
|
|
|
|
_block->needsCleanStack = true;
|
|
|
|
for (CFG::BasicBlock* entry: _block->entries)
|
|
|
|
_addChild(entry);
|
|
|
|
});
|
|
|
|
}
|
2021-08-19 11:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<CFG> ControlFlowGraphBuilder::build(
|
|
|
|
AsmAnalysisInfo const& _analysisInfo,
|
|
|
|
Dialect const& _dialect,
|
|
|
|
Block const& _block
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto result = std::make_unique<CFG>();
|
2021-09-09 14:49:36 +00:00
|
|
|
result->entry = &result->makeBlock(debugDataOf(_block));
|
2021-08-19 11:41:20 +00:00
|
|
|
|
2021-10-13 15:52:08 +00:00
|
|
|
ControlFlowSideEffectsCollector sideEffects(_dialect, _block);
|
|
|
|
ControlFlowGraphBuilder builder(*result, _analysisInfo, sideEffects.functionSideEffects(), _dialect);
|
2021-08-19 11:41:20 +00:00
|
|
|
builder.m_currentBlock = result->entry;
|
|
|
|
builder(_block);
|
|
|
|
|
|
|
|
cleanUnreachable(*result);
|
|
|
|
markRecursiveCalls(*result);
|
2022-01-14 12:37:40 +00:00
|
|
|
markStartsOfSubGraphs(*result);
|
|
|
|
markNeedsCleanStack(*result);
|
2021-06-11 17:17:41 +00:00
|
|
|
|
|
|
|
// TODO: It might be worthwhile to run some further simplifications on the graph itself here.
|
|
|
|
// E.g. if there is a jump to a node that has the jumping node as its only entry, the nodes can be fused, etc.
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlFlowGraphBuilder::ControlFlowGraphBuilder(
|
|
|
|
CFG& _graph,
|
|
|
|
AsmAnalysisInfo const& _analysisInfo,
|
2023-08-23 02:24:09 +00:00
|
|
|
std::map<FunctionDefinition const*, ControlFlowSideEffects> const& _functionSideEffects,
|
2021-06-11 17:17:41 +00:00
|
|
|
Dialect const& _dialect
|
|
|
|
):
|
|
|
|
m_graph(_graph),
|
|
|
|
m_info(_analysisInfo),
|
2021-10-13 15:52:08 +00:00
|
|
|
m_functionSideEffects(_functionSideEffects),
|
2021-06-11 17:17:41 +00:00
|
|
|
m_dialect(_dialect)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StackSlot ControlFlowGraphBuilder::operator()(Literal const& _literal)
|
|
|
|
{
|
|
|
|
return LiteralSlot{valueOfLiteral(_literal), _literal.debugData};
|
|
|
|
}
|
|
|
|
|
|
|
|
StackSlot ControlFlowGraphBuilder::operator()(Identifier const& _identifier)
|
|
|
|
{
|
|
|
|
return VariableSlot{lookupVariable(_identifier.name), _identifier.debugData};
|
|
|
|
}
|
|
|
|
|
|
|
|
StackSlot ControlFlowGraphBuilder::operator()(Expression const& _expression)
|
|
|
|
{
|
|
|
|
return std::visit(*this, _expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
StackSlot ControlFlowGraphBuilder::operator()(FunctionCall const& _call)
|
|
|
|
{
|
2021-11-02 16:48:59 +00:00
|
|
|
Stack const& output = visitFunctionCall(_call);
|
|
|
|
yulAssert(output.size() == 1, "");
|
|
|
|
return output.front();
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraphBuilder::operator()(VariableDeclaration const& _varDecl)
|
|
|
|
{
|
|
|
|
yulAssert(m_currentBlock, "");
|
|
|
|
auto declaredVariables = _varDecl.variables | ranges::views::transform([&](TypedName const& _var) {
|
|
|
|
return VariableSlot{lookupVariable(_var.name), _var.debugData};
|
2023-08-23 02:24:09 +00:00
|
|
|
}) | ranges::to<std::vector<VariableSlot>>;
|
2021-06-11 17:17:41 +00:00
|
|
|
Stack input;
|
|
|
|
if (_varDecl.value)
|
|
|
|
input = visitAssignmentRightHandSide(*_varDecl.value, declaredVariables.size());
|
|
|
|
else
|
|
|
|
input = Stack(_varDecl.variables.size(), LiteralSlot{0, _varDecl.debugData});
|
|
|
|
m_currentBlock->operations.emplace_back(CFG::Operation{
|
|
|
|
std::move(input),
|
|
|
|
declaredVariables | ranges::to<Stack>,
|
|
|
|
CFG::Assignment{_varDecl.debugData, declaredVariables}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
void ControlFlowGraphBuilder::operator()(Assignment const& _assignment)
|
|
|
|
{
|
|
|
|
auto assignedVariables = _assignment.variableNames | ranges::views::transform([&](Identifier const& _var) {
|
|
|
|
return VariableSlot{lookupVariable(_var.name), _var.debugData};
|
2023-08-23 02:24:09 +00:00
|
|
|
}) | ranges::to<std::vector<VariableSlot>>;
|
2021-06-11 17:17:41 +00:00
|
|
|
|
2021-10-13 15:52:08 +00:00
|
|
|
Stack input = visitAssignmentRightHandSide(*_assignment.value, assignedVariables.size());
|
|
|
|
yulAssert(m_currentBlock);
|
2021-06-11 17:17:41 +00:00
|
|
|
m_currentBlock->operations.emplace_back(CFG::Operation{
|
2021-10-13 15:52:08 +00:00
|
|
|
std::move(input),
|
2021-06-11 17:17:41 +00:00
|
|
|
// output
|
|
|
|
assignedVariables | ranges::to<Stack>,
|
|
|
|
// operation
|
|
|
|
CFG::Assignment{_assignment.debugData, assignedVariables}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
void ControlFlowGraphBuilder::operator()(ExpressionStatement const& _exprStmt)
|
|
|
|
{
|
|
|
|
std::visit(util::GenericVisitor{
|
|
|
|
[&](FunctionCall const& _call) {
|
2021-11-02 16:48:59 +00:00
|
|
|
Stack const& output = visitFunctionCall(_call);
|
|
|
|
yulAssert(output.empty(), "");
|
2021-06-11 17:17:41 +00:00
|
|
|
},
|
|
|
|
[&](auto const&) { yulAssert(false, ""); }
|
|
|
|
}, _exprStmt.expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraphBuilder::operator()(Block const& _block)
|
|
|
|
{
|
|
|
|
ScopedSaveAndRestore saveScope(m_scope, m_info.scopes.at(&_block).get());
|
2021-11-04 12:38:21 +00:00
|
|
|
for (auto const& statement: _block.statements)
|
2023-08-23 02:24:09 +00:00
|
|
|
if (auto const* function = std::get_if<FunctionDefinition>(&statement))
|
2021-11-04 12:38:21 +00:00
|
|
|
registerFunction(*function);
|
2021-06-11 17:17:41 +00:00
|
|
|
for (auto const& statement: _block.statements)
|
|
|
|
std::visit(*this, statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraphBuilder::operator()(If const& _if)
|
|
|
|
{
|
2021-09-09 14:49:36 +00:00
|
|
|
auto& ifBranch = m_graph.makeBlock(debugDataOf(_if.body));
|
|
|
|
auto& afterIf = m_graph.makeBlock(debugDataOf(*m_currentBlock));
|
2021-10-13 15:52:08 +00:00
|
|
|
StackSlot condition = std::visit(*this, *_if.condition);
|
|
|
|
makeConditionalJump(debugDataOf(_if), std::move(condition), ifBranch, afterIf);
|
2021-09-09 14:49:36 +00:00
|
|
|
m_currentBlock = &ifBranch;
|
2021-06-11 17:17:41 +00:00
|
|
|
(*this)(_if.body);
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_if.body), afterIf);
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraphBuilder::operator()(Switch const& _switch)
|
|
|
|
{
|
|
|
|
yulAssert(m_currentBlock, "");
|
2023-08-23 02:24:09 +00:00
|
|
|
std::shared_ptr<DebugData const> preSwitchDebugData = debugDataOf(_switch);
|
2021-09-09 14:49:36 +00:00
|
|
|
|
2021-06-11 17:17:41 +00:00
|
|
|
auto ghostVariableId = m_graph.ghostVariables.size();
|
2023-08-23 02:24:09 +00:00
|
|
|
YulString ghostVariableName("GHOST[" + std::to_string(ghostVariableId) + "]");
|
2021-06-11 17:17:41 +00:00
|
|
|
auto& ghostVar = m_graph.ghostVariables.emplace_back(Scope::Variable{""_yulstring, ghostVariableName});
|
|
|
|
|
|
|
|
// Artificially generate:
|
|
|
|
// let <ghostVariable> := <switchExpression>
|
|
|
|
VariableSlot ghostVarSlot{ghostVar, debugDataOf(*_switch.expression)};
|
2021-10-13 15:52:08 +00:00
|
|
|
StackSlot expression = std::visit(*this, *_switch.expression);
|
2021-06-11 17:17:41 +00:00
|
|
|
m_currentBlock->operations.emplace_back(CFG::Operation{
|
2021-10-13 15:52:08 +00:00
|
|
|
Stack{std::move(expression)},
|
2021-06-11 17:17:41 +00:00
|
|
|
Stack{ghostVarSlot},
|
|
|
|
CFG::Assignment{_switch.debugData, {ghostVarSlot}}
|
|
|
|
});
|
|
|
|
|
|
|
|
BuiltinFunction const* equalityBuiltin = m_dialect.equalityFunction({});
|
|
|
|
yulAssert(equalityBuiltin, "");
|
|
|
|
|
|
|
|
// Artificially generate:
|
|
|
|
// eq(<literal>, <ghostVariable>)
|
2021-09-09 14:49:36 +00:00
|
|
|
auto makeValueCompare = [&](Case const& _case) {
|
2021-06-11 17:17:41 +00:00
|
|
|
yul::FunctionCall const& ghostCall = m_graph.ghostCalls.emplace_back(yul::FunctionCall{
|
2021-09-09 14:49:36 +00:00
|
|
|
debugDataOf(_case),
|
2021-06-11 17:17:41 +00:00
|
|
|
yul::Identifier{{}, "eq"_yulstring},
|
2021-09-09 14:49:36 +00:00
|
|
|
{*_case.value, Identifier{{}, ghostVariableName}}
|
2021-06-11 17:17:41 +00:00
|
|
|
});
|
|
|
|
CFG::Operation& operation = m_currentBlock->operations.emplace_back(CFG::Operation{
|
2021-09-09 14:49:36 +00:00
|
|
|
Stack{ghostVarSlot, LiteralSlot{valueOfLiteral(*_case.value), debugDataOf(*_case.value)}},
|
2021-06-11 17:17:41 +00:00
|
|
|
Stack{TemporarySlot{ghostCall, 0}},
|
2021-09-09 14:49:36 +00:00
|
|
|
CFG::BuiltinCall{debugDataOf(_case), *equalityBuiltin, ghostCall, 2},
|
2021-06-11 17:17:41 +00:00
|
|
|
});
|
|
|
|
return operation.output.front();
|
|
|
|
};
|
2021-09-09 14:49:36 +00:00
|
|
|
CFG::BasicBlock& afterSwitch = m_graph.makeBlock(preSwitchDebugData);
|
2021-06-11 17:17:41 +00:00
|
|
|
yulAssert(!_switch.cases.empty(), "");
|
|
|
|
for (auto const& switchCase: _switch.cases | ranges::views::drop_last(1))
|
|
|
|
{
|
|
|
|
yulAssert(switchCase.value, "");
|
2021-09-09 14:49:36 +00:00
|
|
|
auto& caseBranch = m_graph.makeBlock(debugDataOf(switchCase.body));
|
|
|
|
auto& elseBranch = m_graph.makeBlock(debugDataOf(_switch));
|
|
|
|
makeConditionalJump(debugDataOf(switchCase), makeValueCompare(switchCase), caseBranch, elseBranch);
|
|
|
|
m_currentBlock = &caseBranch;
|
2021-06-11 17:17:41 +00:00
|
|
|
(*this)(switchCase.body);
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(switchCase.body), afterSwitch);
|
|
|
|
m_currentBlock = &elseBranch;
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
Case const& switchCase = _switch.cases.back();
|
|
|
|
if (switchCase.value)
|
|
|
|
{
|
2021-09-09 14:49:36 +00:00
|
|
|
CFG::BasicBlock& caseBranch = m_graph.makeBlock(debugDataOf(switchCase.body));
|
|
|
|
makeConditionalJump(debugDataOf(switchCase), makeValueCompare(switchCase), caseBranch, afterSwitch);
|
2021-06-11 17:17:41 +00:00
|
|
|
m_currentBlock = &caseBranch;
|
|
|
|
}
|
|
|
|
(*this)(switchCase.body);
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(switchCase.body), afterSwitch);
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraphBuilder::operator()(ForLoop const& _loop)
|
|
|
|
{
|
2023-08-23 02:24:09 +00:00
|
|
|
std::shared_ptr<DebugData const> preLoopDebugData = debugDataOf(_loop);
|
2021-06-11 17:17:41 +00:00
|
|
|
ScopedSaveAndRestore scopeRestore(m_scope, m_info.scopes.at(&_loop.pre).get());
|
|
|
|
(*this)(_loop.pre);
|
|
|
|
|
|
|
|
std::optional<bool> constantCondition;
|
2023-08-23 02:24:09 +00:00
|
|
|
if (auto const* literalCondition = std::get_if<yul::Literal>(_loop.condition.get()))
|
2021-06-11 17:17:41 +00:00
|
|
|
constantCondition = valueOfLiteral(*literalCondition) != 0;
|
|
|
|
|
2021-09-09 14:49:36 +00:00
|
|
|
CFG::BasicBlock& loopCondition = m_graph.makeBlock(debugDataOf(*_loop.condition));
|
|
|
|
CFG::BasicBlock& loopBody = m_graph.makeBlock(debugDataOf(_loop.body));
|
|
|
|
CFG::BasicBlock& post = m_graph.makeBlock(debugDataOf(_loop.post));
|
|
|
|
CFG::BasicBlock& afterLoop = m_graph.makeBlock(preLoopDebugData);
|
2021-06-11 17:17:41 +00:00
|
|
|
|
|
|
|
ScopedSaveAndRestore scopedSaveAndRestore(m_forLoopInfo, ForLoopInfo{afterLoop, post});
|
|
|
|
|
|
|
|
if (constantCondition.has_value())
|
|
|
|
{
|
|
|
|
if (*constantCondition)
|
|
|
|
{
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_loop.pre), loopBody);
|
2021-06-11 17:17:41 +00:00
|
|
|
(*this)(_loop.body);
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_loop.body), post);
|
2021-06-11 17:17:41 +00:00
|
|
|
(*this)(_loop.post);
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_loop.post), loopBody, true);
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
else
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_loop.pre), afterLoop);
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_loop.pre), loopCondition);
|
2021-10-13 15:52:08 +00:00
|
|
|
StackSlot condition = std::visit(*this, *_loop.condition);
|
|
|
|
makeConditionalJump(debugDataOf(*_loop.condition), std::move(condition), loopBody, afterLoop);
|
2021-06-11 17:17:41 +00:00
|
|
|
m_currentBlock = &loopBody;
|
|
|
|
(*this)(_loop.body);
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_loop.body), post);
|
2021-06-11 17:17:41 +00:00
|
|
|
(*this)(_loop.post);
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_loop.post), loopCondition, true);
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_currentBlock = &afterLoop;
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:49:36 +00:00
|
|
|
void ControlFlowGraphBuilder::operator()(Break const& _break)
|
2021-06-11 17:17:41 +00:00
|
|
|
{
|
|
|
|
yulAssert(m_forLoopInfo.has_value(), "");
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_break), m_forLoopInfo->afterLoop);
|
|
|
|
m_currentBlock = &m_graph.makeBlock(debugDataOf(*m_currentBlock));
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 14:49:36 +00:00
|
|
|
void ControlFlowGraphBuilder::operator()(Continue const& _continue)
|
2021-06-11 17:17:41 +00:00
|
|
|
{
|
|
|
|
yulAssert(m_forLoopInfo.has_value(), "");
|
2021-09-09 14:49:36 +00:00
|
|
|
jump(debugDataOf(_continue), m_forLoopInfo->post);
|
|
|
|
m_currentBlock = &m_graph.makeBlock(debugDataOf(*m_currentBlock));
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 14:49:36 +00:00
|
|
|
// '_leave' and '__leave' are reserved in VisualStudio
|
|
|
|
void ControlFlowGraphBuilder::operator()(Leave const& leave_)
|
2021-06-11 17:17:41 +00:00
|
|
|
{
|
2021-09-09 14:49:36 +00:00
|
|
|
yulAssert(m_currentFunction.has_value(), "");
|
|
|
|
m_currentBlock->exit = CFG::BasicBlock::FunctionReturn{debugDataOf(leave_), *m_currentFunction};
|
2022-01-14 12:37:40 +00:00
|
|
|
(*m_currentFunction)->exits.emplace_back(m_currentBlock);
|
2021-09-09 14:49:36 +00:00
|
|
|
m_currentBlock = &m_graph.makeBlock(debugDataOf(*m_currentBlock));
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControlFlowGraphBuilder::operator()(FunctionDefinition const& _function)
|
|
|
|
{
|
|
|
|
yulAssert(m_scope, "");
|
|
|
|
yulAssert(m_scope->identifiers.count(_function.name), "");
|
|
|
|
Scope::Function& function = std::get<Scope::Function>(m_scope->identifiers.at(_function.name));
|
|
|
|
m_graph.functions.emplace_back(&function);
|
|
|
|
|
2021-11-04 12:38:21 +00:00
|
|
|
CFG::FunctionInfo& functionInfo = m_graph.functionInfo.at(&function);
|
|
|
|
|
2021-10-13 15:52:08 +00:00
|
|
|
ControlFlowGraphBuilder builder{m_graph, m_info, m_functionSideEffects, m_dialect};
|
2021-11-04 12:38:21 +00:00
|
|
|
builder.m_currentFunction = &functionInfo;
|
|
|
|
builder.m_currentBlock = functionInfo.entry;
|
|
|
|
builder(_function.body);
|
2022-01-14 12:37:40 +00:00
|
|
|
functionInfo.exits.emplace_back(builder.m_currentBlock);
|
2021-11-04 12:38:21 +00:00
|
|
|
builder.m_currentBlock->exit = CFG::BasicBlock::FunctionReturn{debugDataOf(_function), &functionInfo};
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:52:08 +00:00
|
|
|
void ControlFlowGraphBuilder::registerFunction(FunctionDefinition const& _functionDefinition)
|
2021-11-04 12:38:21 +00:00
|
|
|
{
|
|
|
|
yulAssert(m_scope, "");
|
2021-10-13 15:52:08 +00:00
|
|
|
yulAssert(m_scope->identifiers.count(_functionDefinition.name), "");
|
|
|
|
Scope::Function& function = std::get<Scope::Function>(m_scope->identifiers.at(_functionDefinition.name));
|
2021-11-04 12:38:21 +00:00
|
|
|
|
2021-10-13 15:52:08 +00:00
|
|
|
yulAssert(m_info.scopes.at(&_functionDefinition.body), "");
|
|
|
|
Scope* virtualFunctionScope = m_info.scopes.at(m_info.virtualBlocks.at(&_functionDefinition).get()).get();
|
2021-06-11 17:17:41 +00:00
|
|
|
yulAssert(virtualFunctionScope, "");
|
|
|
|
|
2021-11-04 12:38:21 +00:00
|
|
|
bool inserted = m_graph.functionInfo.emplace(std::make_pair(&function, CFG::FunctionInfo{
|
2021-10-13 15:52:08 +00:00
|
|
|
_functionDefinition.debugData,
|
2021-06-11 17:17:41 +00:00
|
|
|
function,
|
2021-10-13 15:52:08 +00:00
|
|
|
_functionDefinition,
|
|
|
|
&m_graph.makeBlock(debugDataOf(_functionDefinition.body)),
|
|
|
|
_functionDefinition.parameters | ranges::views::transform([&](auto const& _param) {
|
2021-06-11 17:17:41 +00:00
|
|
|
return VariableSlot{
|
|
|
|
std::get<Scope::Variable>(virtualFunctionScope->identifiers.at(_param.name)),
|
|
|
|
_param.debugData
|
|
|
|
};
|
2023-08-23 02:24:09 +00:00
|
|
|
}) | ranges::to<std::vector>,
|
2021-10-13 15:52:08 +00:00
|
|
|
_functionDefinition.returnVariables | ranges::views::transform([&](auto const& _retVar) {
|
2021-06-11 17:17:41 +00:00
|
|
|
return VariableSlot{
|
|
|
|
std::get<Scope::Variable>(virtualFunctionScope->identifiers.at(_retVar.name)),
|
|
|
|
_retVar.debugData
|
|
|
|
};
|
2023-08-23 02:24:09 +00:00
|
|
|
}) | ranges::to<std::vector>,
|
2021-10-13 15:52:08 +00:00
|
|
|
{},
|
|
|
|
m_functionSideEffects.at(&_functionDefinition).canContinue
|
2021-11-04 12:38:21 +00:00
|
|
|
})).second;
|
|
|
|
yulAssert(inserted);
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 16:48:59 +00:00
|
|
|
Stack const& ControlFlowGraphBuilder::visitFunctionCall(FunctionCall const& _call)
|
2021-06-11 17:17:41 +00:00
|
|
|
{
|
|
|
|
yulAssert(m_scope, "");
|
|
|
|
yulAssert(m_currentBlock, "");
|
|
|
|
|
2021-10-13 15:52:08 +00:00
|
|
|
Stack const* output = nullptr;
|
|
|
|
bool canContinue = true;
|
2021-06-11 17:17:41 +00:00
|
|
|
if (BuiltinFunction const* builtin = m_dialect.builtin(_call.functionName.name))
|
|
|
|
{
|
|
|
|
Stack inputs;
|
|
|
|
for (auto&& [idx, arg]: _call.arguments | ranges::views::enumerate | ranges::views::reverse)
|
|
|
|
if (!builtin->literalArgument(idx).has_value())
|
|
|
|
inputs.emplace_back(std::visit(*this, arg));
|
|
|
|
CFG::BuiltinCall builtinCall{_call.debugData, *builtin, _call, inputs.size()};
|
2021-10-13 15:52:08 +00:00
|
|
|
output = &m_currentBlock->operations.emplace_back(CFG::Operation{
|
2021-06-11 17:17:41 +00:00
|
|
|
// input
|
|
|
|
std::move(inputs),
|
|
|
|
// output
|
|
|
|
ranges::views::iota(0u, builtin->returns.size()) | ranges::views::transform([&](size_t _i) {
|
|
|
|
return TemporarySlot{_call, _i};
|
|
|
|
}) | ranges::to<Stack>,
|
|
|
|
// operation
|
2022-08-23 17:28:45 +00:00
|
|
|
std::move(builtinCall)
|
2021-11-02 16:48:59 +00:00
|
|
|
}).output;
|
2021-10-13 15:52:08 +00:00
|
|
|
canContinue = builtin->controlFlowSideEffects.canContinue;
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Scope::Function const& function = lookupFunction(_call.functionName.name);
|
2021-10-13 15:52:08 +00:00
|
|
|
canContinue = m_graph.functionInfo.at(&function).canContinue;
|
|
|
|
Stack inputs;
|
|
|
|
if (canContinue)
|
|
|
|
inputs.emplace_back(FunctionCallReturnLabelSlot{_call});
|
2021-06-11 17:17:41 +00:00
|
|
|
for (auto const& arg: _call.arguments | ranges::views::reverse)
|
|
|
|
inputs.emplace_back(std::visit(*this, arg));
|
2021-10-13 15:52:08 +00:00
|
|
|
output = &m_currentBlock->operations.emplace_back(CFG::Operation{
|
2021-06-11 17:17:41 +00:00
|
|
|
// input
|
|
|
|
std::move(inputs),
|
|
|
|
// output
|
|
|
|
ranges::views::iota(0u, function.returns.size()) | ranges::views::transform([&](size_t _i) {
|
|
|
|
return TemporarySlot{_call, _i};
|
|
|
|
}) | ranges::to<Stack>,
|
|
|
|
// operation
|
2021-10-13 15:52:08 +00:00
|
|
|
CFG::FunctionCall{_call.debugData, function, _call, /* recursive */ false, canContinue}
|
2021-11-02 16:48:59 +00:00
|
|
|
}).output;
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
2021-10-13 15:52:08 +00:00
|
|
|
if (!canContinue)
|
|
|
|
{
|
|
|
|
m_currentBlock->exit = CFG::BasicBlock::Terminated{};
|
|
|
|
m_currentBlock = &m_graph.makeBlock(debugDataOf(*m_currentBlock));
|
|
|
|
}
|
|
|
|
return *output;
|
2021-06-11 17:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Stack ControlFlowGraphBuilder::visitAssignmentRightHandSide(Expression const& _expression, size_t _expectedSlotCount)
|
|
|
|
{
|
|
|
|
return std::visit(util::GenericVisitor{
|
|
|
|
[&](FunctionCall const& _call) -> Stack {
|
2021-11-02 16:48:59 +00:00
|
|
|
Stack const& output = visitFunctionCall(_call);
|
|
|
|
yulAssert(_expectedSlotCount == output.size(), "");
|
|
|
|
return output;
|
2021-06-11 17:17:41 +00:00
|
|
|
},
|
|
|
|
[&](auto const& _identifierOrLiteral) -> Stack {
|
|
|
|
yulAssert(_expectedSlotCount == 1, "");
|
|
|
|
return {(*this)(_identifierOrLiteral)};
|
|
|
|
}
|
|
|
|
}, _expression);
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::Function const& ControlFlowGraphBuilder::lookupFunction(YulString _name) const
|
|
|
|
{
|
|
|
|
Scope::Function const* function = nullptr;
|
|
|
|
yulAssert(m_scope->lookup(_name, util::GenericVisitor{
|
|
|
|
[](Scope::Variable&) { yulAssert(false, "Expected function name."); },
|
|
|
|
[&](Scope::Function& _function) { function = &_function; }
|
|
|
|
}), "Function name not found.");
|
|
|
|
yulAssert(function, "");
|
|
|
|
return *function;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scope::Variable const& ControlFlowGraphBuilder::lookupVariable(YulString _name) const
|
|
|
|
{
|
|
|
|
yulAssert(m_scope, "");
|
|
|
|
Scope::Variable const* var = nullptr;
|
|
|
|
if (m_scope->lookup(_name, util::GenericVisitor{
|
|
|
|
[&](Scope::Variable& _var) { var = &_var; },
|
|
|
|
[](Scope::Function&)
|
|
|
|
{
|
|
|
|
yulAssert(false, "Function not removed during desugaring.");
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
{
|
|
|
|
yulAssert(var, "");
|
|
|
|
return *var;
|
|
|
|
};
|
|
|
|
yulAssert(false, "External identifier access unimplemented.");
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:49:36 +00:00
|
|
|
void ControlFlowGraphBuilder::makeConditionalJump(
|
2023-08-23 02:24:09 +00:00
|
|
|
std::shared_ptr<DebugData const> _debugData,
|
2021-09-09 14:49:36 +00:00
|
|
|
StackSlot _condition,
|
|
|
|
CFG::BasicBlock& _nonZero,
|
|
|
|
CFG::BasicBlock& _zero
|
|
|
|
)
|
2021-06-11 17:17:41 +00:00
|
|
|
{
|
|
|
|
yulAssert(m_currentBlock, "");
|
|
|
|
m_currentBlock->exit = CFG::BasicBlock::ConditionalJump{
|
2022-08-23 17:28:45 +00:00
|
|
|
std::move(_debugData),
|
|
|
|
std::move(_condition),
|
2021-06-11 17:17:41 +00:00
|
|
|
&_nonZero,
|
|
|
|
&_zero
|
|
|
|
};
|
|
|
|
_nonZero.entries.emplace_back(m_currentBlock);
|
|
|
|
_zero.entries.emplace_back(m_currentBlock);
|
|
|
|
m_currentBlock = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:49:36 +00:00
|
|
|
void ControlFlowGraphBuilder::jump(
|
2023-08-23 02:24:09 +00:00
|
|
|
std::shared_ptr<DebugData const> _debugData,
|
2021-09-09 14:49:36 +00:00
|
|
|
CFG::BasicBlock& _target,
|
|
|
|
bool backwards
|
|
|
|
)
|
2021-06-11 17:17:41 +00:00
|
|
|
{
|
|
|
|
yulAssert(m_currentBlock, "");
|
2022-08-23 17:28:45 +00:00
|
|
|
m_currentBlock->exit = CFG::BasicBlock::Jump{std::move(_debugData), &_target, backwards};
|
2021-06-11 17:17:41 +00:00
|
|
|
_target.entries.emplace_back(m_currentBlock);
|
|
|
|
m_currentBlock = &_target;
|
|
|
|
}
|