Skip to content

Commit 106921c

Browse files
committed
Extract external function part into its own function.
1 parent daad9a4 commit 106921c

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

libsolidity/codegen/ir/Common.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ YulArity YulArity::fromType(FunctionType const& _functionType)
4141
};
4242
}
4343

44+
string IRNames::externalFunctionABIWrapper(Declaration const& _functionOrVarDecl)
45+
{
46+
if (auto const* function = dynamic_cast<FunctionDefinition const*>(&_functionOrVarDecl))
47+
solAssert(!function->isConstructor());
48+
49+
return "external_fun_" + _functionOrVarDecl.name() + "_" + to_string(_functionOrVarDecl.id());
50+
}
51+
4452
string IRNames::function(FunctionDefinition const& _function)
4553
{
4654
if (_function.isConstructor())

libsolidity/codegen/ir/Common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct YulArity
4949

5050
struct IRNames
5151
{
52+
static std::string externalFunctionABIWrapper(Declaration const& _functionOrVardecl);
5253
static std::string function(FunctionDefinition const& _function);
5354
static std::string function(VariableDeclaration const& _varDecl);
5455
static std::string modifierInvocation(ModifierInvocation const& _modifierInvocation);

libsolidity/codegen/ir/IRGenerator.cpp

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,44 @@ string IRGenerator::generateGetter(VariableDeclaration const& _varDecl)
735735
});
736736
}
737737

738+
string IRGenerator::generateExternalFunction(ContractDefinition const& _contract, FunctionType const& _functionType)
739+
{
740+
string functionName = IRNames::externalFunctionABIWrapper(_functionType.declaration());
741+
return m_context.functionCollector().createFunction(functionName, [&](vector<string>&, vector<string>&) -> string {
742+
Whiskers t(R"X(
743+
<callValueCheck>
744+
<?+params>let <params> := </+params> <abiDecode>(4, calldatasize())
745+
<?+retParams>let <retParams> := </+retParams> <function>(<params>)
746+
let memPos := <allocateUnbounded>()
747+
let memEnd := <abiEncode>(memPos <?+retParams>,</+retParams> <retParams>)
748+
return(memPos, sub(memEnd, memPos))
749+
)X");
750+
t("callValueCheck", (_functionType.isPayable() || _contract.isLibrary()) ? "" : callValueCheck());
751+
752+
unsigned paramVars = make_shared<TupleType>(_functionType.parameterTypes())->sizeOnStack();
753+
unsigned retVars = make_shared<TupleType>(_functionType.returnParameterTypes())->sizeOnStack();
754+
755+
ABIFunctions abiFunctions(m_evmVersion, m_context.revertStrings(), m_context.functionCollector());
756+
t("abiDecode", abiFunctions.tupleDecoder(_functionType.parameterTypes()));
757+
t("params", suffixedVariableNameList("param_", 0, paramVars));
758+
t("retParams", suffixedVariableNameList("ret_", 0, retVars));
759+
760+
if (FunctionDefinition const* funDef = dynamic_cast<FunctionDefinition const*>(&_functionType.declaration()))
761+
{
762+
solAssert(!funDef->isConstructor());
763+
t("function", m_context.enqueueFunctionForCodeGeneration(*funDef));
764+
}
765+
else if (VariableDeclaration const* varDecl = dynamic_cast<VariableDeclaration const*>(&_functionType.declaration()))
766+
t("function", generateGetter(*varDecl));
767+
else
768+
solAssert(false, "Unexpected declaration for function!");
769+
770+
t("allocateUnbounded", m_utils.allocateUnboundedFunction());
771+
t("abiEncode", abiFunctions.tupleEncoder(_functionType.returnParameterTypes(), _functionType.returnParameterTypes(), _contract.isLibrary()));
772+
return t.render();
773+
});
774+
}
775+
738776
string IRGenerator::generateInitialAssignment(VariableDeclaration const& _varDecl)
739777
{
740778
IRGeneratorForStatements generator(m_context, m_utils);
@@ -976,12 +1014,7 @@ string IRGenerator::dispatchRoutine(ContractDefinition const& _contract)
9761014
{
9771015
// <functionName>
9781016
<delegatecallCheck>
979-
<callValueCheck>
980-
<?+params>let <params> := </+params> <abiDecode>(4, calldatasize())
981-
<?+retParams>let <retParams> := </+retParams> <function>(<params>)
982-
let memPos := <allocateUnbounded>()
983-
let memEnd := <abiEncode>(memPos <?+retParams>,</+retParams> <retParams>)
984-
return(memPos, sub(memEnd, memPos))
1017+
<externalFunction>()
9851018
}
9861019
</cases>
9871020
default {}
@@ -1011,25 +1044,8 @@ string IRGenerator::dispatchRoutine(ContractDefinition const& _contract)
10111044
"() }";
10121045
}
10131046
templ["delegatecallCheck"] = delegatecallCheck;
1014-
templ["callValueCheck"] = (type->isPayable() || _contract.isLibrary()) ? "" : callValueCheck();
1015-
1016-
unsigned paramVars = make_shared<TupleType>(type->parameterTypes())->sizeOnStack();
1017-
unsigned retVars = make_shared<TupleType>(type->returnParameterTypes())->sizeOnStack();
1018-
1019-
ABIFunctions abiFunctions(m_evmVersion, m_context.revertStrings(), m_context.functionCollector());
1020-
templ["abiDecode"] = abiFunctions.tupleDecoder(type->parameterTypes());
1021-
templ["params"] = suffixedVariableNameList("param_", 0, paramVars);
1022-
templ["retParams"] = suffixedVariableNameList("ret_", 0, retVars);
1023-
1024-
if (FunctionDefinition const* funDef = dynamic_cast<FunctionDefinition const*>(&type->declaration()))
1025-
templ["function"] = m_context.enqueueFunctionForCodeGeneration(*funDef);
1026-
else if (VariableDeclaration const* varDecl = dynamic_cast<VariableDeclaration const*>(&type->declaration()))
1027-
templ["function"] = generateGetter(*varDecl);
1028-
else
1029-
solAssert(false, "Unexpected declaration for function!");
10301047

1031-
templ["allocateUnbounded"] = m_utils.allocateUnboundedFunction();
1032-
templ["abiEncode"] = abiFunctions.tupleEncoder(type->returnParameterTypes(), type->returnParameterTypes(), _contract.isLibrary());
1048+
templ["externalFunction"] = generateExternalFunction(_contract, *type);
10331049
}
10341050
t("cases", functions);
10351051
FunctionDefinition const* etherReceiver = _contract.receiveFunction();

libsolidity/codegen/ir/IRGenerator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class IRGenerator
102102
/// Generates a getter for the given declaration and returns its name
103103
std::string generateGetter(VariableDeclaration const& _varDecl);
104104

105+
/// Generates the external part (ABI decoding and encoding) of a function or getter.
106+
std::string generateExternalFunction(ContractDefinition const& _contract, FunctionType const& _functionType);
107+
105108
/// Generates code that assigns the initial value of the respective type.
106109
std::string generateInitialAssignment(VariableDeclaration const& _varDecl);
107110

0 commit comments

Comments
 (0)