Merge pull request #2478 from ethereum/fallback-dispatcher

Optimise the fallback dispatcher by removing a useless jump
This commit is contained in:
chriseth 2017-07-28 15:31:39 +02:00 committed by GitHub
commit 7e40def689
7 changed files with 15 additions and 14 deletions

View File

@ -2,6 +2,7 @@
Features: Features:
* C API (``jsonCompiler``): Export the ``license`` method. * C API (``jsonCompiler``): Export the ``license`` method.
* Code Generator: Optimise the fallback function, by removing a useless jump.
* Inline Assembly: Show useful error message if trying to access calldata variables. * Inline Assembly: Show useful error message if trying to access calldata variables.
* Inline Assembly: Support variable declaration without initial value (defaults to 0). * Inline Assembly: Support variable declaration without initial value (defaults to 0).
* Metadata: Only include files which were used to compile the given contract. * Metadata: Only include files which were used to compile the given contract.

View File

@ -93,7 +93,7 @@ bool TypeChecker::visit(ContractDefinition const& _contract)
FunctionDefinition const* fallbackFunction = nullptr; FunctionDefinition const* fallbackFunction = nullptr;
for (FunctionDefinition const* function: _contract.definedFunctions()) for (FunctionDefinition const* function: _contract.definedFunctions())
{ {
if (function->name().empty()) if (function->isFallback())
{ {
if (fallbackFunction) if (fallbackFunction)
{ {
@ -482,7 +482,7 @@ bool TypeChecker::visit(FunctionDefinition const& _function)
{ {
if (isLibraryFunction) if (isLibraryFunction)
m_errorReporter.typeError(_function.location(), "Library functions cannot be payable."); m_errorReporter.typeError(_function.location(), "Library functions cannot be payable.");
if (!_function.isConstructor() && !_function.name().empty() && !_function.isPartOfExternalInterface()) if (!_function.isConstructor() && !_function.isFallback() && !_function.isPartOfExternalInterface())
m_errorReporter.typeError(_function.location(), "Internal functions cannot be payable."); m_errorReporter.typeError(_function.location(), "Internal functions cannot be payable.");
if (_function.isDeclaredConst()) if (_function.isDeclaredConst())
m_errorReporter.typeError(_function.location(), "Functions cannot be constant and payable at the same time."); m_errorReporter.typeError(_function.location(), "Functions cannot be constant and payable at the same time.");

View File

@ -162,7 +162,7 @@ FunctionDefinition const* ContractDefinition::fallbackFunction() const
{ {
for (ContractDefinition const* contract: annotation().linearizedBaseContracts) for (ContractDefinition const* contract: annotation().linearizedBaseContracts)
for (FunctionDefinition const* f: contract->definedFunctions()) for (FunctionDefinition const* f: contract->definedFunctions())
if (f->name().empty()) if (f->isFallback())
return f; return f;
return nullptr; return nullptr;
} }

View File

@ -589,6 +589,7 @@ public:
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
bool isConstructor() const { return m_isConstructor; } bool isConstructor() const { return m_isConstructor; }
bool isFallback() const { return name().empty(); }
bool isDeclaredConst() const { return m_isDeclaredConst; } bool isDeclaredConst() const { return m_isDeclaredConst; }
bool isPayable() const { return m_isPayable; } bool isPayable() const { return m_isPayable; }
std::vector<ASTPointer<ModifierInvocation>> const& modifiers() const { return m_functionModifiers; } std::vector<ASTPointer<ModifierInvocation>> const& modifiers() const { return m_functionModifiers; }
@ -596,9 +597,9 @@ public:
Block const& body() const { solAssert(m_body, ""); return *m_body; } Block const& body() const { solAssert(m_body, ""); return *m_body; }
virtual bool isVisibleInContract() const override virtual bool isVisibleInContract() const override
{ {
return Declaration::isVisibleInContract() && !isConstructor() && !name().empty(); return Declaration::isVisibleInContract() && !isConstructor() && !isFallback();
} }
virtual bool isPartOfExternalInterface() const override { return isPublic() && !m_isConstructor && !name().empty(); } virtual bool isPartOfExternalInterface() const override { return isPublic() && !isConstructor() && !isFallback(); }
/// @returns the external signature of the function /// @returns the external signature of the function
/// That consists of the name of the function followed by the types of the /// That consists of the name of the function followed by the types of the

View File

@ -2524,6 +2524,7 @@ bool FunctionType::isBareCall() const
string FunctionType::externalSignature() const string FunctionType::externalSignature() const
{ {
solAssert(m_declaration != nullptr, "External signature of function needs declaration"); solAssert(m_declaration != nullptr, "External signature of function needs declaration");
solAssert(!m_declaration->name().empty(), "Fallback function has no signature.");
bool _inLibrary = dynamic_cast<ContractDefinition const&>(*m_declaration->scope()).isLibrary(); bool _inLibrary = dynamic_cast<ContractDefinition const&>(*m_declaration->scope()).isLibrary();

View File

@ -267,18 +267,13 @@ void ContractCompiler::appendFunctionSelector(ContractDefinition const& _contrac
m_context << notFound; m_context << notFound;
if (fallback) if (fallback)
{ {
m_context.setStackOffset(0);
if (!fallback->isPayable()) if (!fallback->isPayable())
appendCallValueCheck(); appendCallValueCheck();
// Return tag is used to jump out of the function. solAssert(fallback->isFallback(), "");
eth::AssemblyItem returnTag = m_context.pushNewTag();
fallback->accept(*this);
m_context << returnTag;
solAssert(FunctionType(*fallback).parameterTypes().empty(), ""); solAssert(FunctionType(*fallback).parameterTypes().empty(), "");
solAssert(FunctionType(*fallback).returnParameterTypes().empty(), ""); solAssert(FunctionType(*fallback).returnParameterTypes().empty(), "");
// Return tag gets consumed. fallback->accept(*this);
m_context.adjustStackOffset(-1);
m_context << Instruction::STOP; m_context << Instruction::STOP;
} }
else else
@ -536,7 +531,8 @@ bool ContractCompiler::visit(FunctionDefinition const& _function)
m_context.adjustStackOffset(-(int)c_returnValuesSize); m_context.adjustStackOffset(-(int)c_returnValuesSize);
if (!_function.isConstructor()) /// The constructor and the fallback function doesn't to jump out.
if (!_function.isConstructor() && !_function.isFallback())
m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction); m_context.appendJump(eth::AssemblyItem::JumpType::OutOfFunction);
return false; return false;
} }

View File

@ -935,7 +935,7 @@ Json::Value CompilerStack::gasEstimates(string const& _contractName) const
for (auto const& it: contract.definedFunctions()) for (auto const& it: contract.definedFunctions())
{ {
/// Exclude externally visible functions, constructor and the fallback function /// Exclude externally visible functions, constructor and the fallback function
if (it->isPartOfExternalInterface() || it->isConstructor() || it->name().empty()) if (it->isPartOfExternalInterface() || it->isConstructor() || it->isFallback())
continue; continue;
size_t entry = functionEntryPoint(_contractName, *it); size_t entry = functionEntryPoint(_contractName, *it);
@ -943,12 +943,14 @@ Json::Value CompilerStack::gasEstimates(string const& _contractName) const
if (entry > 0) if (entry > 0)
gas = GasEstimator::functionalEstimation(*items, entry, *it); gas = GasEstimator::functionalEstimation(*items, entry, *it);
/// TODO: This could move into a method shared with externalSignature()
FunctionType type(*it); FunctionType type(*it);
string sig = it->name() + "("; string sig = it->name() + "(";
auto paramTypes = type.parameterTypes(); auto paramTypes = type.parameterTypes();
for (auto it = paramTypes.begin(); it != paramTypes.end(); ++it) for (auto it = paramTypes.begin(); it != paramTypes.end(); ++it)
sig += (*it)->toString() + (it + 1 == paramTypes.end() ? "" : ","); sig += (*it)->toString() + (it + 1 == paramTypes.end() ? "" : ",");
sig += ")"; sig += ")";
internalFunctions[sig] = gasToJson(gas); internalFunctions[sig] = gasToJson(gas);
} }