Skip to content

Commit 929ed09

Browse files
committed
Register functions earlier.
1 parent 99aa18c commit 929ed09

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

libyul/backends/evm/ControlFlowGraphBuilder.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ void ControlFlowGraphBuilder::operator()(ExpressionStatement const& _exprStmt)
239239
void ControlFlowGraphBuilder::operator()(Block const& _block)
240240
{
241241
ScopedSaveAndRestore saveScope(m_scope, m_info.scopes.at(&_block).get());
242+
for (auto const& statement: _block.statements)
243+
if (auto const* function = get_if<FunctionDefinition>(&statement))
244+
registerFunction(*function);
242245
for (auto const& statement: _block.statements)
243246
std::visit(*this, statement);
244247
}
@@ -386,11 +389,26 @@ void ControlFlowGraphBuilder::operator()(FunctionDefinition const& _function)
386389
Scope::Function& function = std::get<Scope::Function>(m_scope->identifiers.at(_function.name));
387390
m_graph.functions.emplace_back(&function);
388391

392+
CFG::FunctionInfo& functionInfo = m_graph.functionInfo.at(&function);
393+
394+
ControlFlowGraphBuilder builder{m_graph, m_info, m_dialect};
395+
builder.m_currentFunction = &functionInfo;
396+
builder.m_currentBlock = functionInfo.entry;
397+
builder(_function.body);
398+
builder.m_currentBlock->exit = CFG::BasicBlock::FunctionReturn{debugDataOf(_function), &functionInfo};
399+
}
400+
401+
void ControlFlowGraphBuilder::registerFunction(FunctionDefinition const& _function)
402+
{
403+
yulAssert(m_scope, "");
404+
yulAssert(m_scope->identifiers.count(_function.name), "");
405+
Scope::Function& function = std::get<Scope::Function>(m_scope->identifiers.at(_function.name));
406+
389407
yulAssert(m_info.scopes.at(&_function.body), "");
390408
Scope* virtualFunctionScope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get();
391409
yulAssert(virtualFunctionScope, "");
392410

393-
auto&& [it, inserted] = m_graph.functionInfo.emplace(std::make_pair(&function, CFG::FunctionInfo{
411+
bool inserted = m_graph.functionInfo.emplace(std::make_pair(&function, CFG::FunctionInfo{
394412
_function.debugData,
395413
function,
396414
&m_graph.makeBlock(debugDataOf(_function.body)),
@@ -406,18 +424,10 @@ void ControlFlowGraphBuilder::operator()(FunctionDefinition const& _function)
406424
_retVar.debugData
407425
};
408426
}) | ranges::to<vector>
409-
}));
410-
yulAssert(inserted, "");
411-
CFG::FunctionInfo& functionInfo = it->second;
412-
413-
ControlFlowGraphBuilder builder{m_graph, m_info, m_dialect};
414-
builder.m_currentFunction = &functionInfo;
415-
builder.m_currentBlock = functionInfo.entry;
416-
builder(_function.body);
417-
builder.m_currentBlock->exit = CFG::BasicBlock::FunctionReturn{debugDataOf(_function), &functionInfo};
427+
})).second;
428+
yulAssert(inserted);
418429
}
419430

420-
421431
Stack const& ControlFlowGraphBuilder::visitFunctionCall(FunctionCall const& _call)
422432
{
423433
yulAssert(m_scope, "");

libyul/backends/evm/ControlFlowGraphBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ControlFlowGraphBuilder
5757
AsmAnalysisInfo const& _analysisInfo,
5858
Dialect const& _dialect
5959
);
60+
void registerFunction(FunctionDefinition const& _function);
6061
Stack const& visitFunctionCall(FunctionCall const&);
6162
Stack visitAssignmentRightHandSide(Expression const& _expression, size_t _expectedSlotCount);
6263

0 commit comments

Comments
 (0)