Skip to content

Commit

Permalink
[yul] Functions: Remove dependency on AST ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlt authored and chriseth committed Jun 22, 2021
1 parent 9cf6021 commit 3e09ab6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions libsolidity/codegen/ir/IRGenerationContext.cpp
Expand Up @@ -177,3 +177,8 @@ ABIFunctions IRGenerationContext::abiFunctions()
{
return ABIFunctions(m_evmVersion, m_revertStrings, m_functions);
}

uint64_t IRGenerationContext::internalFunctionID(FunctionDefinition const& _function)
{
return m_functionIDs.try_emplace(_function.id(), m_functionIDs.size() + 1).second;
}
6 changes: 6 additions & 0 deletions libsolidity/codegen/ir/IRGenerationContext.h
Expand Up @@ -152,6 +152,10 @@ class IRGenerationContext
bool inlineAssemblySeen() const { return m_inlineAssemblySeen; }
void setInlineAssemblySeen() { m_inlineAssemblySeen = true; }

/// @returns the runtime ID to be used for the function in the dispatch routine
/// and for internal function pointers.
uint64_t internalFunctionID(FunctionDefinition const& _function);

std::map<std::string, unsigned> const& sourceIndices() const { return m_sourceIndices; }

private:
Expand Down Expand Up @@ -191,6 +195,8 @@ class IRGenerationContext
/// the code contains a call via a pointer even though a specific function is never assigned to it.
/// It will fail at runtime but the code must still compile.
InternalDispatchMap m_internalDispatchMap;
/// Map used by @a internalFunctionID.
std::map<int64_t, uint64_t> m_functionIDs;

std::set<ContractDefinition const*, ASTNode::CompareByID> m_subObjects;
};
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/codegen/ir/IRGenerator.cpp
Expand Up @@ -299,7 +299,7 @@ InternalDispatchMap IRGenerator::generateInternalDispatchFunctions(ContractDefin
solAssert(m_context.functionCollector().contains(IRNames::function(*function)), "");

cases.emplace_back(map<string, string>{
{"funID", to_string(function->id())},
{"funID", to_string(m_context.internalFunctionID(*function))},
{"name", IRNames::function(*function)}
});
}
Expand Down
10 changes: 5 additions & 5 deletions libsolidity/codegen/ir/IRGeneratorForStatements.cpp
Expand Up @@ -1577,7 +1577,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
if (memberFunctionType->kind() == FunctionType::Kind::Internal)
{
auto const& functionDefinition = dynamic_cast<FunctionDefinition const&>(memberFunctionType->declaration());
define(IRVariable(_memberAccess).part("functionIdentifier")) << to_string(functionDefinition.id()) << "\n";
define(IRVariable(_memberAccess).part("functionIdentifier")) << to_string(m_context.internalFunctionID(functionDefinition.id())) << "\n";
if (!_memberAccess.annotation().calledDirectly)
m_context.addToInternalDispatch(functionDefinition);
}
Expand Down Expand Up @@ -1918,7 +1918,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
*_memberAccess.annotation().referencedDeclaration
).resolveVirtual(m_context.mostDerivedContract(), super);

define(_memberAccess) << to_string(resolvedFunctionDef.id()) << "\n";
define(_memberAccess) << to_string(m_context.internalFunctionID(resolvedFunctionDef.id())) << "\n";
solAssert(resolvedFunctionDef.functionType(true), "");
solAssert(resolvedFunctionDef.functionType(true)->kind() == FunctionType::Kind::Internal, "");
if (!_memberAccess.annotation().calledDirectly)
Expand All @@ -1935,7 +1935,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
case FunctionType::Kind::Internal:
if (auto const* function = dynamic_cast<FunctionDefinition const*>(_memberAccess.annotation().referencedDeclaration))
{
define(_memberAccess) << to_string(function->id()) << "\n";
define(_memberAccess) << to_string(m_context.internalFunctionID(function->id())) << "\n";
if (!_memberAccess.annotation().calledDirectly)
m_context.addToInternalDispatch(*function);
}
Expand Down Expand Up @@ -2021,7 +2021,7 @@ void IRGeneratorForStatements::endVisit(MemberAccess const& _memberAccess)
solAssert(funType->kind() == FunctionType::Kind::Internal, "");
solAssert(*_memberAccess.annotation().requiredLookup == VirtualLookup::Static, "");

define(_memberAccess) << to_string(function->id()) << "\n";
define(_memberAccess) << to_string(m_context.internalFunctionID(function->id())) << "\n";

if (!_memberAccess.annotation().calledDirectly)
m_context.addToInternalDispatch(*function);
Expand Down Expand Up @@ -2268,7 +2268,7 @@ void IRGeneratorForStatements::endVisit(Identifier const& _identifier)
{
solAssert(*_identifier.annotation().requiredLookup == VirtualLookup::Virtual, "");
FunctionDefinition const& resolvedFunctionDef = functionDef->resolveVirtual(m_context.mostDerivedContract());
define(_identifier) << to_string(resolvedFunctionDef.id()) << "\n";
define(_identifier) << to_string(m_context.internalFunctionID(resolvedFunctionDef.id())) << "\n";

solAssert(resolvedFunctionDef.functionType(true), "");
solAssert(resolvedFunctionDef.functionType(true)->kind() == FunctionType::Kind::Internal, "");
Expand Down

0 comments on commit 3e09ab6

Please sign in to comment.